main() Based Questions (C, C++, C#, JAVA)
- Can you write a C/C++/C#/JAVA program without main() ?
Its impossible to write a C/C++/C# program without main() as main() marks the entry point of execution for any program. But a program can surely be written in C/C++ without actually writing main() and the program goes like this:
#include<stdio.h>
#define crack(a,n,x,i,e,t,y) y##i##t##e
#define begin crack(u,l,p,a,n,i,m)
int begin()
{
printf("Hello guys");
}As you can see in the above program, I have not used main() anywhere but still logically the program uses main() as follows:
## is called token merging operator that merges tokens based on their position values
#define is a macro that is used to substitute left value with the right value
Thus, crack(a,n,x,i,e,t,y) stores the position of y,i,t,e i.e. 7465
Also, begin stores the value of crack(u,l,p,a,n,i,m)which in turn is substituted with the position 7465. Therefore, positions 7465 of crack(u,l,p,a,n,i,m) gives us main. So wherever we used begin in the program, it gets substituted with main(). This is a very commonly asked question in interview as it tests your concept as well as logic. A simple NO will also serve the purpose but if you show this program to them, it will surely give you and edge.
Program without main() in Java is possible as Java involves Class Loading Mechanism. Whenever we execute a Java program we specify the class name so we are telling the interpreter that class is loaded for execution and not method. static block can be used to possibly execute a java program without main(). Whenever a class is loaded on JVM, it first executes the static block and as soon as the static block ends, it searches for main() method. We can inhibit it for going to main() executing static block using System.exit(0). The program is as follows:
Class Demo
{
static
{
System.out.println("Static block");
System.exit(0);
}
}--------------------------------------------------------------------------------------------
- Does C/C++/C#/JAVA support multiple main() methods?
C/C++ has only one entry point and as the programs are executed from the IDE itself and not from a command prompt, it is not possible to have multiple main() methods in C and C++.
Java can have multiple main() methods provided they are overloaded as shown below:
class MultiMain
{
public static void main(String[] args)
{
main(1);
main('a');
}
public static void main(int i)
{
System.out.println("Integer main");
}
public static void main(char a)
{
System.out.println("Char main");
}
}
C# supports Multiple main() methods provided they are in different classes and the entry point i.e. the main() that is to be executed first is specified while executing the program. Consider the following program:
using System;
using System.Collections.Generic;
using System.Text;
namespace Multiple_Main
{
class A
{
static void Main(string[] args)
{
Console.WriteLine("Class A");
}
}
class B
{
static void Main(string[] args)
{
Console.WriteLine("Class B");
}
}
}The above program when executed normally gives error as there are multiple entry points.So it is necessary to explicitly define at the time of execution of the program, which main() is to be executed and it is done as follows
csc filename.cs /main:A // For Class A csc filename.cs /main:B // For Class B
--------------------------------------------------------------------------------------------
These were some questions that I saw frequently in Placement papers. Hope it helps others. I will continue posting answers to some Frequently Asked Questions. ALL THE BEST 👍