C Syntax
The last lesson compiled and ran a program. Syntax is the rulebook for writing C code, and this lesson names each part.
The full shape
Both lines print, each ending with a semicolon. The semicolon tells the compiler where each statement stops.
#include <stdio.h>
int main() {
printf("C code follows rules.\n");
printf("Each rule has one job.\n");
return 0;
}One statement, one job
One line prints and the program stops. The semicolon marks where the statement ends.
#include <stdio.h>
int main() {
printf("One job.\n");
return 0;
}A missing semicolon
The print line has no semicolon. The compiler stops and points at the next line, where it expected one.
#include <stdio.h>
int main() {
printf("Hello\n")
return 0;
}Example
#include <stdio.h>
int main() {
printf("Syntax is the rules.\n");
printf("Semicolons end statements.\n");
printf("Braces group statements.\n");
return 0;
}Key points
- A C program is a list of statements inside int main().
- A statement is one complete instruction.
- A semicolon closes each statement.
- Curly braces group statements into a block.
- The #include <stdio.h> line comes before main.
- A string is text inside double quotes.
- A \n inside a string moves the print to a new line.
Three statements run in order, each ending with a semicolon. Together they name the three rules: syntax is the rules, semicolons end statements, braces group statements.
Note
A missing semicolon stops the compile, and the compiler says where.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What ends a statement in C?