Home > Tutorials > C Tutorials > Programming in C: Escape Characters and Tabs

Programming in C: Escape Characters and Tabs

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.

About 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.

Check Also

How To Populate A DIV via an HTML SelectBox, PDO, and jQuery

Imagine you have a database full of information, and you want to let users browse ...