简体   繁体   English

解释代码输出的性质吗?

[英]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. 当您设置j = i + 1时,我会被统一。 So j (initially) could be almost anything. 因此,j(最初)几乎可以是任何东西。

In your code i , j are not initialized at the time of declaration. 在代码ij在声明时未初始化。
In for loop you assign j = i + 1 So j remains garbage value whereas i assigned 1 , 在for循环中,您分配j = i + 1所以j仍然是垃圾值,而i分配了1

in for loop you increment i , j and printf values. 在for循环中,增加ij和printf值。 i increment from 1 to 5 , and j from a initial garbage value (that is 66 in your output) to initial garbage + 5 . i15递增,并且j从初始垃圾值(在您的输出中为66)增加到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. 甚至在分配i = 1之前,就给j分配i的值。因此,这里i可以是操作系统提供给它的任意值。 In the above case the value assigned to i by the OS was 66. This arbitrary value could be different on varying systems. 在上述情况下,操作系统分配给i的值为66。在不同的系统上,此任意值可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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