LAST UPDATED ON: SEPTEMBER 17, 2024
C Language Basic Syntax Rules
The C programming syntax specifies the rules for writing the code in the C language. In simple words, these rules inform how to form code statements in a C program:
-
How should the line of code start,
-
how it should end,
-
where to use double quotes,
-
where to use curly brackets,
-
where to use parenthesis, etc.
What is Syntax?
-
Any language, be it English or Hindi, or Spanish, has a grammar.
-
The grammar defines the rules for using the language, for example how to form a sentence, what different words mean, etc.
-
In a normal spoken language or a computer programming language, syntax means how to arrange words, characters, special characters, to make a meaningful statement, or expression, etc.
-
If you don't follow the rules properly then your code will give error.
What is Syntax error?
-
If someone says there is a syntax error in the program, that means you have not written the program correctly.
-
You might have missed some semicolon or some other general mistake in typing the code for the program.
-
Having a syntax error doesn't mean your code's logic is incorrect, it means you have written it incorrectly.
-
Once the syntax is correct, then only the code is compiled and then run.
The C Tokens
-
The smallest individual unit in the C program is known as C Token.
-
Tokens are either keywords or identifiers, constants, variables, or any other symbol which has some meaning in C language.
-
The C program can also be called a collection of various tokens.
Hence the syntax for C language defines how to use these tokens together while writing the C language code.
Let's take an example,
#include <stdio.h>
int main()
{
printf("Hello,World");
return 0;
}
In the above code, if we take the printf
statement:
printf("Hello,World");
Then the tokens in this statement are:
-
printf
,
-
(
,
-
"Hello,World"
,
-
)
-
and ;
So the C tokens are basically the building blocks of a C program.
The picture below shows token in the entire Hello World Program:
Now let's see some important syntax rules, which you must remember always while writing code in the C language.
1. Statement ends with Semicolon (;
)
-
A semicolon ;
is used to mark the end of a statement and the beginning of another statement in the C language.
-
The absence of a semicolon at the end of any statement will mislead the compiler to think that this statement is not yet finished.
-
And it will add the next consecutive statement after it, which may lead to a compilation(syntax) error.
Here is an example of code with a missing semicolon,
#include <stdio.h>
int main()
{
printf("Hello,World")
return 0;
}
error: expected ';' before 'return'
return 0;
^
command terminated with exit code 1
In the above program, we dont' have the semicolon after the printf("...")
statement, hence the compiler will think that starting from printf
up untill the semicolon after return 0
is a single statement and this will lead to compilation error.
2. Adding Comments to Code
-
Comments are plain simple text in a C program that is not compiled in the compilation process.
-
We can write comments in a program, to explain various parts of the program.
-
Although writing comments is not compulsory, but it is recommended to make your program more descriptive, and easier for others to understand.
-
Adding comments makes the code more readable.
-
There are two ways in which we can write comments.
-
Using //
: This is used to write a single-line comment.
-
Using /* */
: Anything enclosed within /*
and */
, will treated as multi-line comments.
-
You can use both these two techniques in one program.
Example of comments in C language:
Here is a simple program to show how to use comments:
/*
This is my first program.
I am very excited!
*/
#include <stdio.h>
int main()
{
// Printing Hello World
printf("Hello,World");
// printf("Useless piece of code.");
return 0;
}
As you can see in the above code, we have added comments, and have also, commented a printf()
statement, which will not get executed.
3. More Syntax rules for the C Language
This is just the beginning, as we will learn more concepts in the C language, we will learn the syntax for using all of them along with examples.
Everything has a way of writing and use when it comes to writing code, and that way is its syntax.
-
C is a case-sensitive language so all C instructions must be written in lower case letters. main is not the same as MAIN.
-
All C statements must end with a semicolon.
-
Whitespace is used in C to add blank space and tabs.
-
You do not have to worry about the indentation of the code.
-
When we write a function, its body is enclosed in curly braces, like for the main()
function. We will learn this in detail when we cover functions.
The picture below shows you the structure of the C program.
Frequently Asked Questions (FAQ)
Here are some frequently asked questions related to the C language syntax.
Q1. What do you understand by tokens in C language?
The smallest individual unit in the C program is known as C Token. Tokens are either keywords or identifiers, constants, variables, or any other symbol which has some meaning in C language. The C program can also be called a collection of various tokens.
Q2. What will happen if we forgot to use a semicolon at the end of the statement in C?
If we forgot to put the semicolon at the end of any statement in the C language then it will lead to a syntax error. In that case, an error message is issued by the compiler that a semicolon is expected.
Q3. What is a Compilation Error?
A compilation error, or compile-time error, is the error returned by the compiler if the syntax of the C program is incorrect. When you try to compile a program with some syntax mistake, then the compiler will give an error, which is called a Compile-time error.
Q4. How to add comments in C?
We can add single-line comments using the //
at the starting of the comment text, and multi-line comment by enclosing the text within /*
and */
in the C program.
Q5. Can a program run without the main()
function in C?
In the C language, the main()
function defines the starting point of execution. If a C program doesn't have a main()
function then no code statement will be executed in that program. In most cases the compiler gives an error, if it cannot find the main()
function.