警告:“numRest”的类型默认为“int”(在函数 'sleep' 中)

Warning: type of ‘numRest’ defaults to ‘int’ (in function 'sleep')

我在函数“sleep”中收到警告:警告:“numRest”的类型默认为“int”,我不知道为什么。它 运行 非常好,但显然我收到了这个警告。其他人在 运行 时收到此警告吗?

void sleep(numRest){

if ((numRest >= 0) && (numRest <=4)){
    printf("Sleep deprived!");
}


else if ((numRest > 4) && (numRest < 6)){
    printf("You need more sleep.");
}


else if ((numRest >= 6) && (numRest < 8)){
    printf("Not quite enough.");
}


else{
    printf("Well done!");
}

return;
}

int main()
{
int numSleep = -1;


if (numSleep == -1){
    printf("Test 1\n");
    printf("Input: -1\n");
    printf("Expected Result: Error, you cannot have a negative number of hours of sleep.\n");
    printf("Actual Result: ");
    sleep(numSleep);
    printf("\n\n");

    numSleep = 4.5;
    printf("Test 2\n");
    printf("Input: 4.5\n");
    printf("Expected Result: You need more sleep.\n");
    printf("Actual Result: ");
    sleep(numSleep);
    printf("\n\n");


}





return 0;
}

问题出在函数签名定义上。

 void sleep(numRest) {

应该是

void sleep(int numRest) {

否则,编译器将"assume"(现在已被最新标准淘汰)缺少的数据类型是int

相关,引用自C11,主要变化(相对于之前的版本)列表

  • remove implicit int

也就是说,

  • sleep() 是一个 library function already,原型是 unistd.h不要 尝试将相同的用于用户定义的函数。
  • int main() 应该是 int main(void),至少托管环境要符合标准。

您必须在函数声明中显式地将变量类型设置为:

void sleep(int numRest) {

//your code here

}