【问题标题】:Print out character in c打印出c中的字符
【发布时间】:2017-03-02 13:37:59
【问题描述】:

我希望能够在 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“打印”到文件。你想使用printf吗?
  • man fprintf。投票结束这是一个简单的错字。

标签: c arrays char printf


【解决方案1】:

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

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

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

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

其中,fp 是文件指针(FILE * 类型),通常由fopen() 返回,请查看man page 了解更多详情。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2013-02-03
    相关资源
    最近更新 更多