1

So I don't really understand why when I use for loop to store numbers from 1 to 4 I get random numbers? When I use for loop without an Array Everything works just fine, but when i add Array, the numbers I get are random, can someone explain why the numbers are random? Here is the simple code:

int i, arr[5]; for(i=1; i<5; i++){ printf("%d ", arr[i]);

4
  • Your array arr is not initialized. Commented Dec 30, 2019 at 13:34
  • You're not storing anything to arr in that loop, Sans initialization, the content of arr is indeterminate. Commented Dec 30, 2019 at 13:34
  • You are printing out something that has not been initialized yet. what do you really want? Commented Dec 30, 2019 at 13:39
  • If you just to want to print out your values from the array in the same loop, just add one line before the printf to replace the “random values”: arr[i-1]=i; Commented Jul 22, 2022 at 21:18

5 Answers 5

3

Your code never explicitly assigns values to the items in the array. In C, variables that are not explicitly declared contain random values. If you're expecting the array to contain zeroes by default, you'd want to declare your array like this:

int arr[5] = {0};

If you want to store the numbers 1 through 4 in your array, you need to add a line in your for loop to assign those values:

arr[i] = i + 1;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I have one more question, how can I make my for loop start counting from start to store the numbers in Array like this : 1, 2, 3, 4, 1, 2, 3, 4 … etc? I used while(i!=5) and then the for loop, but the code was then stuck in my for loop. So what line can make the for loop start counting from start to fill the Array?
You can use the modulus operator to achieve this, like so (make sure this is inside your for loop): arr[i] = (i % 4) + 1;
1

Without initializing arr, you end up with garbage values.

The code for initializing during declaration is given below:

int arr[5] = {1, 2, 3, 4, 5}; // This is declaration and initialisation of arr
for (int i = 0; i < 5; i++) {
  printf("%d ", arr[i]);
}

Comments

1

Need to initialize the array and the variable before using them.

int i = 0, arr[5] = {1,2,3,4,5};
    for(i=0;i<5;i++)
        printf("%d ",arr[i]);

updated the proper answer.

2 Comments

This is incorrect. The for loop will iterate 5 times (from 0 to 4 inclusive). The problem is not related to array indices; the problem is that the values inside the array are never set.
I've noticed this error now and shared a new command line. I'd appreciate it if you examined him. Thank you.
1

You have declared a array but didn't assigned any value so compiler automatically assigning garbage value in it. That's why you get some random values.

#include<stdio.h>
int main()
{
         int arr[5] = {0};
 
         for(int i = 0; i < 5; i++)
         {
                 printf("%d\n", arr[i] = i + 1);
         }
}

Comments

-1

And insert after int i, arr[5];

arr[5] = {1,2,3,4,5};

or try this code...

int i,arr[5];
    for(i=0;i<5;i++){
        arr[i] = (i + 1);
        printf("%d ",arr[i]);
}

2 Comments

Please delete this answer and edit your existing answer.
Please do that.

Your Answer

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