230k views
3 votes
For a C array named a , write a C statement to declare a constant int named N and set it to the calculated number of elements in the array.

User DavidA
by
5.6k points

1 Answer

3 votes

Answer:

const int N = sizeof(a)/sizeof(a[0]);

Step-by-step explanation:

Given

Programming Language: C

Name of Array: a

Required

Statement to declare constant N and calculate the number of array element

The single statement is as follows;

const int N = sizeof(a)/sizeof(a[0]);

What the statement does is that, it divides the size of the array by the size of the array data type on the system used in running the application;

Take for instance

The elements of array a is {1,2,3,7, 10} --- This is type integer

Let's assume that the size of integer datatype on my PC (for instance) is 4

That means the size of array will be 4*5 = 20

The number of array elements will be calculated by 20/4 = 5

User Karmen
by
5.6k points