簡體   English   中英

在c中打印出字符

[英]Print out character in 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]);
     ^

如何打印出這樣的角色?

仔細檢查您的代碼。 對於聲明

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

文件指針在哪里? 它不見了。 您必須提供要查看其輸出的文件指針,例如

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

其中, fp是文件指針(類型為FILE * ),通常由fopen()返回,有關更多詳細信息,請參見手冊頁

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM