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.

user587389
  • 103
  • 1
  • 4
  • Did you try sizeof(x) / sizeof(x[0]) – Venkata Rathnam Dec 01 '19 at 12:44
  • 1
    Note: `main()`is archaic. Use `int main(void)` – machine_1 Dec 01 '19 at 12:47
  • I tried it unmodfied and it outputs 10. Is the source posted identical to the one you tried? – ldz Dec 01 '19 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, ..... – Peter Dec 01 '19 at 12:56
  • 1
    Use `sizeof` https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c – Sadmi Dec 01 '19 at 12:59

4 Answers4

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);
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 2
    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 Dec 01 '19 at 12:52
  • @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. – ikegami Dec 01 '19 at 12:54
  • 1
    @machine_1, There, a tad more explicit now. – ikegami Dec 01 '19 at 12:58
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.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
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.

Lahcen YAMOUN
  • 657
  • 3
  • 15
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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335