#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)

kindred pine
#

guys what is special about main() and how is it different from other functions

ive seen people put a lot of emphasis on the main function in cpp, however i cant tell how its different from other functions. im also not sure if functions in cpp have to be called like python so id like some clarity on that pls

boreal canyonBOT
#

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.

chrome ridge
#

main is the default entry point of the program, meaning it starts there (besides initializing global variables which can involve functions).

kindred pine
#

ohhh

#

but ive seen people start with other functions

#

and other people writing other functions outside of the main function

chrome ridge
#

There is WinMain for Windows GUI subsystem something. That's pretty much it.

kindred pine
#

I see I see

#

so main always has to be first?

chrome ridge
#

No. Usually it's last.

tulip notch
#

I think they mean first to be executed?

kindred pine
#

but its the entry point?

kindred pine
#

if its last then only main would be executed no?

#

or does cpp not work like that

tulip notch
#

OH I see

kindred pine
#

yeh xD

chrome ridge
#

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.

tulip notch
#

So ya, C is not like python

chrome ridge
#

(Also it has implicit return 0; and you are not allowed to call it)

kindred pine
#

in that cpde

chrome ridge
#

It mostly is. Someone else might do f(); though and then it has a use.

kindred pine
chrome ridge
#

Sort of

kindred pine
tulip notch
#

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

chrome ridge
#

Sure

kindred pine
#

but main gets automatically run

chrome ridge
#

Yup

kindred pine
#

other functions after or before main dont until called

#

am i correct?

tulip notch
#

Yep

kindred pine
#

thats so cool

#

its like the __init__ method in python

#

basically

chrome ridge
#

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.

tulip notch
#

Hold on a moment, I think I know a good example

kindred pine
#

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

tulip notch
#

You can't

kindred pine
#

oh dang

tulip notch
#

You can't execute statements outside of a function

kindred pine
#

wooah i see i see

lone fable
#
int i = printf("Hello World!");
int main(){}

is a classic

kindred pine
#

it makes sense

kindred pine
tulip notch
#

Okay to clarify

#

The only statement you can have outside a function is variable definitions

kindred pine
#

OH

#

I see I see

#

because theyd be global vars

lone fable
#

Yes

kindred pine
#

thanks so much guys

#

I appreciate it very much

chrome ridge
#

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.

lone fable
#

printf happens to return a number so it can be used to initialise global variables

kindred pine
boreal canyonBOT
chrome ridge
#

Return value
1,2) Number of characters written if successful or a negative value if an error occurred.

lone fable
#

printf returns a number and prints to the screen as a side effect

chrome ridge
#

strlen is dumb, just use printf :4head:

tulip notch
#

🤣

boreal canyonBOT
#

@kindred pine Has your question been resolved? If so, run !solved :)

kindred pine
#

thanks guys!

#

!close

boreal canyonBOT
#

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

kindred pine
#

!solved

boreal canyonBOT
kindred pine
#

nicee