[英]Implicit declaration of function x
我很清楚函數原型,這個錯誤似乎是一個函數聲明錯誤,這意味着我真的很困惑,為什么我看到這個警告,從而錯誤。
這幾乎就像gcc完全忽略了我的函數原型。 這是編譯器錯誤嗎?
為了簡潔起見,我沒有在單獨的頭文件中聲明此函數,盡管它應該沒有區別。
gcc輸出:
$ gcc -Wall -std=c99 -pedantic primefactors.c
primefactors.c: In function ‘main’:
primefactors.c:8:5: warning: implicit declaration of function ‘largestprime’ [-Wimplicit-function-declaration]
primefactors.c: At top level:
primefactors.c:12:6: error: conflicting types for ‘largestprime’
primefactors.c:8:20: note: previous implicit declaration of ‘largestprime’ was here
碼:
#include <stdio.h>
#include <math.h>
long largetsprime(long);
int main()
{
printf("%d\n", largestprime(600851475143));
return 0;
}
long largestprime(long num)
{
int highest;
int mid = sqrt(num);
for (int i = 2; i < mid; i++) {
if (mid % i == 0) {
if (i % 1 == 0 && i % i == 0)
highest = i;
}
}
return highest;
}
點1
你的函數名largest
拼寫錯誤
long largetsprime(long)
^
s is wrong here
在聲明它應該是
long largestprime(long)
^ before t
點2
您正在使用math.h
sqrt()
庫函數,您應該使用-lm
編譯您的程序:
gcc -Wall -std=c99 -pedantic primefactors.c -lm
點3
你返回int
而你的函數的返回類型很long
。
Point-4還有一個 錯誤 在調用printf()
建議您忘記為long int
添加后綴。
largestprime(600851475143)
應該:
largestprime(600851475143L)
// ^ added suffix L for long
如果您不知道后綴L
那么請閱讀: “L”在整數文字的末尾是什么意思?
感謝@ Eric Postpischil :
點-5: printf()
在main()
函數是印刷long
整型,而你已經使用%d
格式說明打印:
printf("%d\n", largestprime(600851475143));
^
|
returns long
請改用%ld
。
點6:
最大素數函數中的if
-condition i % 1 == 0 and i % i == 0
各自始終為真(除非后者未定義,如果i
為零),因為i % 1
= 0
(每個數字可被1
整除)。
錯字。 宣言稱largeTSprime
。 將它更改為正確的largestprime
並且它將起作用。
ProTip#1:使用camelCapsOnWordBoundaries
或under_scores
以提高可讀性。
ProTip#2:它幾乎從不是編譯器錯誤。
原型中有拼寫錯誤。 應該是largestprime
而不是largetsprime
。
原型中有拼寫錯誤:
largetsprime != largestprime
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.