0
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 ?

1
  • It's because 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)); Commented Apr 9, 2017 at 0:16

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.