There is a set of special characters, called escape characters, which add special things to your printf statements. Escape characters begin with a backslash: \ and are followed by a letter or symbol, such as:

  1. newline character: \n
  2. tab indent: \t
  3. beep sound: \a

The most important escape character is the newline character, which inserts a blank line in your text. The newline character is written: \n. Let’s edit our first.c program to add some newline characters:

#include <stdio.h>
void main( )
{
printf(“\nWe are learning C today.\n“);
printf(“\nNewline characters add blank lines.\n“);
 

Save your first.c source code. Compile it. Link it. Run it. With the newline characters, your text will appear like this:

We are learning C today. Newline characters add blank lines. 

 

Did it work? If you got error messages during the compilation, go back into your editor and fix your mistakes.

Let’s try another escape character: \t, which inserts a tab (automatic spacing) in your text.

#include <stdio.h>
void main( )
{
printf(“\nThere is a tab here: \t Can you see the spacing?”);
printf(“\nHere it is again: \t Can you see it?”);
 

Save your source code as tab.c. Compile it. Link it. Run it. With the tabs, your text will appear like this:

There is a tab here:
Here it is again:
Can you see the spacing?
Can you see it?

Continue on to Variables.

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Join the Conversation

2 Comments

Leave a comment

Your email address will not be published. Required fields are marked *