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 1 of 3.

Rohan said:   7 years ago
@All.

#include<stdio.h>

int main()
{
int arr[] = {12, 13, 14, 15, 16};
printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));

/* (i) sizeof(arr) -> there are 5 elements in the arr[] , and it contains integers and size of integer is 4 bytes...
-> 5 * 4 = 20 bytes
(ii) (*arr) -> it will return 12
sizeof(*arr) -> As , 12 is an integer of 4 bytes...

(iii) sizeof(arr[0])) -> size of first element of arr[] (i.e. 4 bytes)...

*/

return 0;
}
(4)

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.

Iceberg said:   1 decade ago
*arr is same as arr[0], coz *arr is *(arr+0), So it is the size of int. Not the size of pointer. Try printing *arr, it will print the first element.

And if you make it a char array, it will print:

5,1,1

Where 5 is 1*5.

And rest 1 are size of char not the char pointers.

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.

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.

Pondey Ramu said:   2 decades ago
The array elements are 5 and the size is 4 then multiplying both we get 20 that is the size of the array and second one is size of the each array element and third one is size of the first array element that is 4 only thats why anwer 20,4,4

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;

Raghavendra bhat said:   1 decade ago
There are 5 elements in an array. Each having size of 4 bytes.

Hence ans 20, ii)*ptr = declaration size of each array element=4,

iii)Array size of arr(0) = 4 bytes.
Hence answer would be 20, 4, 4.

Thank you.

Suma said:   1 decade ago
The no of elements in the array are 5, each one size is 4 bytes.

So 4*5=20, and arr[0], *arr are same.. first element size is 4 bytes.

Haritha said:   10 years ago
Actually C language int datatype 2 bytes. So 2*5 elements then 10, and array size 2 and first element size of array is 2. So 10, 2, 2.


Post your comments here:

Your comments will be displayed after verification.