10
/*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?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
bakra
  • 387
  • 4
  • 15
  • Most likely, the intention is to make the resulting program print something; generating a compiler message will not probably suffice. – Ian Aug 20 '10 at 11:49
  • 3
    There have been some similar questions just recently involving how-do-I-make-this-basic-syntax-do-something-it's-not-meant-to-do. Has the obfuscated C contest recently died, so a load of judges are now seeking alternative employment? –  Aug 20 '10 at 12:51
  • 2
    The absurdity of the _interview_ questions on here never ceases to amaze me. – nategoose Aug 20 '10 at 18:07

10 Answers10

20

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
  • 74,820
  • 18
  • 121
  • 166
19

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
  • 36,141
  • 15
  • 83
  • 142
  • 3
    Not that it's relevant, but I don't see how that proves the competence of the applicant. – Tamás Szelei Aug 20 '10 at 11:41
  • 5
    Maybe successful applicants will be required to use this technique in production – Tim Robinson Aug 20 '10 at 11:42
  • 1
    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 Aug 20 '10 at 14:53
5
#include <stdio.h>
#define exit(c) return puts("foobar"),0

over main

user411313
  • 3,930
  • 19
  • 16
3

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
  • 108,024
  • 16
  • 131
  • 187
  • 1
    because "it works on my compiler" doesn't necessarily mean "it works"? –  Aug 20 '10 at 12:47
  • @Steve314: I think I made it clear that there is no conforming way of doing it (note the part about UB). – dirkgently Aug 20 '10 at 13:51
0

If you interpreted the question to mean you could not or were not allowed to edit the file by commenting out /* */ or using #ifdef _COMMENT_ME_OUT__ #endif respectively above and below the section you are not allowed to edit and then introducing a new main, then you should give an answer of using another .c file.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
0
#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
  • 5,575
  • 6
  • 41
  • 69
0

If you cannot find a workaround to edit that file, then use a different c file.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Douglas
  • 36,802
  • 9
  • 76
  • 89
  • 1
    nah..he got pissed when I said that – bakra Aug 20 '10 at 11:48
  • 4
    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 Aug 20 '10 at 11:49
  • 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 Aug 20 '10 at 12:10
0

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.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Ian
  • 4,421
  • 1
  • 20
  • 17
0
#define exit(x) (printf("Bye"))
int main(int argc,char* argv)
{
    exit(0);
    getchar();
    return 0;
}
0

Solution 1.

This works without any preprocessor directives in cl and gcc, although I've not tested to make sure I'm not using any extensions:

#include <stdio.h>
static void exit() {
   printf("Hello world");
}
/*you cannot change anything from here below*/
main()
{
exit(0);
}
/*you cannot change anything from here up*/

I think it's valid but I can't remember if masking a standard library function is allowed or not.

Solution 2

As several other answers have specified, you can use preprocessor directives, eg:

  • #define main or exit to be something that calls ifdef
  • use #if 0 to prevent the existing code being compiled
  • using #pragma message or #error to print a message at compile time
  • using #pragma startup to use a different function as main or to run start-up code before main.

Solution 3

If your compiler supports any C++ features in addition to C, there are many answers:

  • Declare a class with a constructor and a static variable of that type
  • Put the existing "main" function into a separate namespace (or class definition) and write a different global main

Solution 4

I also looked for any way of forcing a run-time error (stack overflow, out of memory, null dereference, tc), which would normally cause the program to print something, but couldn't find any way that didn't involve running extra code, in which case the extra code might as well be printf.

Jack V.
  • 1,381
  • 6
  • 20