C Programming - Pointers - Discussion

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;
}

[A]. 2, 1
[B]. 2, 2
[C]. 4, 1
[D]. 4, 2

Answer: Option C

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.


Murthy said: (Mar 26, 2011)  
What is null ?

Sunil Kumar said: (Mar 26, 2011)  
Can anyone explain this program ?

Bijan said: (Apr 20, 2011)  
If you are not clear with output then sizeof("") is equivalent to sizeof(char) where char value = 0, so it would be 1. However, sizeof(NULL) is implementation defined. So if size(int) then 4.

Jack said: (Jul 3, 2011)  
Hi Bijan
please find the below
printf("%d",sizeof("jack"));--->5
because j-1
a-2
c-3
k-4
\o-5
similarly printf("%d",sizeof(""));--->1
because "\0" will be included at the end of the string

Purushotham said: (Sep 13, 2011)  
Here null is act as a pointer? can expalin any one please.

Krishan said: (Oct 22, 2011)  
NULL is always a pointer which points to nothing.
that's why it will always be of the size of any other pointer i.e. 4 bytes.
but what's interesting is "" is a string constant and sizeof(const string) always returns the string length.
though you can still write

char* szptr = "";
printf("%d",sizeof(szptr));

which will give an output 4. but if you write like this,

char* szptr[] = "";
printf("%d",sizeof(szptr));

which will output 1.

Rahul said: (Nov 11, 2011)  
Thanks krishan.

Surender said: (Jan 11, 2012)  
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.

Ravitheja.J said: (Jan 18, 2012)  
@jack. Thanks for your valuable information.

Vamsi said: (Nov 9, 2014)  
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.

Shraddha said: (Apr 27, 2015)  
Is the answer is 4, 2 or 4, 1? Can anyone explain it?

Ramya Reddy said: (Jun 14, 2015)  
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'.

Mahesh said: (Jun 25, 2015)  
O/P is depends on compiler:

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

Linux compiler o/p: 4, 1.

Anjaneyareddy said: (Jul 14, 2015)  
@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.

Anchal said: (Jul 20, 2015)  
@Anjaneyareddy.

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

\0->0?

Jakir said: (Aug 7, 2015)  
#include<stdio.h>

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

I got output as 8 1.

64-bit machine.

Siya said: (May 11, 2016)  
I can't understand it please explain with an example of sizeof (NULL);.

Rahul Chauhan said: (Jun 30, 2016)  
sizeof("")

Whatever we write in double quotation marks ("") it considers as the string literal. A string literal is a sequence of characters which, taken together, form a null-terminated string.
Each character occupies 1 byte in TurboC and in Linux GCC 2 bytes.

Eg. of string literal
name = "Jack";
Each string literal ends with null-character('/0')
name = "Jack/0"

// Each character occupy 1 byte so, sizeof("Jack") = 5 including null character "Jack/0";
1 byte for each character
J = 1
a = 2
c = 3
k = 4
/0 = 5

If we write ("") means it's string literal with null character("/0").
So, sizeof("") is 1 byte.
1 byte for each character. Here only 1 character it is the null character.
so,
/0 = 1

coming on sizeof(NULL)

NULL is not pointing to anything but we can store the address of any type in place of NULL value and address always occupy two bytes because it is unsigned int. unsigned int occupies 2 bytes like int occupy 2 bytes. It is vary from one platform to another.

Windows TurboC: sizeof(NULL) = 2 and sizeof(char) or sizeof('/0') or sizeof("") = 1
Linux GCC compiler: sizeof(NULL) = 4 and sizeof(char) or sizeof('/0') or sizeof("") = 2

Rahul said: (Feb 6, 2017)  
Thanks a lot @Rahul Chauhan.

Pranoti Patil said: (Jun 3, 2017)  
The Size of pointer on 16- bit compiler is 2 bytes.
The Size of pointer on 32- bit compiler is 4 bytes.
The Size of blank character is 1 byte.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.