C Tutorial/printf scanf/printf Justify

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

Flags

Flag characters are used to give directives for the output.

You can use multiple flag characters in any order.

- Indicates that output is left justified.


<source lang="cpp">#include <stdio.h> main() {

   printf("%-10.4d\n", 25);
   printf("%10.4d\n", 25);

}</source>

0025
           0025

Right justifying and left justifying values

<source lang="cpp">#include <stdio.h> int main() {

  printf( "%10s%10d%10c%10f\n\n", "hello", 7, "a", 1.23 );
  printf( "%-10s%-10d%-10c%-10f\n", "hello", 7, "a", 1.23 );
  return 0;

}</source>

hello         7         a  1.230000
hello     7         a         1.230000