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

C প্রোগ্রামিং এর putc() Function এর ব্যবহার

Using the putc() Function
The putc() function writes a character to the specified file stream, which, in our case, is the standard output pointing to your screen.

 

C প্রোগ্রামিংয়ের getchar() Function এর ব্যবহার

Using the getchar() Function
The C language provides another function, getchar(), to perform a similar operation to getc(). More precisely, the getchar() function is equivalent to getc(stdin).

 

The syntax for the getchar() function is

 

C প্রোগ্রামিংয়ে getc() Function এর ব্যবহার

Using the getc() Function
The getc() function reads the next character from a file stream, and returns the character as an integer.

 

The syntax for the getc() function is

 

C প্রোগ্রামিং এর double Data Type সম্পর্কে আলোচনা

The double Data Type
In the C language, a floating-point number can also be represented by another data type, called the double data type. In other words, you can specify a variable by the double keyword, and assign the variable a floating-point number.