In this blog |
---|
Printf explanation |
printf explanation
synatx :
printf("control string",variable list);
control string : it starts with % symbol and is written as..
%[flags] [width] [.precision][length modifier] type specifier
all the things in square brackets are optional.
variable list : in variable list you would specify the variable for which you want to print value.
eg : printf("%d",var1); //var1=2
this printf would be printing value 2
Flags
flags | usage |
- | it is used to dispaly no on left when you had used width |
+ | it is used to specify +ve sign in fron of no |
0 | when the number is right padded it is used to fill zeros instead of spaces on left |
eg for - :
printf("%-7d",a);
input: output:
a =345 345
a =345 345
here - is flag and 7 is width
eg for 0:
printf("%07d",a);
input: output:
a=345 0000345
width
width is used to format the data , giving spaces on left of the printed no . it is used in the bill's of grocery stores
eg
printf("%7d",a);
input: output:
a=345 345
in the output we are having 4 spaces before the no 345
precesion
precision is used to print specified no of chacaters.
for int: if the precision is greater than the length of no then it will fill 0 to left of no, otherwise do nothing.
eg
printf("%.4d",a);
input output:
a=345 0345
for string : it will just print no of characters
for float : in float precision specify the no to be printed after decimal places
length modifier
length modifier are nothing but the modifier used with the bacic data type , in case of long double datatype f is format specifer and L is length modifier(%Lf).
length | their use |
h | when variable is of type short or unsigned shot |
l | when variable is of type long or unsigned long |
L | when variable is of type long double |
eg for long double:
printf("%Lf",a);
input: output:
a=345.34 345.340000
(type specifier is any valid data type in c)
(leave the comment down below if you have a doubt or suggestions ,thnks🙂)
No comments:
Post a Comment