• No results found

Give the output of the following

1. int i =4, z =12;

if( i = 5|| z>50)

printf(“\nwelcome in matrix”); else

printf(“\n you may go now”);

Output: welcome in matrix

First in if i is assigned with 5 that is non zero value and assume as true now this value involve with (||) OR op. and in the case of OR operator if first condition is true it is not go for the next one and jumps out with true value this is known as ”short circuit ”. 2. int i=4,z=12; if(i = 5&& z>5) printf(“hello”); else printf(“bye”);

13. What should we do to treat the

constant 3.14 as a long double?

Output: Use 3.14l

14. What will be the output of the

following statement: printf(“% %d %d”,sizeof(3.14f), sizeof(3.14), sizeof(3.14l)); a. 4 4 4 b. 4 garbage value garbage value c. 4 8 10 d. Error Output: c

15. How floats are stored in binary

form?

Output: hello

First i is assigned with 5 that is non zero and assume true now second condition (z>5) is checked and it is true so both condition are true and finally generate the true so the statement under if is executed and message “hello” is printed.

3. int i =4,j = -1,k =0,w,x,y,z; w = i||j||k; x = i&&j&&k; y = i|| j&&k; z = i&&j||k printf(“\nw=%d x=%d y=%d z=%d”,w,x,y,z); Output: w =1 x =0 y =1 z =1

i =4 that is a non zero value means true condition.

j =-1 that is also a non zero value means true condition.

k =0 that means false condition.

4. int i =4,j =-1,k =0,y,z;

y =j+5&&j+1||k+2; z = i+5 || j+1 && k+2;

printf(“\n y =%d z =%d”,y,z);

Output: 1 1

In the first statement j+5 =4(T) and j+1 =0(F) and k+2 =2(T)

Now first (j+5 && j+1) (4 && 0) gives 0 (F) and second comparison will be (0 || k+2) (0||2) 1(T) so y =1.

In the second statement i+5 =9 and in the exp. (i+5 || j+1 && k+2) (9|| j+1&& k+2) and we know in the

are represented in IEEE format. The Ieee format for floating point storage uses a sign bit, a mantissa and an exponent for representing the power of 2. the sign bit denotes the sign of the number: a 0 represents a positive value and a 1 denotes a negative value. The mantissa is represented in binary after converting it to its normalized form. The normalized form results in a mantissa whose most significant digit is always 1. the IEEE format takes advantage by not storing this bit at all. The exponent is the integer stored in unsigned binary format after adding a positive integer bias. This ensures that the stored exponent is always positive. The value of the bias is 127 for floats and 1023 for doubles 16. int p = 8, q = 20; if(p = = 5 && q<5) printf(“Hello Matrix”); else printf(“Bye Matrix”);

Output: Bye Matrix

At first compiler execute the exp in if and there the first compare p = = 5 that gives false(0) and here && is involve in exp. so control jumps out without checking for the next condition and gives false in if. Now because there is false condition in if control jumps to the else block and printed “Bye Matrix”.

case of || op. if first condition is true then control don’t go for the next condition it jumps out with true result means 1 so z =1. 5. int i = -3, j = 3; if (!i+!j*1) printf(“Hello”); else printf(“Bye”); Output: Bye

Here first we know the priority of all used op. Here !(not) gets higher priority then *(multiplication) and then +(addition)

i = -3( T ) then !i ( ! T ) F(0) similarly !j gives F (0) and 0*1 gives 0 and now exp. will convert (0+0) 0 so condition is false and control jumps out to the else block and execute it resultant “Bye” is printed.

6. int a= 40;

if(a>40 && a<45)

printf(“a is between 40 and 45”);

else

printf(“%d”,a);

Output: 40

Here a>40 is the first compare that gives false and exp. contains && op. In the case of the && if first condition is false then control jumps out with false condition and in if condition is false so finally control jumps to the else block and print the value of a that is 40.

k = i&&j; l = i||j;

printf(“%d %d”,i,j);

Output: -1 1

Here simple print the value of i and j that don’t effect the value of i and j so the value of i and j remains same and gets printed through printf( ).

18. int j = 4,k;

k = !5 && j;

printf(“k =%d”,k);

Output: 0

Here ! gets the first priority and when solved(!5) (!T) (F). And in the case of && op. first condition is false then control don’t go for the next statement and jumps out with false condition means 0 so k =0.

19. int i = -1, j = 1,k,l;

k = !i&&j; l = !i||j;

printf(“%d %d”,i,j);

Output: -1 1

Here i and j gets printed that doesn’t effect in the above steps and remains same.

20. int x = 20, y = 40, z = 45;

if(x>y && x>z)

printf(“x is max”); else if(y>x && y>z)

printf(“y is max”); else

7. int a = 65;

printf(“\n a>=65 ? %d: %c”,a);

Output 65 8. float a= 0.7; if(a<0.7) printf(“Hello”); else printf(“Bye”); Output: Hello

9. We want to round off x, a float, to

an int

value. The correct way to do so would be: a. y=(int) (x+0.5); b. y=int (x+0.5); c. y=(int) x+0.5; d. y=(int) ((int) x+0.5); Output: a

10. Which are the three different

types of real data types available in C and what are the format specifiers used for them.

Output: float 4

bytes %f

double 8

byte %lf long double 10

byte %Lf 11. By default any real number is treated as

Output: double

12. What should we do to treat the

constant

3.14 as a float?

Output: z is max

Here first condition in if is checked (x>y && x>z) and (x>y) gives false and && op. is involved and here first condition is false control gives false result and jumps out to the next else if block and check for the condition here also first condition y>x is true and then second condition is y>z that is false and finally exp. gives false and control jumps to the else block and printed out “z is max”.

21. int i = 4,j,num;

j = (num<0?0:num*num); printf(“%d”,j);

Output: Garbage

Because num is not initialize and when condition is checked it becomes false and compiler execute the num*num and assign in j. so j is also contains garbage value.

22. int a,n=30;

a =( n>5 ? ( n<=10 ? 100 :200):500);

printf(“%d”,n);

Output: 30

Here we see that n is used to check the condition but a can change by condition but n is not change in the above program and we print out the value of n that is 30.

23. int k = 4;

( !k != 1?

Output: Use 3.14f

Output: Hello

Here ! op. encounter in the same statement two times so we solve the exp using associativity that is left to right. now clearly ( !k) is solved that gives[( !4) 0] and exp. convert as 0!=1 that is true so the first printf after ? gets executed and print out “Hello”.