C语言中注意点

C语言中注意点
  • C把char常量视为int类型,而C++将其视为char类型,

    char ch = 'A';
    int x = 'ABCD';	/* !< 对于int是4字节的系统,该语句出现在C程序中没有问题,但是出现在C++程序中会出错*/
    

在C中,常量'A' 字符编码储存为一个int类型的值,相同的数值也储存在变量ch中,但是在ch中该值只占内存的1字节

eg:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x = 'ABCD';
    char c = 'ABCD';
    printf("%d %d %c %c\n", x, 'ABCD', c, 'ABCD');
    return 0;
}

/* !< output */
1094861636 1094861636 D D

Process returned 0 (0x0)   execution time : 0.005 s
Press any key to continue.

猜你喜欢

转载自www.cnblogs.com/xzpin/p/12693802.html