Formatted input output in c

Formatted Input Output in C

Formatted input-output means that the input and the output of a C program will be in a particular format. In C, this requirement is done by format specification is only intended to represent whatever result will be produced in a better format. In the following paragraphs, information regarding different format specifications has been shared.

Format For Integer Input

%wd: Here the letter ‘d’, represents the conversion specification character for an integral value, while ‘w’ specifies an integer number that represents the maximum field width of the input data. If the length of the input data exceeds this field width, then that integral value cannot be stored correctly in that input field.

Example: scanf(“%2d%4d”, &u, &v);

Now if the length of the input data is less than or equal to the field width, then the input data can be correctly stored in that (variable) without any change. If the length of the input data is more than the field width, then the altered input data will be stored in the variable.

  • Input: 5 35
    Output: Now because the length of the input data is less than the field width, the correct input will be stored in the variables. 5 and 35 will be stored correctly in the variables u and v respectively.
  • Input: 54 3456
    Output: Now because the length of the input data is equal to the field width, the correct input will be stored in the variables. 54 and 3456 will be stored correctly u and v respectively.
  • Input: 512 67854
    Output: Now the length of the input data is greater than the field width, the input will be stored in the variables incorrectly. 51 will be stored in u while 2 will be stored in the variable v.

Format For Integer Output

%wd: Here  ‘d’ is used in its standard meaning while ‘w’ specifies the integer number which represents the minimum field width of any integer output data.

Example: printf(“u=%2d, t=%4d”, u, t);

  • Now, if the length of the variable is less than the field width the variable is right-justified, with leading blank(s).
    Input: 5 35
    Output: u= 5, t= 35
  • Now, if the length of the variable is equal to the field width the value of the variable will be printed correctly.
    Input: 35 3591
    Output: u=35, t=3591
  • Now, if the length of the variable is greater than the field width the value of the variable will be printed correctly.
    Input: 3589 359654
    Output: u=3589, t=359654

Format For Floating Point Output

%wf: Here ‘w’ specifies an integer number that represents the maximum field width of the input data. This field width includes the digits before and after decimal along with decimal.

Example: scanf(“%3f%5f”, &u, &v);

Now if the length of the input data is less than or equal to the field width, then the input data can be correctly stored in that (variable) without any change. If the length of the input data is more than the field width, then the altered input data will be stored in the variable.

  • Input: 5 31.5
    Output: Now because the length of the input data is less than the field width, the correct input will be stored in the variables. 5.00 and 31.50 will be stored correctly in u and v respectively.
  • Input: 5.4 34.56
    Output: Now because the length of the input data is equal to the field width, the correct input will be stored in the variables. 5.4 and 34.56 will be stored correctly in u and v respectively.
  • Input: 5.12 67.854
    Output: Now the length of the input data is greater than the field width, the input will be stored in the variables incorrectly. 5.1 will be stored in u while 2.0000 will be stored in the variable v.

Format For Floating Point Output

%w.nf: Here ‘f’ is used in its standard meaning to represent the floating-point number, while ‘w’ specifies the integer number which represents the minimum field width of any integer output data. Here ‘n’ specifies the number of digits after the decimal. By default, the six digits have to be printed after the decimal.

Example: printf(“u=%4.2f, t=%6.2f”, u, t);

Now, if the length of the variable is less than the field width the variable is right-justified, with leading blank(s). If the number of digits after the decimal is exceeded the number ‘n’ then the extra digits are rounded off.

  • Input: 5 3.5
    Output: u= 5.00, t= 3.50
  • Now, if the length of the variable is equal to the field width the value of the variable will be printed correctly.
    Input: 3.15 357.91
    Output: u=3.15, t=357.91
  • Now, if the length of the variable is greater than the field width the value of the variable will be printed correctly.
    Input: 3.589 359.654
    Output: u=3.58, t=359.65

Format For String Input

%ws: Here ‘w’ specifies the total number of characters that will be stored in the string when the code is executed. In fact, it stores the first ‘w’ characters of the input. ‘s’ specifies the input type is a string.

Example:
char str [10];
scanf(“%4s”, str);

Input: Programming
Storage: Only the first four characters will be stored in the corresponding string. By default, a null character will also be stored at the end. So the final storage will be like this ‘P’, ‘r’, ‘o’, ‘g’, ‘\0’.

Format For String Output

%w.ns: In formatted string output the decimal point and the latter ‘n’ are used optionally. Here ‘n’ specifies the first ‘n’ characters of the string that will be displayed along with the ‘w-n’ leading blanks.

Example:
printf(“%4s”, “Programming”);

Output: Programming

Example:
printf(“%10s”, “Ram”);

Output: _______Ram

Example:
printf(“%.4s”, “Programming”);

Output: Prog

Example:
printf(“%10.4s”, “Programming”);

Output: ______Prog

Suppression Character In Input

While writing a C program if a need arises to skip any of the input fields, then we just have to specify the symbol asterisk (*) between the% conversion specification character. In this case, the input field is to be read but its value will not be assigned to any of the address here the character asterisk (*) is called the Suppression character.

Example:
scanf(“%d %*d %d”, &x, &y, &z);

Input: 5 10 15
Result: Here the value ‘5’ will be stored into the variable ‘a’. The value ’10’ will be skipped because of the Suppression Character. The value ’15’ will be stored in the variable ‘y’, and the variable ‘z’ will have the garbage value.

getchar ( ) and putchar()

The getchar ( ) and putchar() are commonly known as macros in C programming language, and they are used for the character Input and character output. The getchar ( ) basically reads a single input character from the standard input. While the putchar() in turn gives one character at a time as an output to the standard output device.

Example:
#include<stdio.h>
main( )
{
char ch;
printf("Enter a character: ");
ch=getchar();
printf("The entered character is: ");
putchar(ch) ;
}
Output:
Enter a character: P
The entered character is: P

Leave a Comment

Your email address will not be published. Required fields are marked *