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

Saturday 20 February 2010

C Question of the Day 4

1.What is the output of the following program?
#include<stdio.h>
int main()
{
    int a=10,b=20,c=30;
    c=a==b;
    printf("c=%d",c);
    return 0;
}
Solution
c=0
== is a relational operator.if the 2 operand is equal it returns 1(true) else return false.here a is not equal to b. so a==b returns 0. and 0 is stored in c. so the result is c=0


2.What is the output of the following program?
#include<stdio.h>
int main()
{
    int a=1,b=2;
    switch(a)
    {
        case 1:
                printf("GOOD\n");
                break;
        case b:
                printf("BAD\n");
                break;
    }
    return 0;
}
Solution
Compiler Error: case label does not reduce to an integer constant
The case statement can have only constant expressions ie we cannot use variable names directly in to a case statement. so an error happen.But enumerated types can be used in case statements

3.What is the output of the following program?
#include<stdio.h>
int main()
{
    int x;
    printf("ans=%d",scanf("%d",&x)); //value 10 is given as input here
    return 0;
}
Solution
ans=1
The scanf function returns number of items successfully read.
Here 10 is given as input which should have been scanned successfully. So number of items read is 1

No comments:

Post a Comment