1 · The Hook
🎣
A railway ticket machine prints your ticket the moment you press a button. Inside it, a processor (the chip that does the work) follows a long list of tiny steps: read the button, look up the fare, print the line “Mumbai → Pune”. It never thinks for itself. It only follows instructions, one at a time, extremely fast.
There is a catch: the processor understands only its own language, long patterns of 0s and 1s. Nobody wants to write a ticket printer in 0s and 1s. So we write the steps in a language people can read, and a tool translates them for the processor. This lesson shows that path, and the first complete program you will write.
2 · Predict First
🤔
What exactly does this print? Think about where each piece of text lands.
printf("Hi\n");
printf("there");
printf("!\n");
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
💡
Predict First — the answer
It prints
Output
Hi
there!
printf prints exactly the characters you give it, nothing more. It never adds a new line by itself. Only \n moves to a new line, so there and ! end up on the same line.
Programs and instructions
An instruction is one tiny step a processor can carry out, such as “add these two numbers” or “show this letter on the screen”. A program is a list of instructions that, followed in order, does a job.
The processor itself understands only machine code: instructions written as patterns of 0s and 1s, different for each kind of processor.
Source code → compiler → executable
You write the program in C, a programming language made of English-like words and symbols. The text you type is the source code, saved in a file such as hello.c.
A compiler is a program that reads your source code, checks that it follows the rules of C, and translates it into machine code. The result is an executable file (on Windows, hello.exe) that the processor can run.
- If your text breaks a rule of C (for example a missing
;), the compiler refuses and reports a compile-time error. No executable is produced. - If it compiles, you can run the executable as many times as you like without compiling again.
The shape of a first C program
#include <stdio.h>
int main(void)
{
printf("Hello, GATE!\n");
return 0;
}
Line by line:
#include <stdio.h>brings in the standard input/output toolbox, which containsprintf. Without it the compiler does not know whatprintfis.int main(void)names the functionmain. A function is a named block of instructions. Every C program starts running at the first line insidemain. The wordintin front says thatmainhands back a whole number when it finishes;(void)says it takes nothing in.{and}(curly braces) mark where the body ofmainstarts and ends.printf("Hello, GATE!\n");is a statement: one complete instruction. Every statement ends with a semicolon;.return 0;endsmainand hands back 0, which by convention means “finished without problems”.
Running it prints:
Output
Hello, GATE!
printf prints exactly what you write
The text between double quotes is a string literal. printf copies it to the screen character by character. It adds no spaces and no new line of its own.
Some characters cannot be typed directly inside the quotes, so C uses escape sequences: a backslash followed by a letter. Each escape sequence is one character.
| Write | Prints |
|---|---|
\n | a new line |
\t | a tab (jump to the next tab position) |
\\ | one backslash \ |
\" | one double quote " |
Statements run top to bottom
Inside main, statements run one after another, in the order they are written. The output of several printf statements is simply joined end to end.
Comments
A comment is a note for humans; the compiler throws it away. Two forms:
/* … */can span several lines. It cannot be nested: the first*/ends it.// …lasts to the end of the line.
Inside a string literal, /* and // are just ordinary characters and get printed.
4 · Visual
From the text you write to the program that runs. You compile once, then run the executable as many times as you like.
#include <stdio.h> <- toolbox that contains printf
int main(void) <- running starts here; main hands back an int
{ <- body of main begins
printf("Hello, GATE!\n"); <- statement 1: print these characters
return 0; <- statement 2: finish, hand back 0
} <- body of main ends
Screen after running:
+---------------------------+
| Hello, GATE! |
| _ | <- \n moved the cursor to a new line
+---------------------------+
Anatomy of the first program, and what the screen holds after it runs.
5 · Worked Examples
Example 1 Easy
What does this print?
printf("Good");
printf("morning\n");
printf("GATE\n");
Solution
Statement 1 prints Good and stays on the same line. Statement 2 adds morning, then \n moves to a new line. Statement 3 prints GATE and a new line.
Output
Goodmorning
GATE
Example 2 Medium
What does this print?
printf("A\tB\nC\\D\n");
printf("\"Hi\"\n");
Solution
Read escape sequences one at a time: A, tab, B, new line, C, one backslash, D, new line. The second statement prints a quote, Hi, a quote, new line.
Output
A B
C\D
"Hi"
Example 3 GATE
What does this print?
printf("1"); /* printf("2"); */ printf("3");
// printf("4");
printf("/*5*/\n");
Solution
Line 1: printf("1") runs; /* printf("2"); */ is a comment and vanishes; printf("3") runs. Line 2 is a // comment. Line 3: the /*5*/ is inside a string literal, so it is printed as ordinary text.
Output
13/*5*/
6 · GATE Traps
⚠️
printfnever adds a new line by itself:printf("a"); printf("b");printsab.- Each escape sequence is one character:
"A\tB\n"holds 4 characters, not 6. - Comments inside a string literal are not comments; they are printed.
/* /* */ */is not a nested comment: the first*/closes it and the last*/is an error.- C is case-sensitive:
MainorPRINTFare different names, notmainandprintf. - A missing
;is a compile-time error: nothing runs at all, not even the statements before it.
8 · Memory Hook
🧠
“Write it, compile it, run it.” The compiler is a translator at a passport office: it checks your form (rules of C) and stamps out a version the processor can read. printf is a typist who types exactly what you dictate, never pressing Enter unless you say \n.
9 · Connect the Dots
This lesson gave the outline every program shares: #include, main, statements, return 0. So far the program can only print fixed text. To work with numbers that change, such as a ticket fare or a count, a program needs somewhere to keep them. Next lesson: where does a program keep a number while it runs?
10 · Self-Test
GATE format. Try every question before opening the vault.
Q1MCQ1 markOutput
What does this print?
printf("a\nb");
printf("c\n");
- (A)
aon line 1,bcon line 2 - (B)
a b con one line - (C)
abcon one line - (D)
a,b,con three lines
Q2MCQ1 markCompiler
A program has printf("Hi") with no semicolon after it, followed by a correct printf("Bye\n");. What happens?
- (A) It prints Bye only
- (B) It prints HiBye
- (C) The compiler reports an error and no executable is produced
- (D) It prints Hi, then stops with an error
Q3NAT1 markEscape sequences
How many characters does printf("A\tB\n"); print (count the tab and the new line as characters)?
Numerical answer: ________
Q4MSQ1 markBasics
Which statements are true?
- (A) The compiler turns source code into an executable.
- (B)
printfadds a new line after every call. - (C) Every C program starts running inside
main. - (D) Comments are removed by the compiler and never run.
Q5MCQ2 marksOutput
What does this print?
printf("x"); // printf("y");
printf("z /* w */\n"); /* printf("v"); */
- (A)
xyz /* w */ - (B)
xz - (C)
xz v - (D)
xz /* w */
11 · Rule Card
- Program = list of instructions; processor runs machine code (0s and 1s).
- Source code (.c) → compiler → executable. Rule broken → compile-time error, nothing runs.
- Shape:
#include <stdio.h>·int main(void) { … return 0; }. - Statements run top to bottom; each ends with
;. printf("…")prints exactly the string literal; it never adds a new line.- Escapes (one character each):
\nnew line,\ttab,\\backslash,\"quote. - Comments:
/* … */(not nestable) and// …; inside quotes they are plain text.
12 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
programinstructionprocessormachine codesource codecompilerexecutablecompile-time error#include <stdio.h>functionmainint (return type of main)statementsemicolonprintfstring literalescape sequence \n \t \ "return 0comment
13 · Answer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Self-Test
Q1 Answer: (A)1 mark
After a comes a new line; b and c share line 2 because nothing moves to a new line between them.
Output
a
bc
- (B) is wrong: printf never inserts spaces.
- (C) is wrong: The \n after a starts a new line.
- (D) is wrong: There is no \n between b and c.
Re-read: Lesson 0.1.1 — Programs and instructions; source code → compiler → executable; the shape of a first C program (main, printf, return)
Q2 Answer: (C)1 mark
A missing semicolon breaks the rules of C, so it is a compile-time error. Nothing runs at all.
- (A) is wrong: Nothing runs: the program never compiles.
- (B) is wrong: The compiler stops before any running happens.
- (D) is wrong: Nothing runs: the program never compiles.
Re-read: Lesson 0.1.1 — Programs and instructions; source code → compiler → executable; the shape of a first C program (main, printf, return)
Q3 Answer: 41 mark
A, tab, B, new line: 4. Each escape sequence is a single character.
Re-read: Lesson 0.1.1 — Programs and instructions; source code → compiler → executable; the shape of a first C program (main, printf, return)
Q4 Answer: (A) (C) (D)1 mark
printf prints exactly the characters given.
- (B) is wrong: printf adds nothing; only \n moves to a new line.
Re-read: Lesson 0.1.1 — Programs and instructions; source code → compiler → executable; the shape of a first C program (main, printf, return)
Q5 Answer: (D)2 marks
// printf("y"); is a comment, so y never prints. /* w */ sits inside the string literal, so it prints. /* printf("v"); */ is a comment.
Output
xz /* w */
- (A) is wrong: printf("y") is inside a // comment.
- (B) is wrong: The /* w */ inside quotes is printed.
- (C) is wrong: printf("v") is inside a /* */ comment.
Re-read: Lesson 0.1.1 — Programs and instructions; source code → compiler → executable; the shape of a first C program (main, printf, return)