在c中打印出字符

Eve*_*lse -1 c arrays printf char

我希望能够在c中打印出charcter的值,如下所示:

fprintf("%c", alphabet[val]);
Run Code Online (Sandbox Code Playgroud)

,其中字母表被初始化为

for(i = 0; i < 26; i++){
    alphabet[i] = i + 65;
}
Run Code Online (Sandbox Code Playgroud)

但是,此行会出现以下错误:

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]);
     ^
Run Code Online (Sandbox Code Playgroud)

如何打印出这样的角色?

Sou*_*osh 5

仔细检查您的代码.对于声明

 fprintf("%c", alphabet[val]);
Run Code Online (Sandbox Code Playgroud)

文件指针在哪里?它不见了.您必须提供要查看输出的文件指针,例如

      fprintf(fp, "%c", alphabet[val]);  
Run Code Online (Sandbox Code Playgroud)

其中,fp是文件指针(类型FILE *),通常返回fopen(),请查看手册页以获取更多详细信息.

如果您希望打印到达stdout,即标准输出,请使用printf().