Placement Prep

Escape Sequences in C: Complete Guide with Examples

A complete reference for C escape sequences with code examples and traced output. Covers newline, tab, null, and placement-round output questions.

By FACE Prep Team 5 min read
c-programming escape-sequences placement-prep coding-rounds string-handling

Escape sequences in C are two-character combinations, always starting with a backslash, that tell the compiler to treat the following character as a control signal rather than a printable symbol.

That one-line definition carries practical weight. Predict-the-output questions in placement coding rounds frequently involve escape sequences. Getting them wrong is rarely a language knowledge gap; it is a cursor-tracing gap. This guide closes it.

What Are Escape Sequences in C?

In C, certain characters have no printable form. A newline, a tab stop, the null byte that marks the end of a string. None of these correspond to a key you can press and see on screen. The C standard solved this with escape sequences: a backslash followed by a specific character that the compiler replaces with a single control character at compile time.

The backslash acts as a signal to the compiler: the character that follows is not to be taken literally. \n is not the letter ‘n’; it is a single byte with ASCII value 10, which terminals interpret as “move to the next line.”

The complete list is documented at cppreference.com. There are 10 standard sequences, plus \xhh (hex value) and \ooo (octal value) for arbitrary byte values.

The Complete Reference Table

SequenceASCII valueDescriptionPlacement test frequency
\n10Newline — moves cursor to next lineVery high
\t9Horizontal tabHigh
\\92Literal backslashHigh
\'39Single quoteMedium
\"34Double quoteMedium
\r13Carriage return — moves cursor to line startHigh (trick questions)
\b8Backspace — moves cursor one position leftHigh (trick questions)
\00Null character — string terminatorVery high
\f12Form feed — advances paper (largely unused today)Low
\v11Vertical tab (largely unused today)Low

The GNU C Library manual covers the full set including hex and octal forms. For placement tests, the eight sequences in the “high” and “very high” rows are the ones worth drilling.

Worked Examples with Output Trace

Each example uses a predict-the-output approach: work through the cursor position after each character before checking the answer.

Example 1: Newline and Tab

#include <stdio.h>

int main() {
    printf("Hello\nWorld\n");
    printf("Name:\tJohn\nAge:\t25\n");
    return 0;
}

Output:

Hello
World
Name:   John
Age:    25

The first \n ends “Hello” and moves the cursor down. \t aligns “John” and “25” to the next tab stop (typically eight spaces wide in most terminals).

Example 2: Literal Backslash and Double Quote

#include <stdio.h>

int main() {
    printf("C:\\Program Files\\MyApp\n");
    printf("He said, \"Hello\"\n");
    printf("It is a fine day\n");
    return 0;
}

Output:

C:\Program Files\MyApp
He said, "Hello"
It is a fine day

\\ produces a single \ in the output. \" lets you embed a double quote inside a double-quoted string. Single quotes inside double-quoted strings need no escaping at all.

Example 3: Backspace and Carriage Return

#include <stdio.h>

int main() {
    printf("Hello\b World\n");
    printf("Start\rEnd\n");
    return 0;
}

Output on most terminal emulators:

Hell World
End

\b moves the cursor one position left without erasing. The space that follows then overwrites ‘o’. For \r, the cursor jumps to the start of the line and “End” overwrites “Sta”, leaving “End” where “Start” was. Most terminals display “End” because “rt” gets overwritten. It is the overwrite behaviour (not deletion) that makes \r and \b appear in placement trick questions.

Example 4: Null Character in Strings

#include <stdio.h>

int main() {
    char str[] = "Hello\0World";
    printf("%s\n", str);
    return 0;
}

Output:

Hello

printf with %s reads bytes until it finds a \0. The array holds “Hello\0World” but printf stops at byte 5. “World” is in memory; it is invisible to every standard string function. This is the most common escape-sequence trap in output-tracing questions.

Common Mistakes and Edge Cases

Escape sequences trip students up in four recurring ways that are worth knowing before an interview or a coding test.

Forgetting the Double Backslash in File Paths

Writing a Windows-style path like C:\Program Files directly in a string literal is wrong. \P is not a recognised escape sequence. The compiler may accept it silently or produce unexpected output depending on the C version. The correct form is C:\\Program Files. This comes up in programs that open files by path on Windows systems.

