Cy Bu
Cy Bu

Reputation: 1437

issues finding number of element in array in c

int input[] = {4, 5, 6, 7, 8};
printf("size of input is %d\n", sizeof(input));

invariably gives me 20 elements!

Any clue why please ?

Upvotes: 0

Views: 41

Answers (1)

Jayveer Parmar
Jayveer Parmar

Reputation: 500

It prints size of array and I think you want number of element in an array. In my computer, int size is 4 bytes. So, total size will be 5*4=20 bytes. If you want number of elements in array, you can write

int input[] = {4, 5, 6, 7, 8};
int n = sizeof(input) / sizeof(input[0]);
printf("number of input is %d\n",n);

Upvotes: 2

Related Questions