user3070406
user3070406

Reputation: 65

keeping a variable from inside a while loop?

I'm trying to create a program that will take inputs into an array, and then print them all when input is terminated. My understanding was that when you declare a variable outside of the loop, it keeps the values, but I can't get this to work out. I know there's a way to do this somehow, but I'm drawing a blank.

#include <stdio.h>
int main(){
    int i=0;
    int n=0;
    int size=0;
    int numbers[i];
    scanf("%d", &numbers[i]);
    while ((i = 1 && numbers[i-1] != 42)){
        scanf("%d", &numbers[i]);
        i++;
        size++;
        //printf("%d",numbers[i]);
    }
    printf ("%d", sizeof(numbers));
    while ((n = 0 && n < sizeof(numbers))){
        printf("%d", numbers[i]);
        printf("\n");
        ++i;
        ++n;
    }
}

Upvotes: 0

Views: 2449

Answers (3)

Miss J.
Miss J.

Reputation: 391

3 things in your code:

  • int numbers[i]; is trying to declare a zero element array, which accounts to undefined behavior.(although there's no bound/range checking in C)
  • scanf("%d", &numbers[i]), when i>=1 where is the storage allocated for this? mostly would end up in an undefined area/ over writing an existing value.

Refer the following links for more information:

Declaring an array with 0 number of elements can still store values

Why does C not define minimum size for an array?

that said you could either declare an array of fixed size or declare the size dynamically using malloc, then loop through the elements , assign and print them.

-the while loop: evaluation and priority of operators: you could re-write your program as:

#include <stdio.h>
int main(){
    int i=0;
    int n=0;
    int size=0;
    int numbers[42];

    scanf("%d", &numbers[i++]);
    while (((numbers[i-1] != 42)))
        scanf("%d", &numbers[i++]);

    size=sizeof(numbers)/sizeof(int); /* Not necessary as array size pre-defined*/
    printf("\nsize:%d\n",size);

    while(n < size)
        printf("%d\n", numbers[n++]);
    printf("\n");
}

Note: you can change the size of the array, do keep in mind that it's an automatic variable and those array elements which haven't been explicitly initialized would be filled with junk values.

Upvotes: 1

Aadil Ahmad
Aadil Ahmad

Reputation: 139

There are a lots of mistakes in your code.They are as follow-

1.int i=0; int number[i]; which makes no sense. because you are creating an array of size 0

  1. while ((i = 1 && numbers[i-1] != 42)) every time you while loop iterates it sets the value of i to 1 and compares numbers[0]!=42 which also makes no sense.

  2. while ((n = 0 && n < sizeof(numbers))) again you are assigning n to 0 and checking if n is less than sizeof(numbers) which is always true.

Although you did not specify your problem correctly I am assuming that you want to scan number till you get 42. And after that you want to print the size of the array and the numbers too. Here is your working code.

    #include <stdio.h>
    int main(){
    int i=0;
    int n=0;
    int size=1;
    int numbers[10000];//I am assuming maximum input to be 10000
    scanf("%d", &numbers[0]);
    i=1;
    while (( numbers[i-1] != 42)){
        scanf("%d", &numbers[i]);
        i++;
        size++;
        //printf("%d",numbers[i]);
    }
    printf ("size=%d\n", size);
    while ( n < size){
        printf("%d", numbers[n]);
        printf("\n");
        //++i;
        ++n;
    }
}

Upvotes: 0

Christophe
Christophe

Reputation: 73366

Your while condition:

(i = 1 && numbers[i-1] != 42) 

has two problems:

  • i = ... actually assigns a value to i. In cas of unexpected looping, allways check if there's a =instead of an == in the condition

  • due to operator precedence, you assign 1 && to i. That's true value (i.e. 1) as long as you're in the loop, and as soon as numbers[i-1] is 42, i turns to 0 (because numbers[i-1]!=42 is false and 1 && false is false i.e. 0 ). This gives you impression that it didn't keep the value.

Edit: Of course, it's the same principle for n in the second loop ;-)

Upvotes: 1

Related Questions