C Syntax & Structure
In this page:
#include <header.h>
return_type function_name() {
statement;
statement;
}
Semicolons
Every C statement must end with a semicolon so the compiler knows exactly where one instruction stops and the next begins; forgetting one is one of the most common beginner errors, and it often produces a confusing error on the following line instead.
Example: Semicolons
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 10
int x = 10;
// Print formatted text to the screen
printf("%d", x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Curly Braces
Curly braces { } group multiple statements into a single block, defining the body of a function, loop, or conditional. The compiler treats everything between a matching pair as one unit that executes (or doesn't) together.
Example: Curly Braces
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
{
// Print formatted text to the screen
printf("Grouped inside braces");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Tokens
A token is the smallest meaningful piece the compiler recognizes when reading your source code -- keywords, variable names, numbers, symbols like + or ;, and string literals are all individual tokens the compiler parses one at a time.
Example: Tokens
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare total and set it to 100
int total = 100;
// Print formatted text to the screen
printf("%d", total);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Case Sensitivity
C treats uppercase and lowercase letters as entirely different characters, so main must be written exactly in lowercase, and variables named total and Total are two separate, independent variables in the same program.
Example: Case Sensitivity
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare total and set it to 5
int total = 5;
// Declare Total and set it to 10
int Total = 10;
// Print formatted text to the screen
printf("%d %d", total, Total);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Indentation
Consistent indentation doesn't affect how the compiler runs your code -- C ignores whitespace -- but it's essential for you and other developers to visually trace which statements belong inside which block, especially in nested loops or conditionals.
Example: Indentation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Check whether 1
if (1) {
// Print formatted text to the screen
printf("Indentation shows nesting");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting the semicolon at the end of a statement, so the compiler reports an error on the following line.
- Writing
PrintforINTwhen C is case-sensitive and onlyprintfandintexist. - Leaving a
{without its closing}, causing an 'expected } at end of input' error.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers