如何确定一个char变量赋值多字符字符常量的结果? [英] How to determine the result of assigning multi-character char constant to a char variable?

查看:92
本文介绍了如何确定一个char变量赋值多字符字符常量的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么一个char变量获得'B'从'AB'的分配,而'一'?

 的char c ='AB';的printf(C:%C \\ n,C);

打印:

  C:将B


解决方案

这是实现定义为前面的答案已经说。

我的海湾合作​​委员会处理'AB'为int。下面code:

 的printf(的sizeof('AB')=%祖\\ N的sizeof('AB'));
的printf('AB'=为0x%08X \\ n,'AB');
的printf('ABC'=为0x%08X \\ n,ABC);

打印:

 的sizeof('AB')= 4
'AB'= 0x00006162
'ABC'= 0x00616263

在您的code,该行:

 的char c ='AB';

可以被认为是:

 的char c =(char)的(0x00006162&安培; 0xFF的);

所以C得到'AB'的最后一个字符的值。在这种情况下,它是B(0X62)

Why a char variable gets 'b' from assignment of 'ab', rather 'a'?

char c = 'ab';

printf("c: %c\n", c);

Prints:

c: b

解决方案

This is implementation defined as earlier answers already say.

My gcc handles 'ab' as an int. The following code:

printf( "sizeof('ab') = %zu \n", sizeof('ab') );
printf( "'ab' = 0x%08x \n", 'ab' );
printf( "'abc' = 0x%08x \n", 'abc' );

prints:

sizeof('ab') = 4
'ab' = 0x00006162
'abc' = 0x00616263

In your code, the line:

char c = 'ab';

Can be considered as:

char c = (char)(0x00006162 & 0xFF);

So c gets the value of the last char of 'ab'. In this case it is 'b' (0x62).

这篇关于如何确定一个char变量赋值多字符字符常量的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