C Tutorial/printf scanf/printf sign character

Материал из C\C++ эксперт
Перейти к: навигация, поиск

" ": Indicates a space for positive values so that positive values and negative values are aligned

#include <stdio.h>
main()
{
    printf("% d\n", 25);
    printf("% d", -25);
}
25
     -25

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

#include <stdio.h>
main()
{
    printf("%+d\n", -25);
    printf("%+d\n", 25);
}
-25
     +25

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

#include <stdio.h>
int main()
{ 
   printf( "% d\n% d\n", 547, -547 );
   return 0;
}
547
-547

Printing numbers with and without the + flag

#include <stdio.h>
int main()
{ 
   printf( "%d\n%d\n", 786, -786 );
   printf( "%+d\n%+d\n", 786, -786 );
   return 0;
}
786
-786
+786
-786