Pin Me

C Programming For Beginners - Part 9

written by: Noel Kalicharan•edited by: J. F. Amprimoz•updated: 10/19/2008

In this article, we discuss the importance of program layout. We also introduce the "newline character" and show how to use it in producing output.

  • slide 1 of 4

    A word on program layout

    In the previous article, we met the following program:

    main() {printf("Welcome to BrightHub");
    }

    C does not require the program to be laid out as in the example. An equivalent program is

    #include <stdio.h>
    main()
    { printf("Welcome to BrightHub"); }

    or

    #include <stdio.h>
    main() { printf("Welcome to BrightHub"); }

    All that matters is that the order of the words and punctuation is maintained. For this small program, it probably does not matter which version we use. However, as program size increases, it becomes imperative that the layout of the program highlights the logical structure of the program.

    This improves its readability and makes it easier to understand. Indentation and clearly indicating which { matches which } can help in this regard. We will see the value of this principle as our programs become more substantial.

  • slide 2 of 4

    Writing output with printf

    Suppose we want to write a program to print the lines (from The Gitanjali by Rabindranath Tagore):

    Where the mind is without fear

    And the head is held high

     

    Our initial attempt might be:

    #include <stdio.h>
    main() {printf("Where the mind is without fear");printf("And the head is held high");
    }

    However, when run, this program would print:

    Where the mind is without fearAnd the head is held high

    Note that the two strings are joined together (we say the strings are concatenated). This happens because printf does not place output on a new line, unless this is specified explicitly. Put another way, printf does not automatically supply a newline character after printing its argument(s). A newline character would cause subsequent output to begin at the left margin of the next line.

    In the example, a newline character is not supplied after fear is printed so that And the head... is printed on the same line as fear and immediately after it.

  • slide 3 of 4

    The newline character, \n

    To get the desired effect, we must tell printf to supply a newline character after printing ...without fear. We do this by using the character sequence \n (backslash n) as in the following:

    #include <stdio.h>
    main() {printf("Where the mind is without fear\n");printf("And the head is held high\n");
    }

    The first \n says to terminate the current output line; subsequent output will start at the left margin of the next line. Thus, And the... will be printed on a new line. The second \n has the effect of terminating the second line. If it were not present, the output will still come out right, but only because this is the last line of output. A program prints all pending output just before it ends.