繁体   English   中英

如何在char数组中放入“ 51”这样的两位数char?

[英]How to put double-digit char like '51' in a char array?

#include<stdio.h>


int main(){
char array[3][3]={{'2','1','3'},{'4','5','9'}};
array[0][0]='51';

}

错误警告:多字符常数[[Wmultichar] array [0] [0] ='51'; ^ ~~~ 17.4.c:6:17:警告:隐式常量转换中溢出[-Woverflow]

如果要在一个字符中存储两位十进制数字,则实际上可以使用4位半字节来存储数字

int two_to_one(const char *number)
{
    return *number - '0' + ((*(number + 1) - '0') << 4);
}

char *char one_to_two(int ch, char *buff)
{
    buff[1] = ch >> 4;
    buff[0] = ch & 0xf;
    buff[2] = 0;

    return buff;
}

字符只能包含一个符号。 “ 51”是两个符号。 如果将其写在双括号(“ 51”)之间,则可能是三个,因为C型字符串始终以\\0 要保留多个符号,应使用char指针和双括号,或使用一维以不同的方式访问它们:

char* array[3] = {"one", "two", "three"}; 
char string[3][7] = {"one", "two", "three"};

第二行表明可以使用最多包含7个字符(包括\\0 )的3个字符串。 我之所以选择这样的数字,是因为“三个”由6个符号组成。

如果要使用多字符常量,则可以将它们存储在大于char的整数变量中。 例如,这可以工作 -在某种意义上说,它存储了一个多字符:

int x = '52';

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM