-
Hajipur, Bihar, 844101
Every programming language has a set of rules that define how instructions are written and executed. These rules are called syntax. Just like grammar in English ensures that sentences make sense, syntax in programming makes sure that your code is valid and understandable to the compiler.
In this chapter, you’ll learn how C programs are structured, how statements are written, and how different elements like variables, data types, and semicolons come together to form a complete program.
C syntax refers to the basic structure and rules that define how a valid C program is written. These rules decide how keywords, operators, and identifiers are arranged so the compiler can understand and execute the instructions correctly.
If you make a syntax error — for example, forget a semicolon or misspell a keyword — the compiler will throw an error, and your program won’t run until you fix it.
A C program generally follows a simple structure. Let’s look at an example:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Here’s how this structure works:
Preprocessor Directive:
The first line #include <stdio.h> tells the compiler to include the standard input/output library before compiling the program. This library contains commonly used functions like printf() and scanf().
Main Function:
The main() function is where the program starts execution. Every C program must have one and only one main() function.
Curly Braces {}:
These mark the beginning and end of a block of code. Anything inside the braces belongs to that block.
Statements:
Each line inside the block is a statement. In this example, printf("Hello, World!"); is a statement that prints text on the screen.
Semicolon ;:
Every statement in C must end with a semicolon. It tells the compiler where one instruction ends and the next begins.
Return Statement:return 0; indicates the program executed successfully. The value 0 is returned to the operating system.
C is a case-sensitive language. That means main, Main, and MAIN are three different identifiers.
For example:
int num = 5;
printf("%d", Num); // Error: ‘Num’ is undefined
The above code will throw an error because num and Num are not the same variable.
Comments are ignored by the compiler. They are used to explain code and make it easier to understand.
There are two types of comments:
Single-line comment:
// This is a single-line comment
Multi-line comment:
/* This is
a multi-line
comment */
Good programmers use comments to describe the purpose of code blocks, especially in long programs.
The smallest individual units in a C program are called tokens. A token can be a keyword, identifier, constant, string literal, or symbol.
C tokens are divided into six main categories:
Keywords – Reserved words like int, return, if, else, etc.
Identifiers – Names given to variables, functions, and arrays.
Constants – Fixed values like 10, 3.14, 'A'.
Strings – A sequence of characters enclosed in double quotes ("Hello").
Operators – Symbols used to perform operations (+, -, *, /).
Special Characters – Braces {}, brackets [], commas ,, semicolons ;, etc.
C has a set of reserved words that have predefined meanings. You cannot use them as variable names.
Examples include:
auto, break, case, char, const, continue, default, do,
double, else, enum, extern, float, for, goto, if,
int, long, register, return, short, signed, sizeof,
static, struct, switch, typedef, union, unsigned, void,
volatile, while
If you try to use a keyword as a variable name, you’ll get a compilation error.
Identifiers are names given to elements like variables, arrays, and functions. They help the compiler identify what’s being referred to.
Rules for naming identifiers:
Must begin with a letter or underscore (_).
Can contain letters, digits, and underscores.
Cannot contain spaces or special characters.
Are case-sensitive.
Cannot be a keyword.
Examples:
Valid identifiers: name, _age, marks1
Invalid identifiers: 1stName, total%, void
A variable is a named memory location used to store data. Each variable must have a type that defines the kind of data it can hold.
Example:
int age = 25;
float height = 5.6;
char grade = 'A';
Here:
int is used for integers.
float is used for decimal values.
char is used for single characters.
C requires you to declare variables before using them.
C uses functions from the stdio.h library for input and output.
Output using printf():
printf("My age is %d", age);
%d is a format specifier used for integers.
Input using scanf():
scanf("%d", &age);
The & symbol is used to store the input value in the variable age.
Whitespace in C (like spaces, tabs, and newlines) is mostly ignored by the compiler. However, proper indentation and spacing make code easier to read.
Example:
int main() {
int num = 10;
printf("%d", num);
return 0;
}
This is much easier to understand than writing everything in one line.
C statements are instructions that tell the computer what to do. They can be simple (like an assignment) or compound (like a set of statements inside { }).
Example:
int a = 5;
int b = 10;
int sum = a + b;
printf("%d", sum);
A block refers to a group of statements enclosed within { }. Blocks are used in loops, conditionals, and functions.
Let’s combine everything you’ve learned so far:
#include <stdio.h>
int main() {
int a, b, sum;
// Asking user for input
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
// Calculating sum
sum = a + b;
// Displaying result
printf("Sum = %d", sum);
return 0;
}
This simple program takes two numbers from the user and prints their sum.
It includes all key syntax rules: header inclusion, main function, variable declaration, input/output, and semicolons.
C syntax defines how programs are written and interpreted. It includes everything from structure and keywords to statements and data types. Once you understand the syntax well, you can easily read, write, and debug C programs.
The best way to master syntax is through practice — write small programs, experiment with variable names, and try to identify syntax errors yourself. Each mistake helps you understand the language more deeply.
What is meant by syntax in a programming language, and why is it important?
Identify the error in the following code snippet:
int Main() {
printf("Hello");
return 0;
}
Explain the role of the #include <stdio.h> statement in a C program.
Write a C statement to declare an integer variable named score and initialize it with 50.
Which of the following are valid identifiers in C: _total, 2ndValue, grade, float?
What is the difference between a single-line comment and a multi-line comment in C?
Write a small C program that takes two numbers as input and prints their difference.
Why is C considered a case-sensitive language? Give an example.
List the six types of tokens in C and give a brief example for each.
Correct the syntax error in the following code:
int a = 5
int b = 10;
sum = a + b;