Input & Output Formatting
Input & Output Formatting
Size Modifier:
Used to modify the type specified by the conversion code. Example:
short, long etc.
Width Modifier:
Used to specify minimum number of positions in the output. (If the data to be displayed requires more
space, then printf overrides the width.) ie., In case , you give 2 as minimum width (%2d)
(i)you need to print 450, printf will not eliminate any digit, 450 is printed.
(ii)you need to print 5, printf will display 05 or (space)5 is displayed.
If a floating point number is being printed, then we may specify the number of decimal places to be
printed with precision modifier. The precision modifier has the format (dot) .m where m is the
number of digits after decimal point.
When both width & precision are used, the width must be large enough to contain the integral value
of the number, the decimal point and the number of digits in the decimal position.
Flag modifier:
Used for (i) justification (ii) padding and (iii)sign
(i)Justification controls the placement of a value when it is shorter than the specified width.
Justification can be left/right.
The default is right justification.
To left justify a value, the flag is set to minus (-).
(ii)Padding defines the character that fills the unused space when the value is smaller than the print
width. It can be space (default) or zero. If there is no flag defined for padding, the unused width is
filled with spaces.
If the flag is 0, the unused width is filled with zeros.
The zero flag is ignored if it is used left justification because adding zeros after a number changes its
values.
(iii) Sign Flag defines the use or absence of a sign in a numeric value , there are 3 formats.
(a)default formatting: inserts a sign only when the value is –ve . Positive values are formatted without
a sign.
(b) Flag set to plus: signs are printed for both +ve and -ve values
(c ) Flag is space : Then +ve numbers are printed with a leading space and –ve numbers with a minus
effort.
#include<stdio.h>
void main() int
a,b; float c,d;
a=15; b=a/2;
printf(“%d\n”,b); --->7
printf(“%3d\n”,b); ---> _ _ 7 ( here in this statement _
means space) printf(“%03d\n”, b); ---> 007
printf(“%-03d\n”, b); ---> 7 (padding is overruled because left justification)
c=15.3; d=c/3;
printf(“%4.2f\n”,d); ---> 5.10
}
2 printf(“%04d”,25); 0025
3 printf(“%i”,1234); 1234
4 printf(“%4.2f”,3.14159); 3.14
5 printf(“%x”, 255); FF
(Hexadecimal)
6 printf(“%o\n”, 255); 377 (Octal)
19 printf(“%d”, (scanf(“%d%d”,&a,&b))); 2
20 printf(“%d”, (scanf(“%d%d%d”,&a,&b,&c))); 3
2. There is only 1 flag for input formatting, the assignment suppression (*). (*) tells scanf that the
next input field is to be read but not stored. It is discarded.
Example: scanf(“ %d %*c %f ”, &x, &y);
3. Width Specification:
• Width specification with input formatting is maximum and not minimum(as in printf)
• The width modifier specifies the maximum number of characters that are to be read
for one format code (%d or %c or %f, etc)
• When a width specification is included ,scanf reads until the maximum number of
characters have been processed or until scanf finds a white space character.
• If scanf finds a white space character before max is reached , it stops.
#include<stdio.h>
void main()
{ float a;
float a;
printf(“Enter a”);
scanf(“%f”,&a); --->2.1
printf(“a=%f\n”,a); ---
>2.100000
}
After scanning, print the variable values , make observations and note them down.
#include <stdio.h>
#include <stdlib.h>
main( )
{
char ch;
printf("Enter a character: ");
ch=getchar();
printf("You have entered a character: ");
putchar(ch);
return 0;
}