Ok、私はC言語のノブですが、簡単です。このプログラムは、大学の割り当てのためのもので、その中にisdigit()関数があるはずです。ここでは、コードGCCコンパイルエラー:フォーマット '%c'は 'char *'型の引数を期待していますが、引数2の型は 'int' [-Wformat]です。
//by Nyxm
#include <stdio.h>
#include <ctype.h>
main()
{
char userChar;
int userNum, randNum;
srand(clock());
printf("\nThis program will generate a random number between 0 and 9 for a user to guess.\n");
/*I changed it from '1 to 10' to '0 to 9' to be able to use the isdigit() function which
will only let me use a 1 digit character for an argument*/
printf("Please enter a digit from 0 to 9 as your guess: ");
scanf("%c", userChar);
if (isdigit(userChar))
{
userNum = userChar - '0';
randNum = (rand() % 10);
if (userNum == randNum)
{
printf("Good guess! It was the random number.\n");
}
else
{
printf("Sorry, the random number was %d.\n", randNum);
}
}
else
{
printf("Sorry, you did not enter a digit between 0 and 9. Please try to run the program again.\$
}
}
私がコンパイルしようとすると、私は
week3work1.c: In function ‘main’:
week3work1.c:14:2: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
次のエラーを取得する地球上の何が起こっているか?ですか私は助けが切望されています。どんな助けでも。私は真剣にこのプログラムをあきらめようとしています。私の教科書が "%c"が正規の 'char'のものであることを示すとき、なぜ 'char *'の引数を期待しているのですか?私はnano、gcc、Ubuntuを使用しています。
'gcc'はあなたのプログラムに対して複数の警告を生成します。あなたは* all *を修正する必要があります。たとえば '#include'( 'srand()'と 'rand()')と '#include '( 'clock()'に必要です)がありません。 –
@KeithThompsonコンパイル時にエラーがなくなり、今度はプログラムが正常に動作します。だから、私はそれが動作し続けるかどうか尋ねていると思いますか?私の教科書では、必要なプリプロセッサ文だけが '#include'で、 '#include 'はisdigit()関数用です。 –
Nyxm
これらの関数を呼び出す場合は、上記のヘッダーが必要です。あなたがそれらを使用せずに立ち去ることができる状況がありますが、私は詳細には入りません。 '#include'ディレクティブを追加するだけです。 '-std = c99 -pedantic'を使うとgccは関数呼び出しについて警告します。どの教科書を使っていますか? '#include'を使わずに 'rand()'を呼び出す、または '#include 'を使わずに 'clock()'を呼び出す例を示していれば、教科書は間違っています。 –