本文介绍了指定一个数字字面8位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

unsigned char ascii;
int a = 0;
char string[4] = ; "A1"
ascii = (string[a] - 'A' + 10) * 16;

警告:转换为无符号的字符
  从'诠释'可能会改变其值

似乎GCC考虑字符和数字面值为int默认。我知道我可以只投的前pression到(无符号字符),但我怎么可以指定字符的文字和数字文字为8位,但不强制转换?

It seems that gcc considers chars and number literals as int by default. I know I could just cast the expression to (unsigned char) but how can I specify char literals and number literals as 8 bit without casts ?

有一个类似的问题:

文字部分被认为是默认双,但他们可以指定浮动方式:

Literal fractions are considered double by default but they can be specified to float by:

3.1f

因此​​,3.1将被视为浮动,而不是双。

Therefore, 3.1 would be considered a float rather than a double.

推荐答案

C ,你不能做计算,在什么比 INT

In C, you cannot do calculations in anything shorter than int

char a = '8' - '0'; /* '8' is int */
char a = (char)'8' - '0'; /* (char)'8' is converted to `int` before the subtraction */
char a = (char)'8' - (char)'0'; /* both (char)'8' and (char)'0' are converted */

这篇关于指定一个数字字面8位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 22:55