简体   繁体   English

将字符串文字分配给char变量时,为什么打印字符d?

[英]Why is the character d printed when I assign a string literal to a char variable?

#include<stdio.h>

int main(void) {
    char a = "any"; //any string
    printf("%c", a);
    getch();
}

Why always d (for %c ) or 100 (for %d ) gets printed? 为什么总是打印d (对于%c )或100 (对于%d )? What's happening? 发生了什么?

char a="any"; is not declaring any string. 没有声明任何字符串。 Your compiler should through error/warning. 您的编译器应通过错误/警告。 You need 你需要

const char *a = "any";   

Now 现在

printf("%c", *a);

will print character a as pointer a is pointer to the first element of the string literal any . 将打印字符a作为指针a是指向字符串文字any的第一个元素的指针。

Your code has issues. 您的代码有问题。 On

char a="any";

the string literal "any" is a pointer but you are saving it in a (small) integer type. 字符串文字“ any”是一个指针,但是您将其保存为(小)整数类型。 Because the value of a is (part of) an address it will have an essentially arbitrary value. 因为a的值是一个地址(的一部分),所以它实际上具有任意值。 On my machine it prints "T" (if I remove the getch() line because that doesn't compile). 在我的机器上,它会打印“ T”(如果我删除了getch()行,因为该行无法编译)。

GCC gives the following warning: GCC发出以下警告:

warning: initialization makes integer from pointer without a cast

and whatever compiler you are using probably tells you something similar. 无论您使用哪种编译器,都可能会告诉您类似的信息。 You should really take this warning seriously. 您应该认真对待此警告。

"something between double quotes" 

Allocates memory for the string and returns a pointer to the string in memory. 为字符串分配内存,并返回指向内存中字符串的指针。 you need to store this pointer in a using char *a = "abc"; 你需要这个指针存储在a使用char *a = "abc"; now 'a' is a pointer to the string abc. 现在,“ a”是指向字符串abc的指针。

printf("%s",a) this will print the entire string abc.

for a single character use single quotes 'a' in this case char a = 'b'; 对于单个字符,在这种情况下使用单引号'a' char a = 'b'; is correct. 是正确的。

printf("%c",a) this will print b.
printf("%d",a) this will print the ascii value of 'b', which is 98

In the statement 在声明中

char a = "any";

the string literal "any" evaluates to the address of its first character which is of type const char * . 字符串文字"any"计算结果为其第一个字符的地址,该地址的类型为const char * Assigning a const char * to a char variable implicitly converts the pointer to an integer. char变量分配const char *会将指针隐式转换为整数。 This emits the following warning if you compile with -W gcc flag: 如果使用-W gcc标志进行编译,则会发出以下警告:

warning: initialization makes integer from pointer without a cast [enabled by default]

What happens next is the least significant byte of the cast integer value is assigned to the char variable a . 接下来发生的是将强制转换整数值的最低有效字节分配给char变量a This is a random value and cannot be predicted. 这是一个随机值,无法预测。 On my machine, it happened to be the ascii code for the character / . 在我的机器上,它恰好是字符/的ascii代码。 On your machine, it happened to be 100, the ascii code for the character d . 在您的计算机上,它恰好是100,即字符d的ascii代码。 It is just pure chance. 这只是纯粹的机会。 Nothing else. 没有其他的。

You should assign a string literal to a const char * as: 您应该将字符串文字分配给const char *如下所示:

const char *a = "any";

and print the string pointed to by a by the %s conversion specifier as: 和打印字符串指向a%s转换指定为:

printf("%s\n", a); 

Also note that getch is not a C standard library function, so your program won't compile on a linux machine. 还要注意, getch不是C标准库函数,因此您的程序不会在linux计算机上编译。 You can use getchar for the same effect, though. 不过,您可以使用getchar达到相同的效果。

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

相关问题 #define一个字符串文字,然后将其分配给一个char * - #define a string literal then assign it to a char* 为什么写入用字符串文字初始化的“char *s”而不是“char s[]”时会出现分段错误? - Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"? 使用字符串文字处理char * init时崩溃,但不使用malloc处理崩溃 - Crash when handling char * init'd with string literal, but not with malloc 当我将一个字符串分配给一个字符时会发生什么? - What happens when I assign a string to a char? 当我将字符串文字分配给非常量指针时,为什么我的 C 编译器不发出警告? - Why doesn't my C compiler warn when I assign a string literal to a non-const pointer? 将char数组的第一个字符分配给另一个变量 - Assign first character of a char array to another variable 我可以将打印的 output 分配给变量吗? - Can I assign a printed output into a variable? 如何从 2-D 字符字符串访问字符 - how do I access a character from 2-D char string 当char *设置为字符串文字时,编译器警告 - Compiler warning when char * set to string literal 如何重用一个字符和一个字符字符串常量中的文字? - How to reuse a literal in a char and a one-character string constant?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM