The greatest mistake you can make in life is to be continually fearing you will make one

Monday 22 February 2010

C Question of the Day 5

1.What is the output of the following program?
#include<stdio.h>
int main()
{
    int x=0,y=1;
    if(x=0)
    {
        printf("True\n");
    }
    else
    {
        printf("False\n");
    }
    return 0;
}
Solution
False
if(x=0) then x value is assigned to zero. now if(x=0) becomes if(0). The rule is if(n) becomes true when n is any positive or negative number except zero. so the condition is failed. so the else part is executed. The answer is false


2.What is the output of the following program?
#include<stdio.h>
int main()
{
    int x=1;
    x=5+5*x++;
    printf("x=%d\n",x);
    return 0;
}
Solution
x=11
When Postfix(x++) operation is used with variable in expression then the expression is evaluated first with original value then the variable is incremented.
x=5+5*1=>x=5+5=>10
now x will be incremented by one.x=10+1=>x=11


3.What is the output of the following program?
#include<stdio.h>
int main()
{
    int x=1,y;
    y=5+5*x++;
    printf("x=%d\t y=%d\n",x,y);
    return 0;
}
Solution

x=2  y=10

No comments:

Post a Comment