2-Input Output Statements
2-Input Output Statements
The list of addresses of variables are used so that scanf() function can place the data
received from a standard input device. The address of a variable is obtained by using address
operator.
printf() function – The printf() function allows to output the data in a fixed format. Its
general form is printf(―format string‖, list of variables);
The printf() function examines the format string from left to right. It keeps on sending
the characters to the standard output device until it encounters either character ‗%‘ or
character ‗\‘(backslash). The moment it encounters %, it picks up the corresponding variable
and outputs its value in the specified format.
The format string of the printf() function contains format specifiers, the sequences
that begin with character ‗\‘ (back slash), known as escape sequences, and the text to be
output alongwith the output data.
All the format specifiers used with scanf() functions are valid for printf() function.
As we see in the output, that the printf() function outputs six (6) decimal digits for a single
precision real value, because in the format specifier, any additional information is not
specified. In general, to have a control over the output, additional information can be passed
in printf() function.
With this additional information, the format specifier for a real value takes the form
%w.df Where w is total width or space used for outputting a real value
including decimal point and the number of decimal digits; and d is the number of decimal
digits to be outputted.
For an integer value %wd
For a string value %ws
By default, the output value always right justified in the field. It is also possible to
output a value left unjustified in the field by writing hyphen ‗—‗ after character ‗%‘ as shown
below
%-5d
int a = 125;
printf(―%6d‖,a); 1 2 5
printf(―% - 6d‖,a); 1 2 5
Exercise
1. Write a program in c to add three values given by the user.
2. Write a program in C to calculate area of rectangle and perimeter.
3. Write a program in C to convert temperature given into Fahrenheit into Celsius.
4. Write a program to convert the time given in seconds to Hour,Minutes and Seconds.
5. Write a program to find the sum of digits of a 4-digit integer number.
6. Write a program to print 4-digits positive integer number in reverse order.