Semicolon | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Semicolon

Why the main function ' int main() ' doesn't need to put semicolon at the end?

1st Feb 2017, 12:25 AM
AmierAiman 47
AmierAiman 47 - avatar
7 Answers
+ 1
That's the c++ syntax.
1st Feb 2017, 12:32 AM
Bill
Bill - avatar
+ 1
syntax is the format required for the c++ compiler. every language has it's own syntax, some similar. In c++ when you have a block of code contained between the {} brackets you don't need to end with a semicolon with a few exceptions. You need the semicolon at the end of a class or a struct at the end of the {} brackets. You do not need a semicolon at the end of a function contained between brackets, or if statements. If the function or if statement was only 1 line and not contained between {} brackets then you would need a semicolon. quick example of what i am talking about with an if statement if (true) cout << "hello"; if (true) { cout << " world"; } no semicolon at the end of the bracket. both are syntactically correct ways to write if statements with 1 line of code. if you need more than one statement you have to use the brackets though
1st Feb 2017, 12:56 AM
Bill
Bill - avatar
+ 1
thanks
1st Feb 2017, 12:58 AM
AmierAiman 47
AmierAiman 47 - avatar
+ 1
and in reference to the question about using namespace std, that needs a semicolon because each line of code, needs a semicolon, with a few exceptions, like with groups of codes between brackets(except at the end of classes and structs and maybe some other exceptions I can't think of right now) also there is no semicolon after an include directive, like with #include <iostream> etc. that is what the language requires. it's the language syntax
1st Feb 2017, 1:01 AM
Bill
Bill - avatar
+ 1
Thanks for helping me.
1st Feb 2017, 1:01 AM
AmierAiman 47
AmierAiman 47 - avatar
0
syntax?
1st Feb 2017, 12:33 AM
AmierAiman 47
AmierAiman 47 - avatar
0
BTW what about ' using namespace std '? Why that need to put semicolon?
1st Feb 2017, 12:36 AM
AmierAiman 47
AmierAiman 47 - avatar