[英]implicit declaration of function 'GetInt' is invalid in C99
我正在尝试在 C 中编写这个简单的练习代码。它要求用户提供一个正数,检查它是否为正数,然后只返回正数。
我收到此错误:
positive.c:28:7: warning: implicit declaration of function 'GetInt' is invalid
in C99 [-Wimplicit-function-declaration]
n = GetInt();
我本以为这意味着我没有声明我的功能之一,或者我没有在某些库中调用。 据我所知,我做了所有这些。 这是我的代码:
#include <stdio.h>
int GetPositiveInt(void);
int main(void)
{
int n = GetPositiveInt();
printf("Thanks for the %i\n", n);
}
/*This all gets called into the above bit*/
int GetPositiveInt(void)
{
int n; /*declare the variable*/
do
{
printf("Give me a positive integer: ");
n = GetInt();
}
while (n <= 0);
return n; /*return variable to above*/
}
有谁知道为什么这会给我一个错误?
那是因为这个 function, GetInt
不存在,或者您忘记包含正确的 header。
您可以通过以下方式替换对 function GetInt
的调用:
scanf("%d", &n);
您尚未声明GetInt()
。 编译器不知道它返回什么以及它收到什么 arguments。 C99 中禁止隐式声明(之前有效 - 只有在启用 C89 时才会产生警告)。
当然,如果你只有声明而没有实现——你会在链接阶段得到一个错误。
为 CS50 edX class 创建了一个库,您需要将其包含在 your.c 文件的顶部。 否则编译器不知道 GetInt() function。
#include <cs50.h>
为我工作:您可以使用get_int();
在 CS50 IDE 中并使用make
编译它或简单地将 -lcs50 放在-lcs50
的末尾。
CS-50 库已更新。 现在你必须使用:
$ int x = get_int("Prompt: ");
而不是GetInt()
您的代码将如下所示:
#include <stdio.h>
int main(void)
{
int n; /*declare the variable*/
do
{
n = get_int(“Give me a positive integer: “);
}
while (n <= 0);
return n; /*return variable to above*/
}
这对我有用。 我也在做 CS50x 2023 计算机科学导论。
摘自“短片”视频之一(第 1 周:C)。 条件语句
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int x = get_int("Prompt: ");
switch(x)
{
case 5:
printf("Five, ");
case 4:
printf("Four, ");
case 3:
printf("Three, ");
case 2:
printf("Two, ");
case 1:
printf("One, ");
default:
printf("Blast-off!\n");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.