In C programming, consider the following code: main() { char a[] = "Visual C++"; char *b = "Visual C++"; printf(" %d %d", sizeof(a), sizeof(b)); printf(" %d %d", sizeof(*a), sizeof(*b)); } What values are printed by the two printf calls on a typical 32 bit system where char pointers are 4 bytes?

Difficulty: Medium

Correct Answer: 11 4 followed by 1 1

Explanation:


Introduction / Context:
This question examines the difference between an array of characters and a pointer to a string literal in C, and how the sizeof operator behaves for each. It is a classic test of understanding how strings are stored in memory and how pointers differ from arrays.


Given Data / Assumptions:

    The array a is declared as char a[] = "Visual C++"; which creates an array sized to hold the string and its terminating null character.
    The pointer b is declared as char *b = "Visual C++"; which makes b point to a string literal stored elsewhere.
    We assume a 32 bit system where the size of a char pointer is 4 bytes.
    The printf calls print sizes using the %d format.


Concept / Approach:
For an array variable, sizeof gives the total size of the array in bytes. For a pointer variable, sizeof gives the size of the pointer itself, not the length of the string it points to. For a char, sizeof(*a) and sizeof(*b) both evaluate to the size of a single char, which is 1 byte on almost all systems. The string "Visual C++" has 10 visible characters plus a terminating null, so the array has length 11.


Step-by-Step Solution:
Count the characters in "Visual C++": V i s u a l gives 6, the space makes 7, C makes 8, plus and plus make 9 and 10. Add one for the closing null terminator, giving 11 bytes in the array. Therefore sizeof(a) is 11. b is a pointer to char, so sizeof(b) is the size of a pointer, which is 4 bytes on the assumed 32 bit system. The expression *a is a char, since a is an array of char, so sizeof(*a) is 1. Similarly, *b is also a char, so sizeof(*b) is 1. The first printf therefore prints 11 4 and the second prints 1 1.


Verification / Alternative check:
You can verify this behavior by compiling similar code on a 32 bit environment and printing the sizes. The output will show the array length including the null terminator for a, the pointer size for b and the element size for both dereferenced expressions.


Why Other Options Are Wrong:
Options that use 10 for sizeof(a) ignore the null terminator that is automatically added to string literals stored in arrays.
Options with 2 for pointer size do not match the assumed 32 bit environment and are more typical of older 16 bit compilers.
Options that show 4 for sizeof(*b) misinterpret the dereferenced pointer as another pointer instead of a char.


Common Pitfalls:
A common mistake is to think that sizeof on a pointer will somehow return the length of the string it points to. In C, sizeof is purely about the type size at compile time, not about runtime length of dynamic data. Another pitfall is forgetting the terminating null character when counting characters in a string literal.


Final Answer:
The program prints 11 4 on the first line and 1 1 on the second line, so the correct option is 11 4 followed by 1 1.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion