+ 2

Can we create a c++ program without main function?

1st Apr 2017, 11:33 AM
Suryakala N
Suryakala N - avatar
6 Answers
+ 8
"No you cannot unless you are writing a program in a freestanding environment (embedded environment OS kernel etc) where the starting point need not be main() As per the C++ standard main() is the starting point of any program in a hosted environment." http://stackoverflow.com/questions/7050925/is-it-possible-to-write-a-program-without-using-main-function
1st Apr 2017, 11:38 AM
Maz
Maz - avatar
+ 8
#define sample main int sample() { // Your code. return 0; } will work.
1st Apr 2017, 1:35 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 4
You can, but atleast I prefer not to do it in Code::Blocks, as there the main is important. Any other function will return you a warning - Undefined Reference to WinMain()... But yes, the code may still run. And I personally prefer a code without warnings...
1st Apr 2017, 1:46 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
No. The main function(as the name suggest) is the main function. Without it its like there is no function to function. Lmao
1st Apr 2017, 11:38 AM
Ritik Pal
Ritik Pal - avatar
+ 1
You don't need this in swift, which is a mix of objectiveC and C++
1st Apr 2017, 12:00 PM
MisterMM23
0
Yes, It would raise an error or be ignored, I think
1st Apr 2017, 11:37 AM
MisterMM23