Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Consider the following simple program. Give the values printed by the printf

ID: 3563559 • Letter: 1

Question

1. Consider the following simple program. Give the values printed by the

printf function.

#include <stdio.h>

int main() {

int i,j,k;

i=7; j=2; k=5;

printf("%d",k>i>j);

i=1; j=2; k=3;

printf("%d",i<j==j<k);

}

2. Give the hexadecimal representation of the integer 37

3. Give the result of exclusive-or of the two hex numbers 2a and 3b

4. Following program fragment illustrate the short-circuit behavior of logical

expressions. Show the output produced by each.

i=7;j=4;k=5;

printf("%d ",i-7 && j++ < k);

printf ("%d %d %d ", i,j,k);

5. How oating-point numbers are stored in memory can vary from machine

to machine. However, now a days most machines follow IEEE

(Institute of Electrical and Electronics Engineers) oating-point standard.

Here oating-point number is stored in a form of scientic notation,

with each number having three parts: a sign, an exponent, and

a mantissa (signicand). For single-precision format (32 bits), sign is

1 bit, the exponent is 8 bits and the mantissa is 23 bits. To store

mantissa eciently (maximize precision) it is stored starting with the

left most 1 bit. For example, if the number is .00101 in binary then it

is stored as 1:01 2??3 and not say :00101 20. Because the left most

bit stored is always 1, it is not actually stored but assumed. Thus, for

.00101 only 01 is stored for mantissa. For the exponent, value stored

is actually (exponent +127). This way we get values for the exponent

in the range -128 to 127. If exponent to be stored is -10, for example,

then the actual value stored is -10+127, which is 117.

Further, note that a xed point number in decimal is converted into a

xed point number in binary rst before storing. For example,

3.5 will be converted into 11.1 . Using all the above rules, 3.5 will be stored as 0x40500000

Give the oating-pont representation of 7.75 in hex

notation.

6. for the following program give the value printed by the printf statement:

#include <stdio.h>

int main() {

int i=1,j=3,k=2,value;

if (i>j)

if (i>k)

value=i;

else

value=k;

else

if (j>k)

value=j;

else

value=k;

printf("%d ",value);

}

2

Explanation / Answer

WHEN ALL ARITHMETIC AND LOGICAL OPERATORS ARE PRESENT IN THE GIVEN EXPRESSION, THEN ORDER (PRIORITY) IS TAKEN AS SHOW BELOW.

1. ARITHMETIC

2. RELATIONAL

3. LOGICAL

4. ASSIGNMENT ('=' operator)

1. Consider the following simple program. Give the values printed by the

printf function.

#include <stdio.h>

int main() {

int i,j,k;

i=7; j=2; k=5;

printf("%d",k>i>j); // 5>7 is false hence, 0. now 0>2 false. hence output is 0 (zero).

i=1; j=2; k=3;

printf("%d",i<j==j<k); // 1<2 = = 2<3 => 1 = = 1 which is true. hence, output is 1.

}

2. Give the hexadecimal representation of the integer 37

ANS: TO REPRESENT GIVEN INTEGER IN HEXADECIMAL FORM, FIRST WE NEED TO DIVIDE THE GIVEN INTEGER BY 16.

i.e. 37 % 16 = 2 (quotient), 5 (remainder). = (25)16

   therefore, (37)10 = (25)16   

3. Give the result of exclusive-or of the two hex numbers 2a and 3b

ANS: EX-OR PRINCIPLE: IF ANY OF THE INPUTS IS ONE, OUTPUT IS ONE, ELSE ZERO.

GIVEN,

2a = 0 0 1 0 1 0 1 0

3b = 0 0 1 1 1 0 1 1

________________

0 0 0 1 0 0 0 1 = (11)16

4. Following program fragment illustrate the short-circuit behavior of logical

expressions. Show the output produced by each.

i=7;j=4;k=5;

printf("%d ",i-7 && j++ < k); // 7 - 7 && (4 < 5) = 0 && (4<5) = 0 Since, in Logical AND logic if leftside value is zero, then right side expression will not be evaluated. hence output is zero.

printf ("%d %d %d ", i,j,k); // therefore, output is, i=7, j=4, k =5

6. for the following program give the value printed by the printf statement:

#include <stdio.h>

int main() {

int i=1,j=3,k=2,value;

if (i>j) // 1>3 condition is false

if (i>k) // nested if condition, hence will not be evaluated since condition above is false.

value=i; // expression will not be evaluated.

else

value=k; // expression is evaluated, since it is in else block. Hence, value is 2

else

if (j>k) // 3>2 contion is true.

value=j; // so that expression is evaluated. Hence, value = 3.

else // since condition is true else block will not be executed.

value=k;

printf("%d ",value);

}

output: 3