Why doesn't the semicolon give errors, but too many of them don't?
Consider this C code:
#include <stdio.h>;
int main(void) {
puts("Hello, world!");; ;
;
return 0; ;
; ;
};
Here I put semicolons almost everywhere. Just for fun. But amazingly it worked ! I got a comma warning after enabling, but other completely wrong semicolons worked. If I forget to put a semicolon after puts
I get the following error
error: expected ';' before 'return'
Why aren't many errors and useless semicolons causing errors? In my opinion, they should be treated as syntax errors.
source to share
A single semicolon constructs the null operator . This is not only legal, but also useful in some cases, such as a while
/ loop for
that doesn't need a real body. Example:
while (*s++ = *t++)
;
C11 6.8.3 Expressions and Null Statements
The null operator (consisting only of semicolons) does nothing.
The only syntax error is the following line:
#include <stdio.h>;
source to share
;
(statement separator) is always used to indicate that a particular statement has completed. There after executing the next statement.
If you don't put the delimeter then it will consider the next statement with the current statement and execute it. And that gives a syntax error.
But in another case, when we put several labels, for example:
int a;;;;;
In this case, we have 5 operators in which int a
is the first operator, and the next four operators are null operators, which will be removed by the compiler at compile time.
See some interesting cases for this question :
int main()
{
int a=0 ;,;
return 0;
}
when we change above programj it still works:
int main()
{
int a=0 ,; /*change done*/
return 0;
}
source to share
;
is an instruction separator in C as mentioned in the above answer. Rahul's answer is completely correct, just you can see this answer to a question that asks why a statement in C ends with a semicolon. That way, when you understand why the semicolon is used, you will understand what happens when you use too many semicolons.
source to share