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;
}
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 1 of 3.
Murthy said:
1 decade ago
What is null ?
Sunil kumar said:
1 decade ago
Can anyone explain this program ?
Bijan said:
1 decade ago
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:
1 decade ago
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
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:
1 decade ago
Here null is act as a pointer? can expalin any one please.
Krishan said:
1 decade ago
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.
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:
1 decade ago
Thanks krishan.
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.
#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:
1 decade ago
@jack. Thanks for your valuable information.
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.
So the difference is the extra \0 that's in the string.
So an extra byte is possible for string.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers