C printf %c 字符打印

C printf %c character printing

以下代码打印(用 clang 编译)

输出

[A][?][?]

代码

#include <stdio.h>

int main(){
    char a = "A";
    printf("[A]");
    printf("[%c]", a);
    printf("[%c]", "A");
}

给出警告(clang 发出)

test.c:4:10: warning: incompatible pointer to integer conversion initializing
      'char' with an expression of type 'char [2]' [-Wint-conversion]
    char a = "A";
         ^   ~~~
test.c:7:20: warning: format specifies type 'int' but the argument has type
      'char *' [-Wformat]
    printf("[%c]", "A");
             ~~    ^~~
             %s

但是

输出

[A][z][z]Characters: a A 

代码

int main(){
    char a = "A";
    printf("[A]");
    printf("[%c]", a);
    printf("[%c]", "A");
    printf ("Characters: %c %c \n", 'a', 65);

}

我认为它与内存和整数有关(既因为警告,也因为呈现为“?”的 "A" 转到 "z",即 "A"--).

那是因为"A"'A''[=14=]'const char[2]串。使用:

char a = 'A';

得到你想要的东西。