 GeeksforGeeks App
Open App Browser
Continue

# Output of C programs | Set 66 (Accessing Memory Locations)

Q1. Is the output of this code True or False?

 `#include ``int` `main(``void``)``{``    ``int` `b = 20;``    ``int``* y = &b;``    ``char` `n = ``'A'``;``    ``char``* z = &n;``    ``y = z;``    ``printf``((*y == *z) ? ``"True"` `: ``"False"``);``}`

```A. True
B. False
C. Program would crash
D. Compilation error```

`A. True`

Explanation:
The binary form of 20 is 10100 which is stored in the memory. Since it is of type int, four bytes are allocated and 10100 is stored in the 0th position. We access elements of an array from left to right. But, in the memory, the bits of memory (not at all arrays) are accessed from right to left. The ASCII value is stored in another block of 1 byte having just a 0th position. When we equate y to z, we are overwriting the value of b indirectly. This makes both the pointers equal.

Q2. What is the output of this program?

 `#include ``#include ``int` `main(``void``)``{``    ``char` `x = ``'A'``;``    ``char``* y = (``char``*)``malloc``(``sizeof``(``char``));``    ``y = &x;``    ``for` `(``int` `i = 0; i < 26; i++) {``        ``printf``(``"%c"``, x);``        ``y += 1;``    ``}``}`

```A. SDKJNSDNSKDJNSKDVNSKJD
B. SLKFVMSLFKVSFLALKDJF
C. ABCDEFGHIJKLMNOPQRSTUVWXYZ
D. NONEOFTHEABOVE```

`C. ABCDEFGHIJKLMNOPQRSTUVWXYZ`

Explanation:
The space allotted for a character is just 1 byte or 8 bits. y is being allocated during run time. It contains the address of x. Now, we can index the memory location of x. And, any change to y reflects in x also. When y+=1 is executed, in each loop, the last bit in that 1 byte of data is being incremented by 1 like:

```Initial:
00001010 (Binary form of 'A')

In loop
00001010 + 1 = 00001011 = B
00001011 + 1 = 00001100 = C
00001100 + 1 = 00001101 = D

and goes on up till Z.
```

Note: Trying to index y and any other location gives a junk value.

Q3. What gets printed at the end of this program?

 `#include ``int` `main(``void``)``{``    ``int` `a = 20, b = 10;``    ``int` `*x = &a, *y = &b;``    ``int` `c = y, d = x;``    ``x = c;``    ``y = d;``    ``printf``(``"%d %d"``, *x, *y);``}`

```A. 20 20
B. 20 10
C. 10 10
D. 10 20```

`D. 10 20`

Explanation:

Here, just the 0th bit of a and b are being changed, leading to the change in the values.

Q4. Is the value of a at the end of the program same as the beginning?

 `#include ``int` `main(``void``)``{``    ``int` `a = 100;``    ``char``* b = (``char``*)&a;``    ``b += 132;``    ``b += 3;``    ``b = (232 - 32) / 2;``    ``b *= 0;``    ``printf``(``"%d\n"``, a);``}`

```A. Yes
B. No
C. Program would crash
D. Compilation error```

`A. Yes`

Explanation:
The binary value of 100 is 01100100, 132 is 10000100 and 3 is 00000011. Now this is what happens

```   b     b     b     b
00000000 00000000 00000000 01100100
+00000000 00000000 00000011 10000100
------------------------------------
00000000 00000000 00000011 11101000

This gives 1000.
```

Now again the values are replaced. 232-32 is 200, and this divided by 2 gives 100, which is replaced at b. So, 11101000 becomes 01100100. b*=0 makes all the 8 bits of b=0. This gives the output of a as 100, which was same as in the beginning.

Q5. What happens to this program?

 `#include ``struct` `A {``    ``int` `a;``} A1;``struct` `B {``    ``int` `b;``} B1;``int` `main(``void``)``{``    ``A1.a = 10;``    ``B1.b = 100;``    ``char``* x = (``char``*)&A1;``    ``char``* y = (``char``*)&B1;``    ``y = (``char``*)0x100;``    ``x = y;``    ``printf``(``"%d\n"``, A1.a);``    ``printf``(``"%d"``, B1.b);``}`

```A. Compilation error
B. Segmentation Fault
C. Output would be returned
D. None of the above```

`B. Segmentation Fault`

Explanation:
The program would crash because y points to 0x100, which is the address of some other program running.