Using the Newline Sequence in scanf

In printf, \n moves the cursor to the next line. In scanf, putting \n in a format string tells the function to skip any amount of whitespace, including none at all. Adding \n at the end of a scanf format string is a common beginner move that causes the program to sit and wait for non-whitespace input before the scan completes. The fix is to leave \n out of scanf format strings entirely.

Confusing Null with the Character Zero

\0 has ASCII value 0. The character '0' has ASCII value 48. These are not the same. A loop that checks str[i] != '0' to find the string terminator will read past every actual string boundary. The correct termination check is str[i] != '\0' or equivalently str[i] != 0. Placement output-tracing questions sometimes swap these to test exactly this distinction.

The CRLF vs LF Split in File Output

On Windows, text files use \r\n (carriage return followed by newline) as the line ending. On Unix-like systems (Linux, macOS), text files use \n alone. A C program that writes \n to a file opened in text mode on Windows will typically have the runtime add \r automatically. A file opened in binary mode writes whatever you specify. This distinction matters in competitive programming judges that run on Linux: a Windows-compiled program that embeds \r\n may produce output with trailing \r bytes that cause an otherwise correct answer to be marked wrong.

How Placement Tests Use Escape Sequences

Placement evaluation tests at service-tier companies include output-tracing questions in the coding section. Escape sequences appear in three patterns:

  • Straight newline and tab: What does this printf call print? Tests whether the student knows \n moves to the next line and \t adds tab-width spacing.
  • The null trap: a \0 embedded mid-string, where the question asks what %s prints. Tests understanding of string termination.
  • The overwrite trick: a format string using \r or \b, where the expected output depends on cursor position, not character sequence.

The tracing technique: step through each character in the format string, updating a mental model of cursor position and screen contents after each one. For placement preparation books that cover C output tracing, the section on escape sequences is usually in the first chapter on I/O.

Service-tier companies (TCS, Cognizant, Wipro, Infosys) test escape sequences in aptitude and coding combined papers. Product-company interviews go further: the \0-embedded string is a common warm-up question before heap allocation and pointer arithmetic.

Escape Sequences, C, and AI Code Assistants

Understanding why \0 terminates a string, and why printf stops there even when more bytes follow, is the kind of C fundamental that separates students who read code from students who merely run it. LLM-based coding assistants have become standard in engineering workflows, and interviewers increasingly ask candidates to spot the bug in AI-generated code. An AI model writing C will get \n right almost every time. It will occasionally misplace \0 in a loop boundary, or use \r\n on a system that expects \n only. The candidate who catches that earns the offer.

TinkerLLM is where you can work with real LLM APIs directly: ₹299 puts actual API calls in your hands, and testing how the model handles edge cases like null-terminated buffers and backslash-heavy strings is a concrete exercise that goes straight into the “things I can discuss in an interview” column.

Primary sources

Frequently asked questions

What is an escape sequence in C?

An escape sequence is a two-character combination starting with a backslash that the compiler treats as a single special character. The 10 standard sequences cover control characters like newline, tab, and the null terminator.

What does the newline escape sequence do in C?

The newline escape sequence moves the cursor to the beginning of the next line. Every printf call that needs output on a new line ends its format string with the newline sequence.

What is the null character in C strings?

The null character has ASCII value 0 and marks the end of every C string. Functions like printf and strlen stop processing at the first null character they encounter, even if more bytes follow in the array.

What is the difference between a single backslash and a double backslash in C?

A single backslash starts an escape sequence. To print a literal backslash character, you write two backslashes in the source code. The compiler reads the pair as a single backslash to print.

Do escape sequences work in scanf format strings?

In scanf, the newline sequence in a format string matches any amount of whitespace, not a literal newline. Using it at the end of a scanf format string often causes the program to block waiting for non-whitespace input, which is a common beginner mistake.

Which escape sequences appear most in placement output-tracing questions?

Newline, tab, double backslash, null, carriage return, and backspace are most common in placement output-tracing MCQs. Carriage return and backspace are specifically chosen because their terminal behaviour surprises most students.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF