简体   繁体   中英

Explain about the nature of output of the code?

Code:

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

Output:

1 66
2 67
3 68
4 69
5 70

Can Anyone explain about the nature of output of the code?

i is unitialized when you set j=i+1. So j (initially) could be almost anything.

In your code i , j are not initialized at the time of declaration.
In for loop you assign j = i + 1 So j remains garbage value whereas i assigned 1 ,

in for loop you increment i , j and printf values. i increment from 1 to 5 , and j from a initial garbage value (that is 66 in your output) to initial garbage + 5 .

Edit On the basis of comments:

If you don't assign an initial value upon declaration the variable will be pointing at an address that may contain previously used data from another application (or any last used).

Before allocating memory in runtime system does not clear the memory before allocating ( just to keep system performance high ) So,default value of the variable is garbage value.

j is assigned the value of i even before i is assigned = 1. So i here can be any arbitrary value provided to it by the OS. In the above case the value assigned to i by the OS was 66. This arbitrary value could be different on varying systems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM