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 3

1.What is the output of the following program?
#include<stdio.h>> 
int main()
{
    float x;
    (int)x=100;
    printf("x=%d",x);
    return 0;
}
Solution

error: lvalue required as left operand of assignment
After using any operator on operand it always return some integer value. type casting operator is also doing the same thing. so (int)x is converted in some integer value(garbage value). we cannot assign any constant value to another constant value in c.
(int)x=100==>2345332=100.
2345332 is a garbage value.
So the program results an error

2.What is the output of the following program?
#include<stdio.h>>
int main()
{
    int x;
    x=sizeof(!7.5);
    printf("x=%d",x);
    return 0;
}
Solution

x=4
! is a unary negation operator.It returns either 0 or 1.
! converts a non zero operand in to 0 and a zero operand in to a 1.
so !7.5 returns 0.since 0 is a integer number and size of integer data type is 4 bytes in a 32 bit compiler

3.What is the output of the following program?
#include<stdio.h>
int main()
{
    int x;
    x=012+0x46+20;
    printf("x=%d",x);
    return 0;
}
Solution

x=100
012 is a octal number.The decimal value of 012=1*8+2=10.
0x46 is a hexadecimal value.The decimal value of 0x46=4*16+6=70.
so the total value of x=10+70+20=100









error: lvalue required as left operand of assignment
After using any operator on operand it always return some integer value. type casting operator is also doing the same thing. so (int)x is converted in some integer value(garbage value). we cannot assign any constant value to another constant value in c.
(int)x=100==>2345332=100.
2345332 is a garbage value.
So the program results an error 

No comments:

Post a Comment