#guys what is special about main() and how is it different from other functions (im new to cpp)
72 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
main is the default entry point of the program, meaning it starts there (besides initializing global variables which can involve functions).
ohhh
but ive seen people start with other functions
and other people writing other functions outside of the main function
There is WinMain for Windows GUI subsystem something. That's pretty much it.
No. Usually it's last.
I think they mean first to be executed?
but its the entry point?
well yeah, so other code can be executed after it
if its last then only main would be executed no?
or does cpp not work like that
OH I see
yeh xD
When your program is
#include <iostream>
void f() {
std::cout << 1;
}
int main() {
std::cout << 2;
}
then 2 gets printed and 1 does not. That's what makes main special.
So ya, C is not like python
(Also it has implicit return 0; and you are not allowed to call it)
but then f() is useless no?
in that cpde
It mostly is. Someone else might do f(); though and then it has a use.
yess i think thats it xD but how?
Sort of
oh wow so the functions can be called like py
The top level is not executed. The only thing it does is define functions and variables.
In C, everything that is executed is in a function, so there has to be a fist function that gets called, and that is main
Sure
but main gets automatically run
Yup
Yep
There are other functions that get automatically run. Like
int g() {
f();
return 42;
}
int i = g();
calls g and then f before main, so "main gets called first" is not quite correct.
Hold on a moment, I think I know a good example
so what if i do this
#include <iostream>
std::cin >> x;
int main(x) {
std::cout<< x + "\n" << endl;
}```
this probably has errors in it but
You can't
oh dang
You can't execute statements outside of a function
wooah i see i see
int i = printf("Hello World!");
int main(){}
is a classic
thats so cool I see I see
it makes sense
but i thought i cant execute statements before main function-
Okay to clarify
The only statement you can have outside a function is variable definitions
Yes
It's honestly a dumb restriction. Originally C did not allow such things and required main to actually be the first function to call. No trickery with initializing a variable with a function call either. C++ inherited that, but because of constructors had to relax the rules. Nowadays may as well allow top level statements and get rid of main.
printf happens to return a number so it can be used to initialise global variables
wait i just realized, the piece of code you sent started with int and returned a string, does it like return 1??
Ah I see I see
int printf(const char* format, ...);
// ... and 3 more
Return value
1,2) Number of characters written if successful or a negative value if an error occurred.
printf returns a number and prints to the screen as a side effect
strlen is dumb, just use printf :4head:
🤣
@kindred pine Has your question been resolved? If so, run !solved :)
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
!solved
Message is already solved
nicee