Reading Mixed Data Types

It is possible to use one scanf statement to input a data line containing mixed mode data. In such cases, care should be taken to ensure that the input data items match the control specifications in order and type. When an attempt is made to read an item that does not, match the type expected, the scanf function does not read any further and immediately returns the values read. For example:

scanf (“%d %c %f %s”, &i, &ch, &sum, name);

If the data entered is

11 a 3.346

then it will assign the values to the variables in the order in which they appear.


Printf ()

printf () is the general purpose formatted console output function. The printf statement provides certain features that can be effectively exploited to the control the alignment and spacing of print – out on the terminals.

The prototype for printf () is:

int printf (char *control _ string, argument _ list);

where control _ string consists of two types of items, namely, characters which will be printed to the screen and format specifiers which control how the subsequent arguments are displayed.

The general form of printf statement is

printf (“control string “, argument1, argument2,……. argumentn);

The control string indicates how many arguments follow and what their types are. The arguments argument1, argument2, . . . . . . argumentn are the variables whose values are formatted and printed according to the specifications of the control string. The arguments should match in numbers, order and type with the format specifications.


Format Specifiers

The format specification has the following form:

%w.v type – specifier

Table shows the format specifiers for printf.


img

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext