函数'enterChar'的隐式声明[-Wimplicit-function-

4 c

我这里有两个问题对我的C程序:1) main(),行C = enterChar();,N = enterNum();,leftJustifiedPic(C, N);,rightJustifiedPic(C, N);全都给我implicit declaration of function.那有什么意思?我已经习惯了Java,在C代码方面它有点不同吗?

2)在方法enterChar()中,我得到conflicting types for 'enterChar'错误并再次不明白它意味着什么以及为什么会发生.我正在研究Eclipse(Cygwin-GCC),如果它与问题有关.

请问smb请告诉我这类错误和警告?我很感激!

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Welcome to the menu!");
    printf("The menu is:\n1. Enter/Change Character\n2. Enter/Change Number\n3. Print Triangle Type 1(Left Justified)\n4. Print Triangle Type 2(Right Justified)\n5. Quit");
    printf("\n");
    printf("Now enter a number from the menu from 1 through 5: \n");
    int num = 0;
    scanf("%d", &num);

    char C;
    int N = 0;
    switch(num){
        case 1:
            C = enterChar();
            break;
        case 2:
            N = enterNum();
            break;
        case 3:
            leftJustifiedPic(C, N);
            break;
        case 4:
            rightJustifiedPic(C, N);
            break;
        default:
            printf("Smth is wrong!");
    }

    return 0;
}

char enterChar(){
   printf("Enter your input as a character. Only 'C' and 'c' are allowed!\n");
   char input = 0 ;
   scanf("%c", &input);
   while(input != 'c' || input != 'C'){

       if(input != 'C' || input != 'c'){
            printf("You have to enter 'C' or 'c'. Try again!");
       }

   }
   return input;
}
Run Code Online (Sandbox Code Playgroud)

jua*_*nza 11

1)在使用它们之前没有声明这些函数,并且您使用的C语言具有"隐式函数声明".这意味着函数被隐式声明为返回int并获取任何类型的任意数量的参数.

2)因为你有一个隐式函数声明int enterChar(),它与定义冲突char enterChar().

解决方案是在之前提供函数声明main().

char enterChar(); // and other function declarations

int main(void) {
  ....
}

// function definitions
char enterChar() { .... }
Run Code Online (Sandbox Code Playgroud)

根据您的用例,可能值得研究使用更新版本的C,它没有这些隐式函数声明(例如C99或C11)


R S*_*ahu 5

当未声明函数原型时,编译器会假定返回类型为int

那就是所谓的隐式声明。

然后,您声明enterChar返回char。由于编译器在调用它时使用了隐式声明main,因此它抱怨类型冲突。

您可以通过在中使用函数之前提供明确的函数声明来解决该问题main

char enterChar();
Run Code Online (Sandbox Code Playgroud)

在使用所有函数之前,最好对它们提供明确的声明。