gpt4 book ai didi

c - 我在这个循环中做错了什么?

转载 作者:行者123 更新时间:2023-11-30 20:36:37 25 4
gpt4 key购买 nike

#include<stdio.h>
int main()
{
for ( int i = printf("\nFor loop initialized ") , printf("Intialized value of i is %i", i) ;
i >= printf("\nCondition checked") , printf("Current value of i is") ;
printf("\nValue of i is decremented") , i-- )
{
printf(" %i", i);
}

printf("\nCondition false, Loop exited.");
return 0;
}

以下是 gcc -Wall -std=c11 -pedantic-errors 给出的错误:

5:53: error: expected declaration specifiers or '...' before string constant
5:84: error: expected declaration specifiers or '...' before 'i'
5:88: warning: value computed is not used [-Wunused-value]

什么是 ...

根据我的说法,输出应该是:

For loop initialized Intialized value of i is 21  
Condition checked Current value of i is 21
Value of i is decremented
Condition checked Current value of i is 20
Value of i is decremented
Condition checked Current value of i is 19
Value of i is decremented
Condition checked Current value of i is 18
Value of i is decremented
Condition checked
Condition false, Loop exited.

最佳答案

好的,这是这里发生的事情:

  1. 您使用的 C 版本不允许在 for 循环内进行声明。

  2. C99 确实允许此类声明,但在 C99 中,i 初始化后的 printf 会导致语法错误,因为它不是另一个声明。

  3. 循环无限的原因是因为比较后的逗号运算符,导致循环继续表达式的值是比较后面的printf的结果,即始终非零。

关于c - 我在这个循环中做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35537651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com