C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 23)
23.
If the size of integer is 4bytes, What will be the output of the program?
#include<stdio.h>

int main()
{
    int arr[] = {12, 13, 14, 15, 16};
    printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
    return 0;
}
10, 2, 4
20, 4, 4
16, 2, 2
20, 2, 2
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
22 comments Page 2 of 3.

Neeraj said:   1 decade ago
The array has 5 elements and and size of each element is 4 bytes, so size of array is 5*4=20 bytes. So sizeof(arr)=20.

*arr is a pointer whose size is 4 bytes in LINUX, GCC(32 bit compiler) and 2 bytes in turbo c, c++(16 bit compiler).

The third one is simple.

Maya said:   1 decade ago
@nirlep:
It is given in the question. that int is of 4 bytes, so multiply by 4 and not 2

Soumya said:   1 decade ago
Thank you pondey ramu.

K SURESH said:   1 decade ago
THANK YOU PRATHYUSHA

Nirlep said:   1 decade ago
Why multiply by 4 it should be 2?

Narendra said:   1 decade ago
The array elements are 5 each of size of 4 bytes. So it is 20,
where as second one is sizeof(*arr) means i.e pointer .all pointers occupies 4 bytesin 32 bit compiler so it is 4 bytes
and last one is same.

In 16 bit compiler it is 10 2 2;

Viraj said:   1 decade ago
Output will be 10 2 2 on 16 bit compiler.

Srinivas.L said:   1 decade ago
The array elements are 5 each of size of 5 bytes. So it is 20.

Where as the second one is it will prints the address to store the int data type array so its value is 4.

And in last one is also it will prints the address of the arr[0], which is integer data type so its value is 4. So the final answer is 20 4 4.

Pradip somase said:   2 decades ago
Thank you Pondey Ramu for description.

N prathyusha said:   2 decades ago
The array elements are 5 each of size 4 bytes so it is 20.
Where as second is size of addres not the individual element so the size of int as declared is 4 so it is 4.
Then the final one is size of individual element i.e. size of first array element so it is 4.


Post your comments here:

Your comments will be displayed after verification.