C syntax means the set of rules that we follow when writing a C program. Just like English has grammar rules, C has syntax rules that tell the compiler how a program should be written.
C syntax is the collection of rules used to write valid C programs. It defines how keywords, variables, operators, functions, statements, and other elements should be written.
int age = 20;
The above statement follows basic C syntax rules.
A basic C program can look like this:
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
This program contains a header, the main() function, statements,
and a return statement.
C is a case-sensitive programming language. Uppercase and lowercase letters are treated as different characters.
int age;
int Age;
Here, age and Age are different identifiers.
A statement is an instruction that tells the program to perform an action. For example:
printf("Hello");
Most ordinary C statements end with a semicolon.
The semicolon ; is used to terminate many C statements.
int age = 20;
printf("%d", age);
Forgetting a required semicolon can produce a compilation error.
Curly braces { } are used to define a block of code.
int main()
{
printf("Hello");
}
The statements between the braces belong to the main() function.
Parentheses ( ) are commonly used with functions and expressions.
main()
printf("Hello");
Function names are followed by parentheses when calling or defining functions.
Keywords are reserved words that have special meaning in C.
int
return
if
else
for
while
Keywords cannot normally be used as names for variables or functions.
Identifiers are names given to program elements such as variables and functions.
int age;
int marks;
Here, age and marks are identifiers.
Common identifier rules include:
student_name
marks1
_total
| Identifier | Example |
|---|---|
| Valid | studentName |
| Valid | marks1 |
| Valid | _total |
| Invalid | 1marks |
| Invalid | student-name |
A variable stores a value that can be used by a program. A variable declaration specifies its type and name.
int age;
A value can be assigned later:
age = 20;
A variable can be declared and initialized in the same statement.
int age = 20;
Here, age is the variable and 20 is its initial value.
A data type tells the compiler what kind of value a variable can store.
int age = 20;
float price = 10.5;
char grade = 'A';
Common basic data types include int, float,
double, and char.
Text written inside double quotation marks is a string literal.
printf("Hello World");
The text Hello World is the string passed to printf().
A single character is commonly written inside single quotation marks.
char grade = 'A';
Here, 'A' is a character constant.
Comments are used to add explanations to source code. They are not executed as program statements.
// This is a comment
/*
This is a
multi-line comment
*/
A single-line comment starts with //.
// Display the message
printf("Hello");
Everything after // on that line is treated as a comment.
Multi-line comments are enclosed between /* and */.
/*
Calculate total marks
and display the result
*/
Spaces, tabs, and line breaks can make code easier to read. In many places, C ignores extra whitespace between tokens.
int age = 20;
Good formatting makes programs easier to understand and maintain.
Indentation is used to visually organize blocks of code.
int main()
{
printf("Hello");
return 0;
}
Although indentation does not normally determine block structure in C, consistent indentation improves readability.
Operators are symbols used to perform operations on values.
int total = 10 + 20;
Here, + is the addition operator.
An expression combines values, variables, and operators to produce a value.
int result = a + b;
The expression a + b calculates a value that is assigned to
result.
A function can be called by writing its name followed by parentheses.
printf("Hello");
Here, printf() is being called with a string argument.
Conditional statements use a specific syntax.
if (age >= 18)
{
printf("Adult");
}
The condition is written inside parentheses and the block is written inside curly braces.
Loops also follow specific syntax rules.
for (int i = 1; i <= 5; i++)
{
printf("%d\n", i);
}
The loop header contains initialization, condition, and update expressions.
Some common beginner mistakes are:
#include <stdio.h>
int main()
{
int age = 20;
if (age >= 18)
{
printf("Adult\n");
}
return 0;
}
This example uses a header, function, variable, condition, braces, parentheses, semicolons, and a function call.
#include <stdio.h>
int main()
{
int marks = 80;
printf("Marks = %d\n", marks);
if (marks >= 40)
{
printf("Pass");
}
return 0;
}
This example demonstrates several important C syntax rules that you will use throughout your C programming course.
Question: Which symbol is normally used to terminate a C statement?