C — Syntax
Statements end with semicolons
Every complete instruction in C ends with a semicolon:
int x = 5;
printf("hello\n");
x = x + 1;
Missing semicolons cause compile errors. The error message usually points to the next line — the compiler didn't know the previous statement was incomplete.
Blocks use curly braces
int main() {
// everything inside these braces is the program
printf("Hello\n");
return 0;
}
Every { must have a matching }. Indentation doesn't matter to the compiler but matters for readability.
Comments
// Single-line comment (C99 and later)
/*
* Multi-line comment
* spans several lines
*/
/* You can't nest comments /* this causes errors */ */
C89 only supports /* */. C99 added // for single-line comments.
Preprocessor directives
Lines starting with # are processed before compilation:
#include <stdio.h> // include system header
#include "myheader.h" // include local header
#define PI 3.14159 // define a macro
#define MAX(a,b) ((a)>(b)?(a):(b)) // function-like macro
#ifdef DEBUG // conditional compilation
printf("Debug mode\n");
#endif
The preprocessor runs before the compiler and handles text substitution.
The main function
Every C program needs main():
int main() {
return 0;
}
int— return type (0 means success, non-zero means error)main— special name the OS looks forreturn 0— tell the OS the program succeeded
Variables and types
int age = 25; // integer
float pi = 3.14f; // single precision float
double precise = 3.14159265358979; // double precision
char letter = 'A'; // single character
unsigned int positive = 100; // non-negative integer
long big = 9000000000L; // large integer
C is statically typed — you must declare the type before using a variable.
Naming conventions
// Variables and functions — snake_case
int student_count = 50;
double calculate_average(double a, double b);
// Constants — UPPER_SNAKE_CASE
#define MAX_USERS 1000
const int TAX_RATE = 8;
// Types (typedefs and structs) — PascalCase
typedef struct Student {
char name[50];
int age;
} Student;
Constants
// Using #define (preprocessor)
#define PI 3.14159
#define MAX_SIZE 100
// Using const (type-safe)
const double TAX_RATE = 0.08;
const int MAX_ATTEMPTS = 3;
const is preferred — it's type-safe and debuggable. #define is a text substitution with no type checking.
Sizeof operator
printf("int: %zu bytes\n", sizeof(int)); // 4
printf("float: %zu bytes\n", sizeof(float)); // 4
printf("double: %zu bytes\n", sizeof(double)); // 8
printf("char: %zu bytes\n", sizeof(char)); // 1
printf("pointer: %zu bytes\n", sizeof(void*)); // 8 (on 64-bit)
sizeof tells you how many bytes a type or variable occupies in memory.
Type casting
int a = 7;
int b = 2;
// Integer division — truncates
printf("%d\n", a / b); // 3
// Float casting — preserves decimal
printf("%.1f\n", (double)a / b); // 3.5
printf("%.1f\n", (float)a / b); // 3.5
C doesn't automatically promote integers to floats. You must cast explicitly.
Compilation
gcc -Wall -std=c11 -o program source.c
-Wall— enable all warnings-std=c11— use C11 standard-o program— output executable name
Common compile errors
// Missing semicolon
printf("hello\n") // error: expected ';' before '}' token
// Undeclared variable
x = 5; // error: 'x' undeclared
// Wrong format specifier
printf("%d", 3.14); // warning: format '%d' expects argument of type 'int'
// Missing #include
strlen("hello"); // warning: implicit declaration of function 'strlen'
Read the error message carefully — it tells you the file, line number, and what went wrong.
Mini Practice
- Create variables of each basic type and print their sizes with
sizeof - Use a preprocessor
#definefor a constant and print it - Cast an integer to a float and observe the difference in division
- Write a multi-line comment above a function explaining what it does
- Compile with
-Walland fix any warnings
Next: output in C →
Related Topics
Frequently Asked Questions about Syntax
What is Syntax in C?
Syntax is a fundamental concept in C. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Syntax?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Syntax.
Why is Syntax important in C?
Syntax is essential for C development. Understanding this concept will help you write better code and solve real-world problems more effectively.