Write a program to i/o and o/p charecteristic with an illustration program
Answers
C programming input output functions
We can classify I/O function into two categories:
Console I/O function
File I/O function
Console Input-Output functions
Console simply means screen and keyboard. There are two types of a console I/O functions:
Formatted input-output function
Unformatted input-output function
The major difference is that formatted function allows us to format the input from the keyboard and the output to be displayed on the screen.
c programming input output functions : printf and scanf
We can see that printf( ) and scanf( ) are formatted input/output function.
So let’s discuss printf( ) and scanf( ):
C programming I/O function: printf( )
printf( ) is the standard library function that is used for precise output formatting.
syntax of printf( ) function:
printf( format-control-string, other-arguments );
Format control string in printf( ) function describes the output format which consists of conversion specifiers, precisions, flags, field widths and literal characters.
Each conversion specifier starts with % sign and ends with a conversion specifier.
We can perform following formatting options with the help of printf( ):
Right and left justification.
Rounding floating-point values.
Inserting literal characters.
Displaying all types of data with appropriate size and precisions.
How printf( ) funtion works?
#include <stdio.h>
int main ()
{
int num = 22;
printf("\nNumber: %d", num);
return 0;
}
Output
Number: 22
Explanation
First of all, printf( ) scans the format control string from left to right. It shows characters onto the screen until it encounters % or \.
In the above program, first, it encounters \n and places the cursor at the beginning of the new line.
Now, Number: is displayed on the screen.
When it encounters conversion specification in the format control string it picks up the first variable from other arguments such as num in the above example.
This process continues until the end of format control string.
Integer conversion specifiers
An integer is a whole number that can be displayed in several formats.
Conversion
Specifier Description
d Signed decimal Integer
i Signed decimal integer
o An unsigned octal integer
u An unsigned decimal integer
x or X An unsigned hexadecimal integer
h, I or II When placed before conversion specifier indicates short, long or long long integer. These are called length modifiers.
Floating-point conversion specifier
A floating-point value contains decimal point.
Conversion
Specifier Description
e or E Floating-point values in exponential notation
f or F Floating-point values in fixed notation
g or G Floating-point value in either floating-point form or exponential form based on length of the value
L Indicates a long double floating-point value if placed before floating-point conversion specifier
Conversion Specifier for strings and character
Conversion
Specifier Description
c Print individual character
s Print string until terminating null character \0 is encountered and requires a pointer to char as an argument
Printing with field width and precision
With the help of field width, we can change the position of data being displayed on the screen.
If the field width is greater than data being displayed, then the data is right justified.
#include <stdio.h>
int main ()
{
printf("%4d\n", 1);
printf("%4d\n", 12);
printf("%4d\n", 123);
printf("%4d\n", 1234);
printf("%4d\n", 123456);
}
Output
1
12
123
1234
123456
Precision has a different meaning when used with different data types.
When used with the integer, it indicates the minimum number of digits to be printed.
Thus, if the value to be printed contains fewer digits than the precision value, zeros are prefixed until the total number of the digit is equivalent to the precision.
When used with the floating-point, it indicates the number of digits to appear after the decimal point.