First C Program & System Output

Escape Sequences (\n, \t, \") Deep Dive

Control line breaks, tabs, and special characters using backslash escape codes.

1. The Hook Have you ever tried typing a document without ever hitting the Enter key or Spacebar? All your words would smash together into one unreadable block of text!

How do we tell printf to break lines or create tab columns?


2. Real World Story When typing on a typewriter, there was a physical lever called Carriage Return that moved the paper roller to the next line.

In C, we use Escape Sequences starting with a backslash \ to perform formatting commands!


3. Problem How do you print quotation marks inside text or move to a new line inside a single printf?


4. Concept: Escape Sequence Lookup Table

| Escape Code | Name | What It Does | Example | | :--- | :--- | :--- | :--- | | \n | Newline | Moves cursor to start of next line | printf("A\nB"); -> A on line 1, B on line 2 | | \t | Tab Space | Inserts 4-8 tab spaces | printf("Name:\tAmit"); | | \\" | Double Quote | Prints literal double quote " | printf(\"Hello \\"BEU\\"\"); -> Hello "BEU" | | \\ | Backslash | Prints literal backslash \ | printf("C:\\Files"); |


5. Mental Model - `\n` = Hitting Enter key on keyboard. - `\t` = Hitting Tab key on keyboard.


6. Code Example

#include <stdio.h>

int main() { printf("Subject\t\tMarks\n"); printf("---------------------\n"); printf("Physics\t\t95\n"); printf("Maths\t\t98\n"); return 0; } ```


7. Dry Run 1. printf("Subject\t\tMarks\n") prints Subject, tabs over twice, prints Marks, then moves to next line. 2. Formatted tabular scorecard appears on screen.


8. Common Mistakes - Using forward slash /n instead of backslash \n. - Forgetting \n at the end of lines, causing outputs to merge together.


9. Viva Corner - Q: What is the difference between \n and \r? - A: \n moves the cursor to the next line. \r (carriage return) moves the cursor back to the start of the current line.


10. Summary Today you mastered line breaks with \n and column formatting with \t!

C Runner