C Programming - Arrays - Discussion

Discussion Forum : Arrays - Find Output of Program (Q.No. 10)
10.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%d\n", sizeof(arr)/sizeof(arr[0]));
    return 0;
}
5
4
6
7
Answer: Option
Explanation:

The sizeof function return the given variable. Example: float a=10; sizeof(a) is 4 bytes

Step 1: float arr[] = {12.4, 2.3, 4.5, 6.7}; The variable arr is declared as an floating point array and it is initialized with the values.

Step 2: printf("%d\n", sizeof(arr)/sizeof(arr[0]));

The variable arr has 4 elements. The size of the float variable is 4 bytes.

Hence 4 elements x 4 bytes = 16 bytes

sizeof(arr[0]) is 4 bytes

Hence 16/4 is 4 bytes

Hence the output of the program is '4'.

Discussion:
4 comments Page 1 of 1.

Shubham said:   5 years ago
For Every elemets of the given array, the size is 4 bytes or not/?

Anjali said:   1 decade ago
@Madhu.

There will not be any error. when initializing an array of unknown size, the number of initializers in the initializer list determines the size of the array.

Madhu said:   1 decade ago
Here we did not declare the size of array i.e float arr[]={12.4,....} this will be an error in c lang because the syntax for declaration is:

data type variable[size]={value};

Sanjay said:   1 decade ago
Here arr[0] contain 4 byte data.

Post your comments here:

Your comments will be displayed after verification.