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