Machine-translated by Google. Questions are authored in English.
Top

Discussion

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;
}

 

  • A.5
  • B.4
  • C.6
  • D.7

Answer: B

Correct answer: 4

This is the standard idiom for the length of an array:

\[ \frac{\text{sizeof(arr)}}{\text{sizeof(arr[0])}} = \frac{4 \times 4}{4} = 4 \]

It works whatever the element type, because the element size divides out. It works only where the array itself is visible - inside a function that received it as a parameter, sizeof would give the size of a pointer.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.