c - 警告 : type of ‘numRest’ defaults to ‘int’ (in function 'sleep' )

标签 c syntax gcc-warning function-declaration

我在函数“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) {

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

相关,引用自 C11,Major changes (over previous versions) list

  • remove implicit int

也就是说,

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

关于c - 警告 : type of ‘numRest’ defaults to ‘int’ (in function 'sleep' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40565736/

相关文章:

objective-c - 子类中 BOOL 上的 "Subscripted value is neither array nor pointer"错误

c - 在不知道整数的实际个数的情况下扫描多个整数

c++ - 这第二个是什么新东西?

c - 如何打印一个函数的地址?

c - 为什么 "function name"在 C 中评估为真以及如何收到警告

c - 如何在微 Controller 中实现多任务?

c - 为什么这个堆栈溢出这么快?

python - 条件分配

java - 此行在 Java : boolean retry = id == 1; 中是什么意思

C:为什么为 int 参数传递 float/double 文字不会引发警告?