Variations of “%d” Format Specifier in C

2

Format specifiers can be defined as the operators which are used in association with printf() function for printing the data that is referred by any object or any variable. When a value is stored within a particular variable then you cannot print the value stored in the variable straightforwardly without using the format specifiers. You can retrieve the data that are stored in the variables and can print them onto the console screen by implementing these format specifiers in a printf() function.

Format specifiers start with a percentage % operator and followed by a special character for identifying the type of the data. There are different types of basic format specifiers in C language and there variations may be helpful in Output Formatting .

Basic format Specifiers in C

Variations of “%d” format specifier

 int c = 12345;

// Right Justification
 printf("%2d\n", c);

 // Left Justification
 printf("%-2d\n", c);
// C program to illustrate
// Variations of %d format specifier
#include <stdio.h>
int main()
{
    int c = 12345;

    // Right Justification
    printf("%2d\n", c);
    printf("%10d\n", c);
    printf("%010d\n", c);
    printf("%010.2d\n", c);
    printf("%010.6d\n", c);
    printf("%+010.6d\n", c);

    // Left Justification
    printf("%-2d\n", c);
    printf("%-10d\n", c);
    printf("%-010d\n", c);
    printf("%-010.2d\n", c);
    printf("%-10.6d\n", c);
    return 0;
}

Output:

 12345
      12345
 0000012345
      12345
     012345
    +012345
 12345
 12345
 12345
 12345
 012345

Explanation :

  • Numeral preceding ‘d’ defines the minimum length of output should be 2 with right justification but here the length of output is more than 2 (i.e. 5). So full length of the variable will be is printed.
    int c = 12345;
    printf("%2d\n", c);

    OUTPUT:"12345"

  • Numeral preceding ‘d’ defines the minimum length of output should be 10 with right justification but unlike the previous example here, the length of output is less than 10.So in this case the output would be right justified with total length of 10.
    printf("%10d\n", c);

    OUTPUT:"_____12345"

  • Numeral preceding ‘d’ defines the minimum length of output should be 10 with right justification but when the numeral is preceded with ‘0’ then all the blank spaces when right justified is filled with zero…but incase it is preceded by any other digit it is considered in the minimum length of output.
    printf("%010d\n", c);

    OUTPUT:"0000012345
    NOTE: BUT similar is not the case with LEFT Justification for this case. In Left Justification no preceding zeroes are printed.

  • Numeral preceding ‘d’ defines the minimum length of output should be ’10’ with right justification as in above example and the number (here, 2) after decimal signifies the relative number of zeroes to be preceded by the output and the zeroes are printed in case the relative number comes out to be greater than zero(0), relative number of zeroes means (number – length of ‘c’) i.e. (2-5=-3). It would be more clear in the coming example.
    printf("%010.2d\n", c);

    OUTPUT:"_____12345"

  • Numeral preceding ‘d’ defines the minimum length of output should be ’10’ with right justification as in above example and the number (here, 6) after decimal signifies the relative number of zeroes to be preceded by the output and the zeroes are printed in case the relative number comes out to be greater than zero(0), relative number of zeroes means (number – length of ‘c’) i.e. (6-5=1).Since, 1 is greater than zero it would be reflected in the output.
    printf("%010.6d\n", c);

    OUTPUT:"____012345"

  • ‘+’ sign preceded before all combination of numerals forces the compiler to use ‘+’ symbol before output, the sign symbol is not counted in the length of the output
    printf("%+010.6d\n", c);
    

    OUTPUT:"____+012345"


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.