C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 17)
17.
If the size of pointer is 32 bits What will be the output of the program ?
#include<stdio.h>
int main()
{
char a[] = "Visual C++";
char *b = "Visual C++";
printf("%d, %d\n", sizeof(a), sizeof(b));
printf("%d, %d", sizeof(*a), sizeof(*b));
return 0;
}
Discussion:
30 comments Page 3 of 3.
Shehnaz said:
8 years ago
Thanks for the given description.
Ziad said:
8 years ago
It's 10 with escape.
Rekha said:
8 years ago
When I compile I got,
11,8
1,1.
Anyone clear my doubt.
11,8
1,1.
Anyone clear my doubt.
(1)
Saurabh said:
7 years ago
@Rekha.
Your system must be 64 bit. Hence the size of the pointer is 64 bit. That's why it is showing 11, 8 instead of 11, 4.
Your system must be 64 bit. Hence the size of the pointer is 64 bit. That's why it is showing 11, 8 instead of 11, 4.
M.guna said:
5 years ago
While we finding the string length we didn't count null right?
Please anyone explain it.
Please anyone explain it.
(1)
Kareena said:
5 years ago
@M.Guna:
Yes, during string length, we don't count null.
But in sizeof, we count null.
Yes, during string length, we don't count null.
But in sizeof, we count null.
(1)
Jeetu said:
5 years ago
Thanks all.
Ankith said:
3 years ago
Thank you @Pranay.
(1)
Vishwajeet Jadhav said:
8 months ago
I think the given answer is wrong.
The right answer should be 11, 8.
1,1
char a[] = "Visual C++"; initializes an array a with the string "Visual C++". The size of this array is the length of the string plus one for the null terminator, so sizeof(a) will be 11.
char *b = "Visual C++"; initializes a pointer b to point to the string "Visual C++". The size of a pointer on most systems is 8 bytes, so sizeof(b) will be 8.
sizeof(*a) gives the size of the first element of the array a, which is a char and is 1 byte.
sizeof(*b) gives the size of the first element pointed to by b, which is also a char and is 1 byte.
The right answer should be 11, 8.
1,1
char a[] = "Visual C++"; initializes an array a with the string "Visual C++". The size of this array is the length of the string plus one for the null terminator, so sizeof(a) will be 11.
char *b = "Visual C++"; initializes a pointer b to point to the string "Visual C++". The size of a pointer on most systems is 8 bytes, so sizeof(b) will be 8.
sizeof(*a) gives the size of the first element of the array a, which is a char and is 1 byte.
sizeof(*b) gives the size of the first element pointed to by b, which is also a char and is 1 byte.
(1)
Aditya Verma said:
4 months ago
@Vishwajeet Jadhav.
Here it's mentioned 32 bits that means 4Byte of pointer size is correct.
Here it's mentioned 32 bits that means 4Byte of pointer size is correct.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers