top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the use of a semicolon (;) at the end of every program statement?

+2 votes
567 views
What is the use of a semicolon (;) at the end of every program statement?
posted Apr 1, 2016 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

The semicolon is a statement terminator in C to help the parser figure out where the statement ends since, by default, statements in C (and C++ and Java) can have any number of lines.if your question was why the C folks chose the semicolon character specifically, I'd guess it's probably because C came out around the time ALGOL, PASCAL and PL/I and they all used semicolons as either separators or terminators.
In C a statement does not necessarily mean action(like increment and decrement and so on) statement,even assigning and declaring a variable will be considered as statements.Take a look at this code

for(i=0;i<10;i++)
{
printf("%d",i);
}

see these points:
1.There is no semicolon after for,because if that's the case the body of loop is not at all executed
but a delay takes place,as continuous increment and condition check takes place till the condition fails.
2.i=0 i.e. assign i with zero,is a statement and hence end with ;
3.i<10 a relational statement so end with ;
4.i++ although a statement don't end with semicolon as it is against the syntax of for loop.
5.printf is a statement and hence end with ;
Now that's the use of ; in C.

answer Apr 1, 2016 by Shivam Kumar Pandey
+1 vote
The semicolon is a statement terminator in C to help the parser figure out where the statement ends since, by default, statements in C (and C++ and Java) can have any number of lines. 

Note that there is a subtle distinction between semicolons as terminators (as they are used in C) and semicolons as separators (as they are used in Pascal). This latter use is on the decline and if you are interested in reading more, here are a couple of references:
answer Apr 1, 2016 by Rajan Paswan
0 votes

It has to do with the parsing process and compilation of the code. A semicolon acts as a delimiter, so that the compiler knows where each statement ends, and can proceed to divide the statement into smaller elements for syntax checking.

answer Apr 1, 2016 by Manikandan J
...