Why don't #include <...> require a semicolon? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why don't #include <...> require a semicolon?

Programming Language: C & C++ Repeat: Why don't #include <...> require a semicolon? Possible reason?: Because like in function definition or class declaration you don't need to have semicolon after the codeblock.

5th Feb 2019, 2:11 PM
Seb TheS
Seb TheS - avatar
7 Answers
+ 6
#includes and other # are part of the preprocessor and is run before the compilation phase. For a #include, code that is going to be included is copy pasted there which has their own ;'s. Also for functions, classes and loops { } are used to mark the end instead of ; and at the end of a class a ; is required because there is another field after the } that needs to be closed.
5th Feb 2019, 3:26 PM
Dennis
Dennis - avatar
+ 7
I think that's because that part of the code isn't intended for the compiler but for the preprocessor which uses its own syntax. I only know that you can't use a semicolon after #define because that would insert semicolons everywhere in the code. #define NUM 50; would lead to every "NUM" being replaced with "50;", not "50" 😶
5th Feb 2019, 3:26 PM
Anna
Anna - avatar
+ 5
Preprocessor directives don't need semicolons.
7th Feb 2019, 5:19 AM
Sonic
Sonic - avatar
+ 4
Ipang answers for your guestions cleared something for me too.
5th Feb 2019, 4:24 PM
Seb TheS
Seb TheS - avatar
+ 3
Dennis I saw such examples for structures, I didn't know classes can be treated the same 😁 Thanks Dennis, Seb TheS , sorry, didn't mean to hijack, was just looking for clarity 👍
5th Feb 2019, 4:23 PM
Ipang
+ 2
Dennis There is another field at the end of class definition block? what field is that please? class MyClass { ... } < a field in here? >;
5th Feb 2019, 4:10 PM
Ipang
+ 2
Ipang Yea, you can specify object names after the }, seperated by ,'s, which are then constructed immediately. You can get away with anonymous classes that way. :)
5th Feb 2019, 4:17 PM
Dennis
Dennis - avatar