Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Hard
  • Last Updated : 16 Apr, 2020

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




#include <stdio.h>
int main(void)
{
    int b = 20;
    int* y = &b;
    char n = 'A';
    char* z = &n;
    y[0] = z[0];
    printf((*y == *z) ? "True" : "False");
}


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

Answer:

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[0] to z[0], we are overwriting the value of b indirectly. This makes both the pointers equal.

Q2. What is the output of this program?




#include <stdio.h>
#include <stdlib.h>
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[0] += 1;
    }
}


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

Answer:



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[0]+=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[1] and any other location gives a junk value.

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




#include <stdio.h>
int main(void)
{
    int a = 20, b = 10;
    int *x = &a, *y = &b;
    int c = y[0], d = x[0];
    x[0] = c;
    y[0] = d;
    printf("%d %d", *x, *y);
}


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

Answer:

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 <stdio.h>
int main(void)
{
    int a = 100;
    char* b = (char*)&a;
    b[0] += 132;
    b[1] += 3;
    b[0] = (232 - 32) / 2;
    b[1] *= 0;
    printf("%d\n", a);
}


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

Answer:



A. Yes

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

   b[3]     b[2]     b[1]     b[0]
 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[0]. So, 11101000 becomes 01100100. b[1]*=0 makes all the 8 bits of b[1]=0. This gives the output of a as 100, which was same as in the beginning.

Q5. What happens to this program?




#include <stdio.h>
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[0] = y[1];
    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

Answer:

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 <stdio.h>
int main(void)
{
    int a = 100;
    char* x = (char*)&a;
    char** y = &x;
    y[0] = (char*)0x62fe14;
    printf("%x\n", x);
    printf("%x", *y);
}


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

Answer:

D. 0x62fe14
   0x62fe14

Explanation:
y[0] already contains 0x62fe14 (address of x). And *y gives the address of x, i.e., 0x62fe14. Performing y[0]=(char *)0x62fe14 replaces the value but is of no personal use. If you change anything in 0x62fe14 not only effects y[0] 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 <stdio.h>
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[0] = x[0];
    printf("%x", **z);
}


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

Answer:



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[0] = x[0] 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 <stdio.h>
int main(void)
{
    int a = (int)0b01001011;
    int* b = &a;
    int n[10] = { (int)0b010110, 17,
                  -4, -13, 19, -19,
                  10, (int)0b11110110 };
    for (int i = 0; i < 9; i++) {
        printf("%c", b[0]);
        b[0] += n[i];
    }
}


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

Answer:

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[0] contains the entire number. Adding each value of n, replaces the value in b[0], leading to print each character (because of format specifier).

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




#include <stdio.h>
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[3], b[2], b[1], b[0]);
}


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

Answer:

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[0] stores 2 bytes and b[1] 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 <stdio.h>
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[7], a[6], a[5],
           a[4], a[3], a[2],
           a[1], a[0]);
}


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

Answer:

A. 0 0 0 0 0 0 0 64

Explanation:
Size of this union is 8 bytes or 64 bits. The latest value put into the union is 100, which replaces the first position that had 10 initially. The hexadecimal value of 100 is 64, leading to this output. 7 bytes as 0 and 1 byte as 64.

Take a step-up from those “Hello World” programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.



My Personal Notes arrow_drop_up
Recommended Articles
Page :