C printf sign

Description

+ Indicates that i number is printed using a sign character (+ or -).

Example 1

Adding sign for int value


#include <stdio.h>
main()//  w w  w . j  av  a2s  .  c om
{
    printf("%+d\n", -25);
    printf("%+d\n", 25);
}

The code above generates the following result.

Example 2

' ' indicates a space for positive values so that positive values and negative values are aligned


#include <stdio.h>
main()/*  w w  w.  j a v  a2  s.  c om*/
{
    printf("% d\n", 25);
    printf("% d", -25);
}

The code above generates the following result.

Example 3

Printing a space before signed values not preceded by + or -


#include <stdio.h>
/*  w  w  w  . j  av  a  2 s . c o m*/
int main()
{ 
   printf( "% d\n% d\n", 547, -547 );

   return 0;

}

The code above generates the following result.

Example 4

Printing numbers with and without the + flag


#include <stdio.h>
/*from ww  w. jav a 2 s  .  c  o  m*/
int main()
{ 
   printf( "%d\n%d\n", 786, -786 );
   printf( "%+d\n%+d\n", 786, -786 );

   return 0;

}

The code above generates the following result.





















Home »
  C Language »
    Input / Output »




printf
scanf