-1
void fun()
{
    // What goes here?
}
void main()
{
    int x = 20;
    fun();
    x = 10;
    printf("%d",x); // Should print 20.
}

This was one of my test question. I was wondering if I should use static int. Can you please help me?

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
Muthu
  • 17
  • 3
    Just so you know, the only two valid signatures for `main()` in C are `int main(void)` and `int main(int argc, char **argv)` (or equivalently, `int main(int argc, char *argv[])`). The only thing I can think of, and it's probably not what they are after (or maybe it is, but I'm just saying that because it says "in the main function"), is calling `printf("20\n");` and then `exit(0);` in `fun()`. – RastaJedi Jul 02 '16 at 04:39
  • is it necessary to print from the main method only? – Kaushal28 Jul 02 '16 at 04:41
  • 4
    You could print 20 from inside `fun()`, and then `exit()`... Still a silly question to ask in a test. – void_ptr Jul 02 '16 at 04:41
  • You need x to be global or pass x to the function – Sudip Bhattarai Jul 02 '16 at 04:42
  • @SudipBhattarai even if you do that, `x` is being set to `10` _after_ `fun()` returns and _before_ `printf()` prints. – RastaJedi Jul 02 '16 at 04:44
  • Then there's no way to do that. because the x in `x=10;` and `printf("%d",x);` refer same x. since `x=10;` apperars before `printf`. The anwer you are supposed to give to the question is: threr's no such code to make that possible. – Sudip Bhattarai Jul 02 '16 at 04:51
  • Well, if you want to be really nasty, you can put a `#define` inside `fun()` and make it define `printf()` macro which does something to its arguments and then calls `printf()` function. – void_ptr Jul 02 '16 at 04:52
  • Duplicate of http://stackoverflow.com/questions/38157191/c-program-using-user-defined – Antti Haapala -- Слава Україні Jul 02 '16 at 08:17

2 Answers2

11

I do not condone this practice, and this is a horrible idea. But technically this meets the question's criteria, sort of.

void fun()
{
// Essentially this is a function with an empty body
// And I don't care about () in a macro
// Because this is evil, regardless
#define printf(a, b) (printf)(a, b*2)
}

void main() // I know this is not a valid main() signature
{
  int x = 20;
  fun();
  x = 10;
  printf("%d", x);
}
void_ptr
  • 618
  • 5
  • 15
  • Ha. I should've known there was a bizarre way. – Matti Virkkunen Jul 02 '16 at 04:59
  • Just so OP (@Muthu) knows (I say this because I'm sure +void_ptr already knows this), this doesn't actually put any "logic" in `fun()`, since this `#define` could be anywhere (before it is used) and still achieve the same result (since preprocessing/macro expansion happens _before_ compilation, i.e., this technically only changes the "logic" in `main()`). – RastaJedi Jul 02 '16 at 10:53
1

Standard disclaimers apply.

Approach 1: Create a new x variable in an inner scope.

void fun()
{
    #define fun() { int x
    #define printf } printf
}

Approach 2: Define a second variable that changes to 10 so that x can always be 20.

void fun()
{
    #define x x=20,y
}
jxh
  • 69,070
  • 8
  • 110
  • 193