-1

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!

8 Answers8

11

I tried to #define return printf("Hello World"); Worked with C++ and MinGW GCC. Even if this is like no style ;)

NeverToLow
  • 141
  • 1
  • 9
7
class TEST{
public:
    TEST(){
        cout << "Hello World";
    }
};
TEST test_obj; //Create an instnace to TEST Class

int main(){
    return 0;
}
Anand
  • 143
  • 1
  • 1
  • 5
5

NeverToLow beat me to it.

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.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 6
    I would recommend instead: `#define return puts("Hello World"); return ` The final "return" actually lets you return a value. – abelenky Feb 20 '18 at 16:57
  • 2
    That's so ugly and horrible it is *almost* beautiful ;-) – Jesper Juhl Feb 20 '18 at 17:05
  • @abelenky: That's not necessary. If you're thinking of designing the `return` macro so it's more robust and flexible and can be used safely as the code is maintained -- please stop and think about what you're doing. 8-)} – Keith Thompson Feb 20 '18 at 21:15
  • It is not a good answer because the Pre-processor will replace the macro `return` with that statement `puts("Hello World")`. so if we compile against generating intermediate file we'll find it there inside main. – Itachi Uchiwa Dec 07 '20 at 18:38
  • @ItachiUchiwa I disagree. My answer meets the requirements, which don't say anything about what any intermediate results should look like. The requirements are ugly, so an ugly solution is unavoidable. – Keith Thompson Dec 08 '20 at 19:55
4

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.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
1

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;
}
Aman Raj
  • 239
  • 3
  • 15
1

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.

Dwhitz
  • 1,250
  • 7
  • 26
  • 38
0

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

Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42
  • The question is tagged with both C, and C++. Such solution would not work in C. In addition: all `#pragma`s are compiler-specific, so it wouldn't be portable code. – Algirdas Preidžius Feb 20 '18 at 17:00
  • Yes, pragmas are non-standard extensions (this is the reason it was invented for). And no, pragmas would work even in plain C if the compiler supports it. For instance, "#pragma startup foo" will do exactly what Rasul is looking for: launching foo() on the app start. – Yury Schkatula Feb 20 '18 at 17:09
  • Sorry if I was not clear enough, I meant "_declare a static instance of a class, then print that message in the class constructor_" wouldn't work in C; and since `#pragma`s are compiler dependent, it doesn't make sense recommending one, without knowing if the target compiler/architecture supports it. – Algirdas Preidžius Feb 20 '18 at 17:18
  • Yeah, static classes wouldn't work in C, this is why I designated them as "For instance, in C++...". According to pragmas, they're employed in most compilers so the question is just to find necessary one for given compiler/OS (for instance, for the same thing, same compiler may have different pragmas in Windows and Linux). – Yury Schkatula Feb 20 '18 at 17:48
0

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;
}
Zoe
  • 27,060
  • 21
  • 118
  • 148