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 2 of 3.
Shraddha said:
1 decade ago
Is the answer is 4, 2 or 4, 1? Can anyone explain it?
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'.
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.
If you run using Dev C++ then o/p: 8, 1.
Linux compiler o/p: 4, 1.
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.
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:
1 decade ago
@Anjaneyareddy.
If you say so for NULL then the sizeof ("") - - >sizeof ("\0").
\0->0?
If you say so for NULL then the sizeof ("") - - >sizeof ("\0").
\0->0?
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.
int main()
{
printf("%d, %d\n", sizeof(NULL), sizeof(""));
return 0;
}
I got output as 8 1.
64-bit machine.
(1)
Siya said:
9 years ago
I can't understand it please explain with an example of sizeof (NULL);.
Rahul Chauhan said:
9 years ago
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
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:
9 years ago
Thanks a lot @Rahul Chauhan.
Pranoti Patil said:
8 years ago
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.
The Size of pointer on 32- bit compiler is 4 bytes.
The Size of blank character is 1 byte.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers