gpt4 book ai didi

c - 在 C 中的循环内声明一个函数

转载 作者:太空宇宙 更新时间:2023-11-04 07:02:17 25 4
gpt4 key购买 nike

#include <stdio.h>

int main()
{
int intValue, menuSelect,Results;

intValue = 1;

while (intValue > 0)
{
printf ("Enter a positive whole number please.\n:");

scanf ("%d", &intValue);

if (intValue > 0)
{
printf ("Enter the number 2 to shrink the positive whole number\n:");

scanf ("%d", &menuSelect);

if (menuSelect == 2);
{
Results = shrink (intValue);

printf ("Shrink of %d is %d\n",intValue,Results);
}
}
return 0;
}

我一直收到这个错误代码

prog.c: In function 'main':
prog.c:24:12: warning: implicit declaration of function 'shrink' [-Wimplicit-function-declaration]
Results = shrink (intValue);
^
prog.c:37:1: error: expected declaration or statement at end of input
}
^

关于如何声明名为 shr​​ink 的函数以便它执行的任何建议:我应该将它放在 main 之外吗?或者可能在 while 循环中?或者在 if?

最佳答案

  1. 没有收缩函数的定义或声明。
  2. 您可以在 main() 之前声明函数并将函数定义写在程序的任何位置,但不能在另一个函数内。
  3. 您不应在循环内定义函数,因为您会遇到错误,除非您在函数声明之前添加检查以查看该函数是否已存在。
  4. if (menuSelect == 2); --> ; 在您的情况下不是必需的。

示例伪代码

int shrink(int x);
int main()
{
shrink(variable)
}
int shrink(int x)
{
<code>
}

int shrink(int x)
{
<code>
}

int main()
{
shrink(variable)
}

关于c - 在 C 中的循环内声明一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36557851/

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