Srividya

Friday, November 25, 2005

nice C-puzzle

void fun(void)
{
/*Do something here so that the printf stmt in main function prints something other than 20 */
}

int main()
{
int i = 20;
fun();
printf("%d", i);
}

One Solution is...


void fun(void)
{
#define printf(a,b) printf("%d",10);
// we can also use
// #define fun() i=10
}


int main()
{
int i = 20;
fun();
printf("%d", i);
}

3 Comments:

  • 1. We could print some thing else in fun() and the use exit.

    void fun(){
    printf("GG");exit(1);
    }

    2. Well since there is no \n or \t there no mater what you print 20follows.

    void fun(){
    printf("1233432");
    }

    result is 123343220 (which is !=20)

    By Blogger manjunath, at 9:30 AM  

  • /*Do something here so that the main function prints something other than 20*/
    we should make printf in main function to print other than 20. Not the other way.

    By Blogger srividya, at 9:40 PM  

  • All the modifications should be made in fun()

    Isn't that right?

    I gave both of my answers inside fun method i suppose.

    By Blogger manjunath, at 7:45 PM  

Post a Comment

<< Home