Why this program does not output "Hi there!" sentence without using main function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why this program does not output "Hi there!" sentence without using main function?

#include <iostream> using namespace std; void printSomething() { cout << "Hi there!"; } int main() { printSomething(); return 0; }

5th Sep 2020, 6:11 AM
Seyal Kandahari
Seyal Kandahari - avatar
5 Answers
+ 5
If your code compiles successfully(without any error), the compiler starts executing statements within basic main function. This is because, the compiler is programmed to start execution from the main function. So that, you have to call your own functions inside the main in order to get the output you except. Refer to this link to know more: https://en.cppreference.com/w/cpp/language/main_function
5th Sep 2020, 6:36 AM
Vinay_GB
Vinay_GB - avatar
+ 2
All programs in C++ first Execute the main() function and execution begins with main(). It not only for C++. Most of languages start run with main().
5th Sep 2020, 6:30 AM
Vadivelan
+ 1
Vadivelan if there is no main function in program, is it executable or not?
5th Sep 2020, 6:35 AM
Seyal Kandahari
Seyal Kandahari - avatar
5th Sep 2020, 6:37 AM
Vadivelan
+ 1
main() is used to initiate the function. So if you have many functions, the main() will be the one that controls all the other functions. It is more of the more of the "mother" function or "head" function...
5th Sep 2020, 4:20 PM
Emmanuel K
Emmanuel K - avatar