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

Thursday 18 February 2010

C Question of the Day 1

1.What is the output of the following program?
#include<stdio.h>
int main()
{
    int i=10,j=20;
    j=i++ + ++i;
    printf("%d %d",i,j);
    return 0;
}
Solution
As per C standard, there is no such rule to evaluate either right or left hand side of operator + on priority.Hence the statements like i+++++i should be avoided in programming


2.What is the output of the following program?
#include<stdio.h>
int main()
{
    float x;
    x=50/100;
    printf("x=%f",x);
    return 0;
}
Solution
x=0.000000
The expected output is 0.500000.because dividing an integer by an integer resulting a integer value. so 50/100 provide 0. but x is a float so the result is 0.000000
int/int=int
int/float=float
float/int=float
float/float=float



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


4. What is the output of the following program?
#include<stdio.h>
int main()
{
    int a=10,b=20,c=30;
    printf("%d%d%d");
    return 0;
}
Solution
-973480632 -973480616 0
Garbage value is printed

5.What is the output of the following program?
#include<stdio.h>
int main()
{
    int x=10;
    if(!!x)
        printf("!x=%d",!x);
    else
        printf("x=%d",x);
    return 0;
}
Solution
!x=0
! is a unary negation operator.! converts a non zero operand in to 0 and a zero operand in to a 1



!x=0

2 comments:

  1. Hi this is a great blog... Do you have similar questions for Arrays and pointers???

    ReplyDelete
  2. I am learning C using the book Let US C.. The topic on arrays and pointers in that book is a bit confusing.. are there any guides for that??

    ReplyDelete