gpt4 book ai didi

c - C语言中如何分配unsigned short和unsigned long类型的变量?

转载 作者:行者123 更新时间:2023-12-02 08:36:20 25 4
gpt4 key购买 nike

根据书籍,整数类型的变体占用或多或少的内存字节,具体取决于体系结构,但是类型 unsigned short 值最多可以达到 65 535,因为类型 unsigned long 4 294 967 295。

到目前为止一切顺利,但是当我们想到格式占位符时,我们可以看到 int 类型的哪些变体(如 short 和 long)有自己的格式占位符;分别为 %hd%ld。这样我们就可以知道为这种类型保留了多少字节,因为我们知道我们正在显式使用数据类型,但是,对于无符号短和无符号长类型,只有一个格式占位符:%你.

对我来说,这意味着我无法控制将在此变量中设置的数字,我想要的意思是,我必须将变量声明为 unsigned 并且编译器将解释他的数字是否属于 unsigned short 或 unsigned 的范围长。

声明这种类型变量的正确形式是什么?

unsigned short <identifier_variable>;

unsigned short int <identifier_variable>;

或者只是:

unsigned <identifier_variable>;

并且如果数字属于unsigned short类型的范围,则为该变量预留2字节,否则如果分配的数字属于unsigned long类型,都将正常工作并预留字节数因为此变量将为 4 个字节

那么,我必须如何做这个声明,是否有我在 GNU 文档中找不到的其他格式占位符?

最佳答案

So far so good, but when we think of the format placeholders, we can see which variations of int type like short and long have your own format placeholders; %hd and %ld, respectively. This way we can know how many bytes will be reserved for this types, because we have we are aware that we are using explicitly the data types, however, for unsigned short and unsigned long types just there a single format placeholder: %u.

更正:unsigned short 和 unsigned long 有 %hu%lu .请注意这些如何反射(reflect)签名格式占位符。 %d%u用于签名和未签名 int s,分别。 hl修饰符变化 intshortlong .

%hd     signed short
%hu unsigned short
%d signed int
%u unsigned int
%ld signed long
%lu unsigned long

What is the correct form of to declare variables of this types?

unsigned short <identifier_variable>;

unsigned short int <identifier_variable>;

or just:

unsigned <identifier_variable>;

前两个是等价的。最后一个unsigned表示 unsigned int而不是 unsigned short .

请注意 shortshort int 的简写, 和 longlong int 的缩写.还有 signed是隐含的,所以 signed intint相同. (异常(exception):charsigned char 不同,它是具有实现定义的符号的不同类型。)

以下是编写基本整数类型的所有不同方式:

char
signed char
unsigned char
short AKA signed short, signed short int
unsigned short AKA unsigned short int
int AKA signed, signed int
unsigned AKA unsigned int
long AKA signed long, signed long int
unsigned long AKA unsigned long int

关于c - C语言中如何分配unsigned short和unsigned long类型的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20982292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com