简体   繁体   English

在c中打印出字符

[英]Print out character in c

I'd like to be able to print out the value of a charcter in c as follows: 我希望能够按如下所示打印出c中字符的值:

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

, where alphabet is initialized as ,其中字母初始化为

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

However, this line gives the following errors: 但是,此行出现以下错误:

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]);
     ^

How can I print out a character like this? 如何打印出这样的角色?

Check your code closely. 仔细检查您的代码。 For the statement 对于声明

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

where is the file pointer? 文件指针在哪里? It's missing. 它不见了。 You have to provide the file pointer, to which you want to see the output, something like 您必须提供要查看其输出的文件指针,例如

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

where, fp is the file pointer (of type FILE * ), usually returned by fopen() , check the man page for more details on this. 其中, fp是文件指针(类型为FILE * ),通常由fopen()返回,有关更多详细信息,请参见手册页

In case, you want the prints to arrive on stdout , ie, the standard output, use printf() . 如果您希望打印输出到达stdout ,即标准输出,请使用printf()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM