The syntax of **C** is the set of rules that specifies how the symbols and characters must be organized to create a valid program. Think of it as the "grammar" of the C language. If the syntax is incorrect, the program will result in a Compilation Error.
Let's look at the simplest C program again and break it down piece by piece to understand its syntax components:
#include <stdio.h> - This is a header file library that lets us work with input and output functions, such as printf().int main() - This is a function. Any code inside its curly brackets {} will be executed. It is the entry point of every C program.printf("Hello World!"); - This is a function used to output/print text to the screen.return 0; - This ends the main() function and returns the value 0 to the operating system, signifying that the program ran successfully.} - The closing curly bracket marks the actual end of the function.In C, the semicolon is a **Statement Terminator**. It tells the compiler that one instruction has ended and the next one is about to begin. Forgetting a semicolon is the most common mistake made by beginners.
printf("Hello");printf("Hello") (Will cause a syntax error)C is a **Case-Sensitive** language. This means that the compiler treats uppercase and lowercase letters differently. For example:
printf is a valid keyword.Printf or PRINTF will cause an error because C does not recognize them as the same function.myVar and myvar are treated as two completely different variables.Comments are used to explain the code and make it more readable. They are ignored by the compiler during execution. There are two types of comments in C:
| Type | Syntax | Usage |
|---|---|---|
| Single-line | // This is a comment |
Used for short explanations. |
| Multi-line | /* Comment here */ |
Used for long descriptions or documentation. |
Identifiers are names given to variables, functions, or arrays. They must start with a letter or an underscore (_).
Keywords are reserved words in C that have a special meaning. You cannot use these as your variable names. Examples include:
int, float, charif, else, for, whilereturn, void, staticWhile C doesn't technically require indentation to run, it is a "best practice" that professional developers follow. Proper indentation makes it clear which code belongs inside which function or loop.
{}. This makes your code look professional and easy for others (and search engine bots) to read and understand.