tags:

views:

285

answers:

8
/*you cannot change anything from here below*/
main()
{
exit(0);
}
/*you cannot change anything from here up*/

This was asked during an interview.

I was told to print something on console.

anybody?

+17  A: 

weird question...

int main(void)
{
    printf("hello");
    return 0;
}
#define main int lol
/*you cannot change anything from here below*/
main()
{
exit(0);
}
/*you cannot change anything from here up*/
tenfour
+1 for 'weird question' (and the solution)
Pavel Radzivilovsky
Not that it's relevant, but I don't see how that proves the competence of the applicant.
Tamás Szelei
Maybe successful applicants will be required to use this technique in production
Tim Robinson
Maybe it tests if the applicant is able to do printf-debugging without touching the code? If he is aware of the possibilities of the preprocessor? Or whatever else. Though a competent applicant not being aware about this could learn it within a few hours, even when he hasn't an immediate answer...
Secure
+5  A: 
#include <stdio.h>
#define exit(c) return puts("foobar"),0

over main

+3  A: 

One implementation defined way would be to use the pragma directives to print during compilation.

#pragma message "Compiling " __FILE__ "..."

Or, you could do this with some macros and a printf (but not without introducing UB in some aspect or the other) at runtime.

#define exit(x) printf("Hello, world!")
int main() {
 exit(0); 
 return 0; /* if pre-C99 */
}
dirkgently
does the macro thing work?anybody?
bakra
Why don't you try it out?
dirkgently
@dirkgently - because "it works on my compiler" doesn't necessarily mean "it works"?
Steve314
@Steve314: I think I made it clear that there is no conforming way of doing it (note the part about UB).
dirkgently
A: 
#include <stdio.h>

#pragma message("Some foobar")

#error This is an error message

int main()

{

exit(0);

}

I think the interviewer wanted to know if you're aware of the #error directive ... just my 2 cents.

celavek
the message will appear in the compiler, not the console.
tenfour
@tenfour in the compiler's output console
celavek
A: 

Use a different c file.

Douglas
nah..he got pissed when I said that
bakra
hahaha he got pissed at you for giving valid solutions to his ridiculous interview question :P this must have been a fun interview. let us know if you get / accept the job :)
tenfour
Strictly speaking, the question asks "print something to the console" - the simplest way to do that would be "echo something". Don't write code if you've already got the right tool compiled and installed on millions of machines.
Douglas
A: 

Most answers involve the #define c-preprocessor instruction to change what the program means. Most compilers also support something like

#pragma startup foo()

details depend on the compiler vendor. You can make code run BEFORE main(*) is called that way.

Ian
+15  A: 

Really surprised that nobody posted this yet:

#include <stdio.h>

#if 0

/*you cannot change anything from here below*/
main()
{
exit(0);
}
/*you cannot change anything from here up*/

#endif

int main()
{
   printf("Hello, World!");
   return 0;
}

Prints at runtime and no undefined behavior whatsoever.

Tyler McHenry
A: 
#define exit(x) (printf("Bye"))
int main(int argc,char* argv)
{
    exit(0);
    getchar();
    return 0;
}
fahad