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;
}
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development