C Special Characters

1. What are Special Characters in C?

  • Special characters are prefixed with a backslash \.

  • They are used for newline, tab, quotes, backslash, etc.

  • Mainly used in strings and character constants.


2. Common Special Characters (Escape Sequences)

Escape Sequence Description Example Output
\n Newline Moves cursor to next line
\t Horizontal tab Adds tab spacing
\\ Backslash Prints \
\' Single quote Prints '
\" Double quote Prints "
\a Alert / bell Makes a beep sound
\b Backspace Deletes previous character
\r Carriage return Moves cursor to line start
\v Vertical tab Moves cursor down a vertical tab
\0 Null character Marks end of string
\? Question mark Prints ?
\f Form feed Moves cursor to next page (rarely used)

3. Example – Using Newline \n and Tab \t


 

Output:

Hello
World!
Name Age City
Alice 25 Delhi
Bob 30 Mumbai

4. Example – Printing Quotes and Backslash

#include <stdio.h>

int main() {
printf(“She said, \”Hello!\”\n”); // double quotes
printf(“Path: C:\\Program Files\\MyApp\n”); // backslash
return 0;
}

Output:

She said, "Hello!"
Path: C:\Program Files\MyApp

5. Example – Alert and Backspace

#include <stdio.h>

int main() {
printf(“Alert sound\a\n”); // beep
printf(“Hello\bWorld\n”); // backspace deletes last character
return 0;
}

Output:

Alert sound
HellWorld

Notice \b removed the last o in Hello.


6. Key Points About Special Characters

  1. Always use backslash \ to denote special characters.

  2. Can be used in strings (" ") or character constants (' ').

  3. Useful for formatting output, path names, or control characters.

  4. Some are rarely used today (\f, \v), but basic ones (\n, \t, \\, \") are common.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

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