我在寫第一個C程序。我試圖修改Ritchie和Kernighan關於Farhenheith和Celsius轉換的早期教程,但是我收到了我無法解決的錯誤。C編譯器錯誤
我的目標是運行./a.out fahr 20
,腳本打印出Farhenheit溫度從20開始(從300開始)到攝氏溫度的轉換。
但如果我寫./a.out celsius 0
腳本將打印出攝氏溫度的轉換從0開始(和去300)到華氏溫度。
這是當前的腳本(下面是從編譯器中得到的錯誤)。
我認爲有很多錯誤,但我只是開始學習和感謝任何幫助。
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv){
float fahr, celsius;
int upper, step;
upper = 300;
step = 20;
if (strcmp("fahr", argv[1] == 0)){
fahr = argv[2];
while (fahr <= upper){
fahr_conversion(fahr);
fahr = fahr + step;
}
}
else if (strcmp("celsius", argv[1] == 0)){
celsius = argv[2];
while (celsius <= upper){
cels_conversion(celsius);
celsius = celsius + step;
}
}
}
int fahr_conversion(float fahr){
float celsius;
celsius = (5.0/9.0) * (fahr - 32.0);
printf("%3.0f -> %6.1f\n", fahr, celsius);
}
int cels_conversion(float celsius){
float fahr;
fahr = (celsius + (9.0/5.0)) + 32.0;
printf("%3.0f -> %6.1f\n", celsius, fahr);
}
錯誤:
Far-Cels-Converter $ cc main.c
main.c:16:21: warning: incompatible integer to pointer conversion passing 'int'
to parameter of type 'const char *' [-Wint-conversion]
if (strcmp("fahr", argv[1] == 0)){
^~~~~~~~~~~~
/usr/include/string.h:77:39: note: passing argument to parameter here
int strcmp(const char *, const char *);
^
main.c:17:8: error: assigning to 'float' from incompatible type 'char *'
fahr = argv[2];
^~~~~~~~
main.c:19:4: warning: implicit declaration of function 'fahr_conversion' is
invalid in C99 [-Wimplicit-function-declaration]
fahr_conversion(fahr);
^
main.c:23:29: warning: incompatible integer to pointer conversion passing 'int'
to parameter of type 'const char *' [-Wint-conversion]
else if (strcmp("celsius", argv[1] == 0)){
^~~~~~~~~~~~
/usr/include/string.h:77:39: note: passing argument to parameter here
int strcmp(const char *, const char *);
^
main.c:24:11: error: assigning to 'float' from incompatible type 'char *'
celsius = argv[2];
^~~~~~~~
main.c:26:4: warning: implicit declaration of function 'cels_conversion' is
invalid in C99 [-Wimplicit-function-declaration]
cels_conversion(celsius);
^
main.c:32:5: error: conflicting types for 'fahr_conversion'
int fahr_conversion(float fahr){
^
main.c:19:4: note: previous implicit declaration is here
fahr_conversion(fahr);
^
main.c:38:5: error: conflicting types for 'cels_conversion'
int cels_conversion(float celsius){
^
main.c:26:4: note: previous implicit declaration is here
cels_conversion(celsius);
^
4 warnings and 4 errors generated.
偉大我認爲我幾乎在那裏:但是我收到了此警告:'main.c:21:10:warning:隱式聲明函數'strtof'在 無效C99 [-Wimplicit-function-declaration] fahr = strtof(argv [2],0);' – Jeff
作品。 Horray! – Jeff
沒錯,你需要''strtof'的''頭文件。 –
nemetroid