C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 9)
9.
What will be the output of the program if the size of pointer is 4-bytes?
#include<stdio.h>

int main()
{
    printf("%d, %d\n", sizeof(NULL), sizeof(""));
    return 0;
}
2, 1
2, 2
4, 1
4, 2
Answer: Option
Explanation:

In TurboC, the output will be 2, 1 because the size of the pointer is 2 bytes in 16-bit platform.

But in Linux, the output will be 4, 1 because the size of the pointer is 4 bytes.

This difference is due to the platform dependency of C compiler.

Discussion:
27 comments Page 2 of 3.

Siya said:   9 years ago
I can't understand it please explain with an example of sizeof (NULL);.

Jakir said:   1 decade ago
#include<stdio.h>

int main()
{
printf("%d, %d\n", sizeof(NULL), sizeof(""));
return 0;
}

I got output as 8 1.

64-bit machine.
(1)

Anchal said:   1 decade ago
@Anjaneyareddy.

If you say so for NULL then the sizeof ("") - - >sizeof ("\0").

\0->0?

Anjaneyareddy said:   1 decade ago
@Shraddha.

Here 4, 1 is the correct answer because of sizeof ("") --->sizeof ("\0").

So it will take an one bit value. So it will be print 1 and sizeof (NULL) ----> sizeof (null\0).

n->0.
u->1.
l->2.
l->3 and \0->4.

So the output will become into 4 and 1.

Mahesh said:   1 decade ago
O/P is depends on compiler:

If you run using Dev C++ then o/p: 8, 1.

Linux compiler o/p: 4, 1.

Ramya reddy said:   1 decade ago
As null is a null pointer and size of all pointers is same and is given 4 its size is 4 and sizeof "" is 1 since it is an empty string with '\0'.

Shraddha said:   1 decade ago
Is the answer is 4, 2 or 4, 1? Can anyone explain it?

Vamsi said:   1 decade ago
Here answer is 4, 2 and not 4, 1 because sizeof("") has a string inside and not a char.

So the difference is the extra \0 that's in the string.

So an extra byte is possible for string.

Ravitheja.j said:   1 decade ago
@jack. Thanks for your valuable information.

Surender said:   1 decade ago
NULL is macro which is define in stdio.h
#define NULL 0

See the Example :

#include<stdio.h>
#define surender 0

int main()
{
printf("%u",sizeof(surender));
}

Out put : Depends On Complier,
2 : in Turbo C
4 : GCC/VC++

The Displayed size is sizeof(int) actually NULL is a integer constant defined in stdio.h, having value as 0.


Post your comments here:

Your comments will be displayed after verification.