0
#include <stdio.h>
main()
{
int num[9], i = 0, count = 0;

while (i<10)
{
    scanf("%d", &num[i]);

    if (num[i] % 2 == 0)
    {
        count++;
    }
    i++;
}

printf("we have %d  double numbers\n", count);
}

Run-Time Check Failure #2 - Stack around the variable was corrupted

What should I do?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    `num[9]` out of array – user2736738 Nov 23 '17 at 15:37
  • 3
    Functions MUST have a return type, Specially the main!! – josemartindev Nov 23 '17 at 15:38
  • so what i need to do can you write me? –  Nov 23 '17 at 15:38
  • 1
    `while (i<10)` --> `while (i<9)` – Aditi Rawat Nov 23 '17 at 15:40
  • 2
    Whenever you see this: _Run-Time Check Failure #2 - Stack around the variable was corrupted_ then it means most of the time that some array index has gone out of range, which is actually the case here (see answer below). However this error message is specific to Microsoft compilers when you compile in debug mode. And it's not guaranteed to happen when an array index goes out of range – Jabberwocky Nov 23 '17 at 15:42
  • Also check [this link](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) to see whan a `main()` can/should return. – M. Prokhorov Nov 23 '17 at 15:42
  • `num` has 9 elements indexed from 0 to 8, but you’re looping from 0 to 9 - that last loop iteration writes past the end of the array, hence the error. – John Bode Nov 23 '17 at 15:43

2 Answers2

4

Your while loop hits all values of i from 0 to 9 inclusive, but attempting to access num[9] takes you out of bounds. You'll need to reduce the while loop range:

while (i<9) {
    ...
}

In addition, you really should give your main() function a return type, since modern compilers don't tolerate it being missing:

int main()
{
    ...

    return 0;
}
frslm
  • 2,969
  • 3
  • 13
  • 26
2

The valid range of indices that can be used to access an array with N elements is [0, N - 1] or that is the same [0, N ).

Thus the condition in the while statement

while (i<10)

has to be rewritten like

while (i < 9)

The reason of the error is using "magic numbers" throughout the program. Try to use named constants instead of magic numbers, In this case it will be easy to understand what magic number is used in what part of the code.

The program can look like

#include <stdio.h>

#define N 9

int main( void )
{
    int num[N];
    unsigned int count = 0;
    unsigned int i = 0;


    while ( i < N )
    {
        scanf( "%d", &num[i] );

        if ( num[i] % 2 == 0 ) ++count;

        i++;
    }

    printf( "we have %u  double numbers\n", count);
}

Instead of the while loop it would be better to use a for-loop because the variable i is not used outside the loop.

For example

#include <stdio.h>

#define N 9

int main( void )
{
    int num[N];
    unsigned int count = 0;

    for ( unsigned int i = 0; i < N; i++ )
    {
        scanf( "%d", &num[i] );

        if ( num[i] % 2 == 0 ) ++count;
    }

    printf( "we have %u  double numbers\n", count);
}

A more correct approach of declaring indices of arrays is using the type size_t for them.

In fact the array is not used in the program. You could count even entered values without using an array.

Take into account that according to the C Standard the function main without parameters shall be declared like

int main( void )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335