Q6. What is the output of this program (Consider address of x as 62fe14)?

 `#include ``int` `main(``void``)``{``    ``int` `a = 100;``    ``char``* x = (``char``*)&a;``    ``char``** y = &x;``    ``y = (``char``*)0x62fe14;``    ``printf``(``"%x\n"``, x);``    ``printf``(``"%x"``, *y);``}`

```A. 0x0010
0x1902
B. Crash
C. 0x62fe14
0x62fe15
D. 0x62fe14
0x62fe14```

```D. 0x62fe14
0x62fe14```

Explanation:
y already contains 0x62fe14 (address of x). And *y gives the address of x, i.e., 0x62fe14. Performing y=(char *)0x62fe14 replaces the value but is of no personal use. If you change anything in 0x62fe14 not only effects y but also changes the address of a as y has the address of x, which is stored in the 0th position.

Q7. What is the output of this program?

 `#include ``struct` `A {``    ``int` `a1;``    ``struct` `B {``        ``int` `a1;``    ``} A1;``} B1;``int` `main(``void``)``{``    ``B1.a1 = 10;``    ``B1.A1.a1 = 11;``    ``int``* x = &B1.a1;``    ``int``* y = &B1.A1.a1;``    ``x = y;``    ``char``** z = (``char``**)&x;``    ``*z = x;``    ``printf``(``"%x"``, **z);``}`

```A. a
B. b
C. ab
D. No output```

`B. b`

Explanation:
Initializing x to y changes the address held of x. Basically, murdering the address of B1.a1 and replacing the value in x with the value of that in y which is 11. *z = x is of no use as it already contains the address of x. Printing the hexadecimal value of z gives b (hexadecimal of 11).

Q8. What is printed?

 `#include ``int` `main(``void``)``{``    ``int` `a = (``int``)0b01001011;``    ``int``* b = &a;``    ``int` `n = { (``int``)0b010110, 17,``                  ``-4, -13, 19, -19,``                  ``10, (``int``)0b11110110 };``    ``for` `(``int` `i = 0; i < 9; i++) {``        ``printf``(``"%c"``, b);``        ``b += n[i];``    ``}``}`

```A. Karnataka
B. karnataka
C. Kerala
D. kerala```

`A. Karnataka`

Explanation:
The integer value of 01001011 is 75, the ASCII value of ‘K’. The integer value of 010110 is 22 and that of 11110110 is -10. Since there is no exceeding of 32 bits (4 bytes), b contains the entire number. Adding each value of n, replaces the value in b, leading to print each character (because of format specifier).

Q9. How does the output look like in this program?

 `#include ``int` `main(``void``)``{``    ``// Use this as a reference:``    ``// 1011001100110011``    ``// 1111000011110011``    ``// 0111000011010011``    ``// 0011000000001001;``    ``int` `a = (``int``)0b1011001100110011111100001111001101110000110100110011000000001001;``    ``short` `int``* b = (``short` `int``*)&a;``    ``printf``(``"%d %d %d %d"``, b, b, b, b);``}`

```A. decimal value of 1011001100110011 decimal value of 1111000011110011 decimal value of 0111000011010011 decimal value of 0011000000001001
B. Junk value Junk value Junk value decimal value of 0011000000001001
C. Junk value Junk value Junk value Junk value
D. Junk value Junk value decimal value of 0111000011010011 decimal value of 0011000000001001```

`D. Junk value Junk value decimal value of 0111000011010011 decimal value of 0011000000001001`

Explanation:
Size of short int is 2 bytes which is equal to 16 bits. a takes only 32 bits out of these 64 bits. And, b stores 2 bytes and b stores 2 bytes. That is the reason you get the output with two junk values and two decimal values.

Q10. What is the output of this program?

 `#include ``union` `A {``    ``int` `a;``    ``long` `long` `int` `b;``} A1;``int` `main(``void``)``{``    ``A1.a = 10;``    ``A1.b = 100;``    ``char``* a = (``char``*)&A1;``    ``printf``(``"%x %x %x %x %x %x %x %x"``,``           ``a, a, a,``           ``a, a, a,``           ``a, a);``}`

```A. 0 0 0 0 0 0 0 64
B. 0 0 0 0 0 0 0 0
C. All junk values
D. 0 0 0 0 0 0 0 A```

`A. 0 0 0 0 0 0 0 64`