Output of C programs | Set 55 (Shift Operators)
Last Updated :
03 Jan, 2020
Prerequisite: Shift operators
Q.1 What Is The Output Of this program?
C
#include <stdio.h>
int main()
{
unsigned int i = 0x80;
printf("%d ", i << 1);
return 0;
}
Option
a) 0
b) 256
c) 100
d) 80
ans :- b
Explanation :- We know that 0x is hexa-decimal representation of number so 80 converted in decimal is 128 binary(10000000), its left shift 1 so it is (100000000)equal to 256.
Q.2 What Is The Output Of this program?
C
#include <stdio.h>
int main()
{
unsigned int a = 0xffff;
unsigned int k = ~a;
printf("%d %d\n", a, k);
return 0;
}
Option
a) 65535 -65536
b) -65535 65535
c) 65535 65535
d) -65535 -65535
ans :- a
Explanation :- This code 0x is Hexadecimal representation of number so ffff hexadecimal is converted into Decimal 65535 and stored in a. The negation of a -65535 is stored in k. k is an unsigned integer but it has a negative value in it. When k is being printed the format specifier is %d which represents signed integer thus -65536 gets printed as value of k. If the format specifier for k had been %u the value that would have been printed is 4294901760.
Q.3 What Is The Output Of this program?
C
#include <stdio.h>
int main()
{
unsigned int a = -1;
unsigned int b = 4;
printf("%d\n", a << b);
return 0;
}
Option
a) -1
b) 16
c) 8
d) -16
ans :- d
Explanation :- Here a integer is simply left-shifted 4 bit and print so it is -16.
Q.4 What Is The Output Of this program?
C
#include <stdio.h>
int main()
{
unsigned int m = 32;
printf("%d %d", m >> 1, ~m);
return 0;
}
Option
a) 16 5
b) 64 -32
c) 16 33
d) 16 -33
ans :- d
Explanation :- This program 32 is(10000) and we know that negative number in binary system contain a one's complement so here take a complement of a number and only print this number so the negation of 32 is -33.
Q.5 What Is The Output Of this program?
C
#include <stdio.h>
int main()
{
unsigned int a = -1;
unsigned int b = 15;
printf("%d, ", a << 1);
printf("%d\n", b >> 1);
return 0;
}
Option
a) -2, 7
b) 2, 8
c) -1, 15
d) -2, 15
ans :- a
Explanation :- Here -1 binary(0001) if left shift 1 so it is -2(0010) and 15 binary(1111) is right shift 1 so it is (0111)so value 7.
Similar Reads
Output of C programs | Set 58 (operators) Prerequisite : Operators in C Q.1 What is the output of this program? CPP #include <iostream> using namespace std; int main() { printf("value is = %d", (10 ++)); return 0; } Options a) 10 b) 11 c) compile time error d) run time error ans: c Explanation : lvalue required as increment
3 min read
Output of C programs | Set 53 (Operators) Prerequisite: Operators in C 1. What will be the output of the following? CPP #include <stdio.h> int main(void) { int a; int b = 1; int x[5] = { 1, 2, 3, 4, 5 }; a = 5 * 4 + x[--b] - (9 / b); printf("%d", a); return 0; } Options: 1. 12 2. Abnormal Termination 3. 21 4. No Output The a
2 min read
Output of C programs | Set 55 (Ternary Operators) Predict the output of below programs Question 1 C #include <stdio.h> int main() { int x, a = 0; x = sizeof(a++) ? printf("Geeks for Geeks\n") : 0; printf("Value of x:%d\n", x); printf("Value of a:%d", a); return 0; } Output: Geeks for Geeks Value of x:16 Value of
4 min read
Output of C programs | Set 49 (Operators) Prerequisite : Operators in C 1. What will be the output of the following program? CPP #include <stdio.h> int main(void) { int i = 40 >> 5 << 3 >> 2 << 1; printf("%d", i); return 0; } Options: 1. 4 2. 0 3. 40 4. 1 Answer : (1) Explanation: The answer is option
3 min read
Output of C programs | Set 55 1. What will be the output of the following program? C #include <stdio.h> int main() { int a = 03489; printf("%d", a); return (0); } Options: 1. 1865 2. runtime error 3. Syntax error 4. 0 The answer is option(3). Explanation: Any integral value prefix with 0 acts as octal number but
3 min read
Output of C programs | Set 56 (While loop) Prerequisite : While loops Q.1 What is the output of this program? CPP #include <iostream> using namespace std; int main() { unsigned int x = 3; while (x-- >= 0) { printf("%d ", x); } return 0; } Option a) 3 2 1 0 b) 2 1 0 -1 c) infinite loop d) -65535 Answer : C Explanation: Here
2 min read