C Syntax
October 2, 2026 • 5 min read

Table of contents
Part of the series:C Explained
Syntax is the shape of the language: what you are allowed to write and where. C’s syntax is tiny compared to most languages, and this post shows the pieces you will see in every file: comments, statements, blocks, variables, constants, and how to print text and read input. Conditionals and loops have their own posts later, so they are intentionally not covered here.
Comments
Comments are notes for humans; the compiler ignores them entirely. There are two forms:
// A single-line comment, from the // to the end of the line.
/*
A multi-line comment.
Everything between /* and */ is ignored.
*/
Use them to explain why the code does something. Code says what it does; a comment should say why.
Statements and blocks
A statement is one instruction, and it always ends with a semicolon:
int x = 5;
printf("hi\n");
A block is a group of statements wrapped in braces { ... }. Blocks group code, and they also define where variables are visible. Every main function body is a block:
int main(void) {
int x = 5;
printf("hi\n");
return 0;
}
Braces always come in pairs, and semicolons end statements, not braces. Forgetting a semicolon is the most common compile error a beginner hits.
Identifiers
An identifier is a name you invent: variable names, function names. Rules are simple:
- Letters, digits, and the underscore
_are allowed. - Must start with a letter or
_, not a digit. - No spaces, no dashes, no punctuation.
- Case matters:
count,Count, andCOUNTare three different names.
int studentCount = 3; // valid
int 2ndPlace = 1; // INVALID: starts with a digit
int total_amount = 10; // valid
Some words are reserved by the language and cannot be used as names, like int, return, if, and while. They are the keywords of the language, and your editor will color them differently.
Variables
A variable is a named box in memory that holds a value. You must declare it before using it. A declaration states the type and the name:
int age; // declares an int named age
double price; // declares a double named price
int a, b, c; // declares three ints in one line
You can give it a value at declaration time. That is called initialization:
int age = 30;
double price = 19.99;
Or assign later with the = operator. Assigning is an action, so it is a statement and needs its semicolon:
age = 31;
The rule is simple: declare before you use, initialize before you read. An uninitialized variable holds whatever garbage was already in memory, and reading it is a bug.
Constants
A constant is a value that never changes. The const keyword marks a variable as read-only after initialization:
const int maxUsers = 100;
maxUsers = 200; // compile error: cannot assign to a const
The language also has a preprocessor directive, #define, that names a value, though const is the safer modern choice. You will see both in real code:
#define MAX 100
The preprocessor replaces every MAX in the file with 100 before the compiler runs. Note that #define lines do not end with a semicolon.
Printing and reading input
Printing uses printf, and the % symbols inside the string are replaced by the values you pass. Each %? is a format specifier describing how to print one value.
#include <stdio.h>
int main(void) {
int age = 30;
double grade = 9.5;
printf("I am %d years old.\n", age);
printf("My grade is %.2f.\n", grade);
printf("age=%d grade=%.2f\n", age, grade);
return 0;
}
The common specifiers: %d prints an int, %f prints a double (and %.2f prints it with two decimal places), %c prints a single char, %s prints a string, and %p prints an address (used later with pointers). The \n inside a string is an escape sequence, a way to write a character that is hard to type directly. The ones you will use constantly:
\n newline \t tab
\" double quote \\ backslash
% percent \0 null terminator (used with strings)
To print a literal percent sign, write %%.
Reading input uses scanf, the mirror image of printf. Give it a specifier and the address of the variable to fill in. The & operator means “the address of”; you will understand it deeply in the pointers post, but for now know that scanf("%d", &age) stores what the user typed into age:
#include <stdio.h>
int main(void) {
int age;
printf("How old are you? ");
scanf("%d", &age);
printf("You are %d years old. Good to know!\n", age);
return 0;
}
The risky part of scanf: string input with %s does not limit length and can overflow the buffer, so prefer fgets for reading text. Numbers via %d and %f are fine for learning.
Include and define live outside the code
Lines starting with # are preprocessor directives1, not C statements. #include <stdio.h> copies the contents of the standard header into your file so the compiler knows the names of functions like printf. Every program that prints or reads needs that one line at the top.
The big picture
Every C file is built from the same alphabet: comments to explain, statements ended with ;, blocks wrapped in {}, names that follow simple rules, variables you declare and initialize, and printf/scanf to talk to the user. That is the grammar you will type hundreds of times. The next post fills in the kinds of values those variables can hold: the data types.
Footnotes
/sponsor
Enjoyed this post? You can sponsor me and this site.