简体   繁体   English

function 'GetInt' 的隐式声明在 C99 中无效

[英]implicit declaration of function 'GetInt' is invalid in C99

I am trying to make this simple code for practice in C. It asks the user to give a positive number, checks if it's positive or not, then returns just positive numbers.我正在尝试在 C 中编写这个简单的练习代码。它要求用户提供一个正数,检查它是否为正数,然后只返回正数。

I am getting this error:我收到此错误:

positive.c:28:7: warning: implicit declaration of function 'GetInt' is invalid
  in C99 [-Wimplicit-function-declaration]
            n = GetInt();

I would have thought this meant I didn't declare one of my functions or that I didn't call in some library.我本以为这意味着我没有声明我的功能之一,或者我没有在某些库中调用。 To the best of my knowledge, I did all of this.据我所知,我做了所有这些。 Here is my code:这是我的代码:

#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*/
}

Does anyone have any ideas on why this is giving me an error?有谁知道为什么这会给我一个错误?

That's because this function, GetInt does not exist or you forgot to include the correct header.那是因为这个 function, GetInt不存在,或者您忘记包含正确的 header。

You can replace the call to the function GetInt by:您可以通过以下方式替换对 function GetInt的调用:

scanf("%d", &n);

You haven't declared GetInt() .您尚未声明GetInt() Compiler don't know what it returns and what arguments it receives.编译器不知道它返回什么以及它收到什么 arguments。 Implicit declarations was forbidden in C99 (was valid before - would only produce warning if you'll enable C89). C99 中禁止隐式声明(之前有效 - 只有在启用 C89 时才会产生警告)。

Of course if you'll have only declaration and no implementation - you'll get an error on linking phase.当然,如果你只有声明而没有实现——你会在链接阶段得到一个错误。

There's a library created for the CS50 edX class you need to include at the top of your.c file.为 CS50 edX class 创建了一个库,您需要将其包含在 your.c 文件的顶部。 Otherwise the compiler doesn't know about the GetInt() function.否则编译器不知道 GetInt() function。

#include <cs50.h>

Worked for me: You may use get_int();为我工作:您可以使用get_int(); in the CS50 IDE and compile it using make or simply put -lcs50 at the end of clang.在 CS50 IDE 中并使用make编译它或简单地将 -lcs50 放在-lcs50的末尾。

The CS-50 library has been updated. CS-50 库已更新。 Now you must use:现在你必须使用:

$ int x = get_int("Prompt: "); $ int x = get_int("Prompt: ");

instead of GetInt()而不是GetInt()

Your code will look like this:您的代码将如下所示:

#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*/
}

This worked for me.这对我有用。 I'm also doing the CS50x 2023 Introduction to Computer Science.我也在做 CS50x 2023 计算机科学导论。

Taken from the one of the "Shorts" Video, (Week 1: C).摘自“短片”视频之一(第 1 周:C)。 Conditional Statements条件语句

#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.

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