C++ 类型 unsigned long int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11779704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:31:48  来源:igfitidea点击:

C++ type unsigned long int

c++c

提问by Anders Lind

Is unsigned long intequivlant to unsigned long? in C++

unsigned long intequivlant来unsigned long?在 C++ 中

In my opinion they are same. But I saw some people still using unsigned long int in code. Don't understand why? Could anybody explain it for me

在我看来,它们是一样的。但是我看到有些人仍然在代码中使用 unsigned long int。不明白为什么?谁能给我解释一下

#include <stdio.h>

int main() {
    unsigned long int num = 282672; 
    int normalInt = 5;
    printf("");
    return 0;
}

回答by

Yes.

是的。

longis just a shorthand for long int. This is because in principle longis just a qualifier (it can also be used for prolonging a doubledatatype, for example)

long只是 的简写long int。这是因为原则long上只是一个限定符(例如,它也可以用于延长double数据类型)

From C++ ISO Standard, section 7.1.5.2, a table of equivalent type specifiers:

来自 C++ ISO 标准的第 7.1.5.2 节,等效类型说明符表:

enter image description here

在此处输入图片说明

回答by Hristo Iliev

§6.7.2 of the C99 standard gives the following list of types (this is only an excerpt):

C99 标准的 §6.7.2 给出了以下类型列表(这只是摘录):

  • short, signed short, short int, or signed short int
  • unsigned short, or unsigned short int
  • int, signed, or signed int
  • unsigned, or unsigned int
  • long, signed long, long int, or signed long int
  • unsigned long, or unsigned long int
  • long long, signed long long, long long int, or signed long long int
  • unsigned long long, or unsigned long long int
  • short, signed short, short int, 或signed short int
  • unsigned short, 或者 unsigned short int
  • int, signed, 或signed int
  • unsigned, 或者 unsigned int
  • long, signed long, long int, 或signed long int
  • unsigned long, 或者 unsigned long int
  • long long, signed long long, long long int, 或signed long long int
  • unsigned long long, 或者 unsigned long long int

with the following additional point:

有以下附加点:

(5) Each of the comma-separated sets designates the same type, except that for bit-fields, it is implementation-defined whether the specifier intdesignates the same type as signed intor the same type as unsigned int.

(5) 每个逗号分隔的集合指定相同的类型,除了位域之外,说明符int指定与 相同的类型signed int还是与 相同的类型是实现定义的unsigned int

回答by Sebastian Mach

Yes. unsigned, signed, short, long, long longall are simple type specifiers for XXX int.

是的unsigned, signed, short, long,long long都是 的简单类型说明符XXX int

See 7.1 Specifiers [dcl.spec]in the standard:

参见标准中的7.1 说明符 [dcl.spec]

3 [ Note: Since signed, unsigned, long, and short by default imply int, a type-name appearing after one of those specifiers is treated as the name being (re)declared. [ Example:

void h(unsigned Pc); // void h(unsigned int)

void k(unsigned int Pc); // void k(unsigned int)

—end example ] —end note ]

3 [ 注意:由于默认情况下有符号、无符号、长整型和短整型都表示 int,因此出现在这些说明符之一之后的类型名称将被视为(重新)声明的名称。[ 例子:

void h(unsigned Pc); // void h(unsigned int)

void k(unsigned int Pc); // void k(unsigned int)

—结束示例] —结束注释]

and 7.1.6.2 Simple type specifiers [dcl.type.simple]

7.1.6.2 简单类型说明符 [dcl.type.simple]

    Table 10 — simple-type-specifiers and the types they specify

    Specifier(s)            | Type
    ------------------------+---------------------------------
    type-name               | the type named
    simple-template-id      | the type as defined in 14.2
    char                    | “char”
    unsigned char           | “unsigned char”
    signed char             | “signed char”
    char16_t                | “char16_t”
    char32_t                | “char32_t”
    bool                    | “bool” 
    unsigned                | “unsigned int”
    unsigned int            | “unsigned int”
    signed                  | “int”
    signed int              | “int”
    int                     | “int”
    unsigned short int      | “unsigned short int”
    unsigned short          | “unsigned short int”
    unsigned long int       | “unsigned long int”
    unsigned long           | “unsigned long int”
    unsigned long long int  | “unsigned long long int”
    unsigned long long      | “unsigned long long int”
    signed long int         | “long int”

回答by trojanfoe

unsigned long intis the correct type definition, however intcan be ignored.

unsigned long int是正确的类型定义,但是int可以忽略。

回答by NX1

Yes, they are the same. Saying unsigned long intis simply explicitly stating that it is an int.

是的,它们是一样的。说unsigned long int只是明确说明它是一个整数。

You can always look at the size of the type by sizeof(unsigned long int)and sizeof(unsigned long)

您始终可以通过sizeof(unsigned long int)和查看类型的大小sizeof(unsigned long)

Hope this helps.

希望这可以帮助。