Often in a code snippet, I will see

printf("%d %2d %3d", i,i*i,i*i*i);

What does putting the numbers in front do, since it works the same without them? (ie)

printf("%d %d %d", i,i*i,i*i*i);

The first number indicate the minimum width of the field, for example "%2d" will print a number at least two characters wide. If the actual number is only one digit the first character will be a space. If the actual number is more than 2 digits the program will display however many digits are needed to format the number.

>>What does putting the numbers in front do, since it works the same without them
No it does not. Replace the value of i with the number 1 and see how different it is when you replace i with a number such as 12345.

/*
 * p_operators.c
 * it demostrates some of the converter operators in printf()
 *
 */
#include <stdio.h>

int main( void )
{
    int x = 123;
    double y = 4.5678;
    int    len = 9;
    char text[10] = "Hello";
    
    /*
     *    string formatting options
     */
     printf( "\nString formatting options\n" );
     printf( "123456789012345678901234567890\n" ); /* 30 digits as ruler reference */
     printf( "%s<<<<\n", text );    /* display text, the <<<< will tell you where the string ends */
     printf( "%10s<<<<\n", text );  /* a minimal of ten characters displayed. The string is right justified */
     printf( "%-10s<<<<\n", text ); /* same above left justified */
     printf( "%.3s<<<<\n", text ); /* after the point, max of char to be output */
     printf( "%.*s<<<<\n", len, text ); /* after point a *, meaning get the maximum character output from an integer parameter, in this case len */
     
     
     /*
      * integer formatting options
      */
      printf( "\nInteger formatting options\n" );
      printf( "123456789012345678901234567890\n" ); 
      printf( "%d<<<<\n", x );         
      printf( "%10d<<<<\n", x );    /* print integer in a field of 10, fill with spaces if necesary */
      printf( "%010d<<<<\n",x );    /* same that above, by sustitue spaces for `0's' */
      printf( "%*d<<<<\n", len, x );/* print in a field determined by the parameter, this case len */
      printf( "%-10d<<<<\n", x );   /* print in a field of 10 left justified */
      printf( "%.1d<<<<\n", x );   /* print a maximun of one, however if the integer is longer it will ignore the limit */
      
      /*
       * character formatting options
       */
       printf( "\nCharacter formatting options\n" );
       printf( "123456789012345678901234567890\n" ); 
       printf( "%c<<<<\n", text[0] );   /* just one char */
       printf( "%10c<<<<\n", text[0] ); /*  one char 10 places right justified */
       
       /*
        * floating-point formatting options        
        */
        printf( "floating-point formatting options\n" );
        printf( "123456789012345678901234567890\n" ); 
        printf( "%f<<<<\n", y );    /* six decimal places printed */
        printf( "%10f<<<<\n", y );  /* a field of 10 padded with spaces if necesary */
        printf( "%10.3f<<<<\n", y ); /* 10 is the field and .3 is the decimal maximum */
        printf( "%10.8f<<<<\n", y ); /* field of 10 will pad if necesary, 8 decimal places */
        
        getchar();
        return 0;
}

Output:

String formatting options
123456789012345678901234567890
Hello<<<<
     Hello<<<<
Hello     <<<<
Hel<<<<
Hello<<<<

Integer formatting options
123456789012345678901234567890
123<<<<
       123<<<<
0000000123<<<<
      123<<<<
123       <<<<
123<<<<

Character formatting options
123456789012345678901234567890
H<<<<
         H<<<<
floating-point formatting options
123456789012345678901234567890
4.567800<<<<
  4.567800<<<<
     4.568<<<<
4.56780000<<<<

the numbers before them represents the limit of places(tens,hundreds etc.) of the data type(usually float/integer) should be..

get it?

AWesome post, as soon as i can i will start posting my codes, may not be strictly related to anything posted here, but it may help others to solve issues.

I love it here, i will become more active as my knowledge grow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.