I have come across to an interesting thing. In an interview I was asked to print "Hello World" to the console. But the main function must be:
int main(void)
{
return 0;
}
it must not be modified!
I have come across to an interesting thing. In an interview I was asked to print "Hello World" to the console. But the main function must be:
int main(void)
{
return 0;
}
it must not be modified!
I tried to #define return printf("Hello World");
Worked with C++ and MinGW GCC.
Even if this is like no style ;)
class TEST{
public:
TEST(){
cout << "Hello World";
}
};
TEST test_obj; //Create an instnace to TEST Class
int main(){
return 0;
}
This works in either C or C++ (and is, of course, horribly bad style in either language):
#include <stdio.h>
#define return puts("Hello World");
int main(void) {
return 0;
}
The semicolon in the macro definition is necessary. It causes the following 0;
to be a statement expression, which does nothing. (Falling off the end of main
does an implicit return 0;
in C++, and in C starting with C99.)
the macro approach which consists in redefining return
is nice but has some drawbacks since it's not strictly compliant with the c89
standard and generates some warnings:
> gcc -std=c89 -Wall test.c
test.c: In function 'main':
test.c:7:12: warning: statement with no effect [-Wunused-value]
return 0;
^
test.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
now we could derive from this method, redefining just main
, and an unused function after that
#include "stdio.h"
#define main(x) main(x) { printf("Hello world\n"); return 0; } int foo()
int main(void)
{
return 0;
}
preprocessor output:
int main(void) { printf("Hello world\n"); return 0; } int foo()
{
return 0;
}
now the program compiles without warnings, with c89 standard, and doesn't redefine a keyword.
You can define a function before main and pass a constructor to print Hello World.
#include<bits/stdc++.h>
using namespace std;
class print{
print(){
cout << "Hello World";
}
};
print obj;
int main(){
return 0;
}
You can also use macro preprocessor (define macros).
#include<bits/stdc++.h>
using namespace std;
#define return printf("Hello World");
int main(){
return 0;
}
You can link it with a shared library and put a return printf("Hello World");
in DLLmain
under Windows or compile with option -init,DLLMain
under Linux.
You can move that functionality into initialization code. For instance, in C++ you can declare a static instance of a class, then print that message in the class constructor.
Or, you may include a library via #pragma and do that code in "loaded" event: https://stackoverflow.com/a/9759936/1964969
You can simply do that by making a class. In this code we have made a constructor in our HelloWorld class and what that constructor will do is print Hello World so whenever we make an object of class HelloWorld. The compiler will check what is written in the class HelloWorld and when it will see that there is a constructor in that class, it will check what is written in that constructor. We have written the code that will print HelloWorld. So it will make that object to print Hello World becuase of our constructor. I hope this code and explanation will help you
#include <bits/stdc++.h>
using namespace std;
class HelloWorld
{
public:
HelloWorld()
{
cout << "Hello World" << endl;
}
};
HelloWorld obj;
int main()
{
return 0;
}