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

Wednesday, 24 February 2010

C Question of the Day -Enum

1.What is the output of the following program?
#include<stdio.h>
int main()
{
    enum measure {left=10,right,top=100,bottom};
    printf("left=%d\tright=%d\ttop=%d\tbottom=%d\n",left,right,top,bottom);
    return 0;
}

Solution
left=10 right=11 top=100 bottom=101
left is assigned to10.so next to left ie right is automatically assigned to 12.top is explicitly assigned to 100 so next to top ie bottom becomes 101

 2.What is the output of the following program?
#include<stdio.h>
int main()
{
    enum student1 {Anand=0,Ashok,Alex} Boys;
    enum student2 {skippy=0,slowvy,Ashok} Girls;
    Girls=slowvy;
    switch(Girls)
    {
        case Skippy:
            printf("Skippy\n");
            break;
        case Slowy:
            printf("Slowy\n");
            break;
        case Ashok:
            printf("Ashok\n");
            break;
        default:
            break;
    }
     return 0;
}
Solution

Compiler Error
The program is failed to compile because 'Ashok' is both in the enumeration list

3.What is the output of the following program?
#include<stdio.h>
#define FALSE 1
int main()
{
    enum Boolian{FALSE=0,TRUE};
    enum Boolian b;
    printf("False=%d\n",FALSE);
    printf("True=%d\n",TRUE);
    return 0;
}
Solution

Compiler Error
The program is failed to compile because the preprocessor will try to change the 'FALSE' to 1 in the enum statement

4.What is the output of the following program?
#include<stdio.h>
enum Boolian {FALSE=0,TRUE}b;
#define FALSE 1
int main()
{
    enum Boolian b;
    printf("False=%d\n",FALSE);
    printf("True=%d\n",TRUE);
    return 0;
}
Solution

False=1 True=1
This program will compile but the #define statement will make FALSE as 1.so FALSE and TRUE both have a value of 1

5.What is the output of the following program?
#include<stdio.h>
enum {false,true};
int main()
{
    int x=1;
    do
    {
        printf("x=%d\t",x);
        x++;
        if(x<10)
            continue;
    }while(false);
    return 0;
}
Solution
x=1

6.What is the output of the following program?
#include<stdio.h>
int main()
{
    float f=5,g=10;
    enum {i=10,j=20,k=50};
    printf("%d\n",++k);
    printf("%f\n",f<<2);
    pritf("%lf\n",f%g);
    return 0;
}
Solution

Line no 6: Error Lvalue required
Line no7: Cannot apply left shift to float
Line no8:Cannot apply mod to float
Explanation:
Enumeration constants cannot be modified, so you cannot apply ++
Bit wise operators and % operators cannot be applied on float values
7.What is the output of the following program?
#include<stdio.h>
typedef enum errorType{warning,error,exception}error;
int main()
{
    error e;
    e=1;
    printf("e=%d\n",e);
    return 0;
}
Solution
Compiler error:Multiple declaration for error
Explanation:
The name error is used in 2 places. first it is a enumeration constant with value 1.
The second one is it is a type name(due to typedef) for enum error type.The compiler cannot distinguish the meaning of error so it produce a error message

2 comments:

  1. pls what is an output for program 3 and 4

    ReplyDelete
  2. pls what is an output for program 3 and 4

    ReplyDelete