In: Computer Science
C++
If you tried to compile a source file that doesn't contain a main() function, would this error be detected by the preprocessor, the compiler, or the linker? Briefly justify your answer.
Hint: think about how the files that don't have main() get processed in a project with multiple files. Alternatively, try it out and look at the error message you get.
C++ program without main function
Let's check with the Code:
1.Macro Arguments
#include<stdio.h>
#define str main
int str(void)
{
printf("Hello! Me without main");
return 0;
}
Here, A macro which defines the main
# define str main
The output:
Hello! Me without main
2. Static initializer
// Program without using main function
#include <iostream>
int execute()
{
std::cout << "Int run()";
exit(EXIT_SUCCESS);
}
static int s = execute();
int main()
{
std::cout << "Int run() - never executed";
}
The Output:
Int run()
3. Start function
/* Program without using main function */
#include <stdio.h>
void _start()
{
printf("Run the program without main !");
exit(0);
}
Here, start function would facilitate run program without main function.