Sunday, February 7, 2010

Interesting Question...

Question was: Guys tell me how to print both 'if' and 'else' block in a program..

I gave following solution (But still wondering about the requirement?)


int main(void)
{
int a=2;
A:
a--;
if(a>0)
{
/* write ur code */

goto A;
}
else
{
/* write ur code */
}

return 0;
}


2 comments:

  1. might it help ....

    #include
    #include

    int main()
    {
    int a ;
    if(fork())
    {
    printf("in if \n");
    }
    else
    {
    printf("in else \n");
    }
    return 0 ;
    }

    ReplyDelete
  2. yeah..But is not this writing two programs. What I understand is above program will be divided into two process (in two different address spaces where child will be having the copy of the parent's space).
    And Fork will return 0 to child therefor, IF condition will fail for Child while it will pass for Parent. right?

    ReplyDelete