2017-03-02 70 views
-1

我希望能夠在C打印出一個字符內的值,如下所示:打印出的字符在C

fprintf("%c", alphabet[val]); 

,其中字母被初始化爲

for(i = 0; i < 26; i++){ 
    alphabet[i] = i + 65; 
} 

然而,此行提供以下錯誤:

encode.c: In function ‘main’: 
encode.c:76:13: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [-Wincompatible-pointer-types] 
    fprintf("%c", alphabet[val]); 
      ^
In file included from encode.c:1:0: 
/usr/include/stdio.h:356:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘char *’ 
extern int fprintf (FILE *__restrict __stream, 
      ^
encode.c:76:19: warning: passing argument 2 of ‘fprintf’ makes pointer from integer without a cast [-Wint-conversion] 
    fprintf("%c", alphabet[val]); 
       ^
In file included from encode.c:1:0: 
/usr/include/stdio.h:356:12: note: expected ‘const char * restrict’ but argument is of type ‘char’ 
extern int fprintf (FILE *__restrict __stream, 
      ^
encode.c:76:5: warning: format not a string literal and no format arguments [-Wformat-security] 
    fprintf("%c", alphabet[val]); 
    ^

如何打印出這樣的字符?

+3

'fprintf'「打印」到一個文件。你想使用'printf'嗎? – alain

+1

[man fprintf](https://linux.die.net/man/3/fprintf)。投票結束這個簡單的錯字。 – Lundin

回答

5

請仔細檢查您的密碼。聲明

fprintf("%c", alphabet[val]); 

其中是文件指針嗎?它不見了。你必須提供文件指針,到你想看到的輸出,像

 fprintf(fp, "%c", alphabet[val]); 

其中,fp是文件指針(FILE *型),通常由fopen()返回,檢查man page瞭解更多詳情在這。

如果您希望打印到達stdout,即標準輸出,請使用printf()