C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 9)
9.
What will be the output of the program in 16-bit platform (under DOS)?
#include<stdio.h>

int main()
{
    struct node
    {
        int data;
        struct node *link;
    };
    struct node *p, *q;
    p = (struct node *) malloc(sizeof(struct node));
    q = (struct node *) malloc(sizeof(struct node));
    printf("%d, %d\n", sizeof(p), sizeof(q));
    return 0;
}
2, 2
8, 8
5, 5
4, 4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
36 comments Page 1 of 4.

Anjali Patel said:   1 year ago
sizeof(p), sizeof(q)

Here, we are printing the size of a pointer variable, so it will be [2] in a 16-bit system, and [4] in a 32-bit system.

Because the pointer variable just stores the memory address.
(2)

John said:   1 decade ago
Thanks everyone for your explanations,

First I was confused sizeof this struck is 4, but actually this is sizeof pointer
sizeof pointer depend on system with 16/32/64 bit

16 bit : is 2 byte
32 bit : 4 byte
64 bit : 8 byte

Thanks
(1)

Mayur Gaikwad said:   10 years ago
Hello friends.

The size of pointer depends upon the system with 16/32 bit.

So for 16 bit it will take 2 bytes.

For 32 bit system it will take 4 bytes.

Thank you.
(1)

K.eswar said:   10 years ago
If the question is printf(%d,%d\n",sizeof(struct node));

The answer would be 4.

But he is asking sizeof (p) which is a pointer.

Sizeof (pointer variable) is always 2 only irrespective of whether int* or struct node*.
(1)

RAJI said:   9 years ago
Thank you so much @Chandresh.
(1)

Rupa said:   7 years ago
It gives the output as 8, 8 in Ubuntu. Which will be the correct answer.
(1)

Amandeep Singh said:   7 years ago
As per me, the answer is 8, 8.

Ashish said:   8 years ago
Yes, Agree @Hardik.

It's 8,8 for 64 bit.

Hardik chugh said:   8 years ago
For 64-bit linux os answer comes to be 8, 8.

PRITAM GORAIN said:   1 decade ago
The output of this question will be 4, 4 in gcc compiler. Since according to ANSI standard all pointers are of 4 bytes in size.


Post your comments here:

Your comments will be displayed after verification.