Submitted by tushar pramanick on Tue, 03/05/2013 - 15:32

Aligning Output
As you might have noticed in the previous section, all output is right-justified. In other words, by default, all output is placed on the right edge of the field, as long as the field width is longer than the width of the output.

 

You can change this and force output to be left-justified. To do so, you need to prefix the minimum field specifier with the minus sign (-). For example, %-12d specifies the minimum field width as 12, and justifies the output from the left edge of the field.

 

Listing 5.7 gives an example of aligning output by left- or right-justification.

 

TYPE
Listing 5.7. Left- or right-justified output.

 

1:  /* 05L07.c: Aligning output */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int num1, num2, num3, num4, num5;
7:
8:     num1 = 1;
9:     num2 = 12;
10:    num3 = 123;
11:    num4 = 1234;
12:    num5 = 12345;
13:    printf("%8d  %-8d\n", num1, num1);
14:    printf("%8d  %-8d\n", num2, num2);
15:    printf("%8d  %-8d\n", num3, num3);
16:    printf("%8d  %-8d\n", num4, num4);
17:    printf("%8d  %-8d\n", num5, num5);
18:    return 0;
19: }

 

OUTPUT
I get the following output displayed on the screen after I run the executable 05L07.exe from a DOS prompt on my machine:

 

C:\app> 05L07

           1  1

          12  12

         123  123

        1234  1234

       12345  12345

    C:\app>

 

ANALYSIS
In Listing 5.7, there are five integer variables, num1, num2, num3, num4, and num5, that are declared in line 6 and are assigned values in lines 8_12.

 

These values represented by the five integer variables are then printed out by the printf() functions in lines 13_17. Note that all the printf() functions have the same first argument: "%8d %-8d\n". Here the first format specifier, %8d, aligns the output at the right edge of the field, and the second specifier, %-8d, does the alignment by justifying the output from the left edge of the field.

 

After the execution of the statements in lines 13_17, the alignment is accomplished and the output is put on the screen like this:

 

       1  1

      12  12

     123  123

    1234  1234

   12345  12345

Related Items

The #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Why is random access to a disk file necessary?