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 ?
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);
sizeof(input)will give you the number of elements in the array multiplied by their type size, in this case 4 bytes. If you want to get the number of elements just divide by the size of the type of each element,printf("size of input is %d\n", sizeof(input)/sizeof(int));