Check The Programming Section
Flags are fairly uncommon in format specifiers and although several flag options exist, you are most likely to use only two of them. The first is a minus sign (-), which you will use in conjunction with a field width specifier.
Consider the example given below, names are left justified and ages are right justified to names. To print name and age in a single line the format string of the prinf() could be "%15s %d".
Henry Jones 23
Sam Smith 14
The required code to print the name and age is given below:
#include<stdio.h>
int main(){
printf("%-15s %d\n","Henry Jones",23);
printf("%-15s %d","Sam Smith",14);
}
In the above code, 15 represents the width of the string and the - (minus) flag makes the string output to left justified and then age of the person gets printed in same line. Now consider the output as default right justified without using - (minus) flag in the formatted string of the printf.
~~~~Henry Jones 23
~~~~~~Sam Smith 14
The required code for default right justified as
#include<stdio.h>
int main(){
printf("%15s %d\n","Henry Jones",23);
printf("%15s %d","Sam Smith",14);
}
This flag is only meaningful with numeric data. By default, when printf displays a number, it prints a minus sign for negative numbers, but it does not print a plus sign for positive numbers. If you want the plus sign to appear, this flag will cause printf to display it.
#include<stdio.h>
int main(){
int num = 89;
int val = -34;
printf("%+d %+d",num, val);
}
Here + (plus) flag forces to print the sign of the number. For negative number it preceeds minus (-) sign before the number and for positive number the plus (+) signs preceeds the value. The above code produces the output as:
+89 -34
It prints a blank space before the value, if value is negative then minus (-) sign is placed. Consider the example given below, where val is initialized with a positive value and num is initialized with a negative value
#include<stdio.h>
int main(){
int val = 34;
int num = -30;
printf("% d\n",val);//line1
printf("% d",num);//line2
}
The printf() at line1 inserted a space before the value of val and printf() at line2 prints the value of num without inserting an space. Because num have a negative value and the above code will produce the output as:
~34
-30
It is used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero. o is used as format specifier to convert a decimal number to its equivalent octal and x is used to prints its equivalent hexa-decimal number. Consider the example given below:
#include<stdio.h>
int main(){
int val = 14;
printf("%d in Octal = %#o\n",val,val);
printf("%d in hexadecimal (uppercase) = %#X\n",val,val);
printf("%d in hexadecimal = %#x",val,val);
}
The flag character # placed the appropriate format character before the equivalent octal and hexadecimal number as follows:
14 in Octal = 016
14 in hexadecimal (uppercase) = 0XE
14 in hexadecimal = 0xe
Left-pads the number with zeroes (0) instead of spaces when padding is specified with width sub-specifier. For example,
#include<stdio.h>
int main(){
int val = 14;
printf("%05d\n",val);//line1
printf("%5d",val);//line2
}
At line1 0 (zero) is padded before the value and 14 is displayed as right justified. Similarly at line2 14 get displayed as right justified, but instead of 0 padding blank spaces are padded before 14. In the above example, 5 is the size of the width specifier and the output of the code is as follows:
00014
1