Program Layout, printf and the newline character

Page content

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.

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.

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.

References

Reference text: C Programming - A Beginner’s Course

Related programming references

This post is part of the series: C Programming for Beginners

A straightforward introduction to Programming in C for people with no previous programing experience.

  1. C Programming For Beginners - Part 1
  2. C Programming For Beginners - Part 2
  3. C Programming for Beginners – Part 3
  4. C Programming for Beginners – Part 4
  5. C Programming For Beginners - Part 5
  6. C Programming For Beginners - Part 6
  7. C Programming For Beginners - Data Types
  8. C Programming For Beginners - Part 8
  9. C Programming For Beginners - Part 9
  10. C Programming For Beginners - Part 10
  11. C Programming For Beginners - Part 11
  12. C Programming For Beginners - Part 12
  13. C Programming For Beginners - Part 13
  14. C Programming For Beginners - Part 14
  15. C Programming For Beginners - Integer Data Types
  16. C Programming for Beginners - Part 16
  17. C Programming For Beginners - Integer Expressions, Operators and Precedence
  18. C Programming For Beginners - Part 18
  19. C Programming For Beginners - Printing Double and Float
  20. C Programming For Beginners - Mixing double, float and int