Can anybody explain why the "main" isn't a reserved word in C acording to SOLOLEARN? And why it print 3 the following code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can anybody explain why the "main" isn't a reserved word in C acording to SOLOLEARN? And why it print 3 the following code?

#include <stdio.h> int main() { int main =3; printf("%d", main); return 0; }

24th Jun 2019, 1:47 PM
Radu Nicolae Serb
Radu Nicolae Serb - avatar
3 Answers
+ 3
Long story short, the compiler views main() as a function (which it is), so using main as a variable isn't in conflict with it. As for your second question, int main = 3 is declaring the variable 'main' and initializes its value as 3. Printf is then printing the value of 'main' which is equal to 3, so 3 is what is being printed by that code. Hope that helps!
24th Jun 2019, 2:30 PM
AgentSmith
+ 2
Thx..... Yes if "main" isn't a reserved word, the code has to output on the screen 3, but as far as I know the C language standard doesn't allow to use "main" as a name for a variable. So my question was around the "main" used like in the code if it is acording to C standard.
24th Jun 2019, 5:00 PM
Radu Nicolae Serb
Radu Nicolae Serb - avatar
+ 2
You're welcome. That's what I was implying in the first paragraph. Main isn't a reserved keyword in C language, so you can use it as a variable name if you want.
24th Jun 2019, 5:04 PM
AgentSmith