0
#include <stdio.h>
int main() {
  int x[] = {22, 8, 87, 76, 45, 43, 34, 13, 51, 15};
  int count = 0;
  while (x[count] != '\0') {
    count++;
  }
  printf("%d", count);
return 0;
}

I cannot figure out why this program gives me output 12 whereas it is supposed to be 10. Where is the mistake? Is there any other way to know the number of elements?Because I have to use it further to calculate the median of the given data.

Please anyone help me clear this doubt. Thanks in advance.

5
  • Did you try sizeof(x) / sizeof(x[0]) Commented Dec 1, 2019 at 12:44
  • 1
    Note: main()is archaic. Use int main(void) Commented Dec 1, 2019 at 12:47
  • I tried it unmodfied and it outputs 10. Is the source posted identical to the one you tried? Commented Dec 1, 2019 at 12:48
  • The mistake is that the loop accesses elements past the end of x, and has undefined behaviour. That means it could legitimately do anything - print the value 12, print the value 10, reinstall your operating system, ..... Commented Dec 1, 2019 at 12:56
  • 1
    Use sizeof stackoverflow.com/questions/37538/… Commented Dec 1, 2019 at 12:59

4 Answers 4

5

A NUL is only added when initializing from a string literal.

char a1[] = "abc";               // 4 elements: 0x61, 0x62, 0x63, 0x00.
char a2[] = { 'a', 'b', 'c' };   // 3 elements: 0x61, 0x62, 0x63.
char a3[] = { 97, 98, 99 };      // 3 elements: 0x61, 0x62, 0x63.

Since your array doesn't contain '\0' (0), you read past its end until you happen to hit a 0. This is undefined behaviour.

You could add a zero:

#include <stdio.h>

int main(void) {
    int x[] = { 22, 8, 87, 76, 45, 43, 34, 13, 51, 15, 0 };
    size_t count = 0;
    while (x[count]) {
        count++;
    }

    printf("%zu\n", count);
}

That said, using a sentinel value here looks odd. One normally uses the size of the array.

#include <stdio.h>

int main(void) {
    int x[] = { 22, 8, 87, 76, 45, 43, 34, 13, 51, 15 };
    size_t count = sizeof(x) / sizeof(x[0]);
    printf("%zu\n", count);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think adding zero to the end of an int array to denote the end of the array a good idea.
@machine_1, I already said as much. I showed what was needed to make the OP's code work, and I showed what would normally be used instead. There are pros and cons to using sentinel values. They are still heavily used. It's probably not the best choice here, but there's no way to know.
@machine_1, There, a tad more explicit now.
1

An array of int is not necessarily zero terminated. Array of char sometimes is. But in any case, if you have the array itself and not just a pointer to the array, here's how you get the size of the array:

size_t const count = sizeof(x) / sizeof(*x);

Note that this will not work if you have passed the array as an argument to a function, because then the size information is lost in the function.

Comments

0

This condition x[count]!='\0' is not correct. It's only correct when you have to deal with strings, in that case '\0' stands for end of the string, otherwise, when you deal with an array, you have to carry on with you a variable to get the length of your array. You can get this size with sizeof(count)/sizeof(count[0]);

In fact x[count] can point any point of memmory, x being the origin, and count the offset, this is why you can access count[11] for exemple.

Comments

0

As you correctly pointed out yourself the array has 10 elements

int x[] = {22, 8, 87, 76, 45, 43, 34, 13, 51, 15};

and among the elements there is no sentinel value that is equal to '\0'.`.

So the loop invokes undefined behavior due to accessing the memory outside the array.

You could append to the array an element with the value equal to 0 like

int x[] = {22, 8, 87, 76, 45, 43, 34, 13, 51, 15, 0 };

In this case the loop

while (x[count] != '\0') {
    count++;
}

will count all elements except the last one. So you will need to increase the variable count after the loop that the last element also would be count.

Or you could rewrite the loop like

while ( x[count++] );

To get the number of elements in an array you can obtain its size and then divide it by the size of its element. For example

size_t count = sizeof( x ) / sizeof( x[0] );

or

size_t count = sizeof( x ) / sizeof( *x );

or even like

size_t count = sizeof( x ) / sizeof( x[1000] );

because the operator sizeof does not evaluate the used expressions.

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.