Getting Started
Syntax, Output & Comments
The grammar rules of C, how printf prints text and numbers, escape sequences, and how to comment code.
C syntax rules
C is strict about grammar. Five rules cover almost every beginner error:
- Every statement ends with a semicolon
;. - C is case-sensitive —
printfworks,Printfdoes not. - Blocks use curly braces
{ }. - Whitespace is ignored — indent for humans, not for the compiler.
- Text goes in double quotes
"..."; a single character goes in single quotes'A'.
Printing with printf
printf (print formatted) writes text to the screen. Text without \n stays on the same line:
#include <stdio.h>
int main(void) {
printf("Learning ");
printf("C is fun.");
printf("\n");
printf("This is on a new line.\n");
return 0;
}Learning C is fun. This is on a new line.
To print numbers, put a format specifier in the text and pass the value after it. %d prints an integer:
#include <stdio.h>
int main(void) {
int marks = 92;
printf("Marks: %d\n", marks);
printf("%d + %d = %d\n", 7, 5, 7 + 5);
return 0;
}Marks: 92 7 + 5 = 12
You will meet all the format specifiers in the next lesson.
Escape sequences
A backslash \ inside a string starts an escape sequence — a special character you cannot type directly.
| Sequence | Meaning |
|---|---|
\n |
New line |
\t |
Horizontal tab |
\\ |
A backslash |
\" |
A double quote inside a string |
\' |
A single quote |
\0 |
The null character (ends every string) |
%% |
A percent sign (inside printf only) |
#include <stdio.h>
int main(void) {
printf("Name\tMarks\n");
printf("Asha\t95\n");
printf("Ravi\t88\n");
printf("She said \"C is easy\" and scored 100%%.\n");
printf("Path: C:\\Users\\asha\n");
return 0;
}Name Marks Asha 95 Ravi 88 She said "C is easy" and scored 100%. Path: C:\Users\asha
puts and putchar
Two simpler output functions are also in stdio.h:
puts("text")prints a string and adds a newline automatically.putchar('A')prints exactly one character.
#include <stdio.h>
int main(void) {
puts("Hello from puts");
putchar('O');
putchar('K');
putchar('\n');
return 0;
}Hello from puts OK
Comments
Comments are notes for humans. The compiler ignores them completely.
// A single-line comment runs to the end of the line.
/* A multi-line comment
can span several lines. */
int total = 0; // comments can follow code tooTest yourself
4 questions · pick an answer to check it instantly
- 1
What is printed by
printf("A\tB\nC");?\tinserts a tab and\nstarts a new line, soCappears on the second line. - 2
How do you print a percent sign with
printf?Insideprintf,%starts a format specifier, so a literal percent sign is written as%%. - 3
Which statement about C is true?
C is case-sensitive, uses braces (not indentation) for blocks, and strings use double quotes. Semicolons end statements. - 4
What does
puts("Hi");do differently fromprintf("Hi");?putsalways appends\n. It cannot format numbers.