C++类型unsigned long int [duplicate]

4xrmg8kj  于 2023-08-03  发布在  其他
关注(0)|答案(5)|浏览(186)

此问题已在此处有答案

Standard guarantees for aliases of integer types in C/C++? E.g.: Is "unsigned" always equal to "unsigned int"?(1个答案)
两年前关闭。
unsigned long int等价于C++中的unsigned long吗?
在我看来,它们是一样的。但是我看到有些人仍然在代码中使用unsigned long int,我不明白为什么。下面是一个示例代码:

#include <stdio.h>

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

字符串

zpqajqem

zpqajqem1#

是的。
long只是long int的简写。“这是因为原则上long只是一个限定符(例如,它还可以用于延长double数据类型)
C++ ISO标准的7.1.5.2一节中提供了等效类型说明符的表:


的数据

iyzzxitl

iyzzxitl2#

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

  • shortsigned shortshort intsigned short int
  • unsigned shortunsigned short int
  • intsignedsigned int
  • unsignedunsigned int
  • longsigned longlong intsigned long int
  • unsigned longunsigned long int
  • long longsigned long longlong long intsigned long long int
  • unsigned long longunsigned long long int

并附加以下一点:
(5)每个逗号分隔的集合指定相同的类型,除了对于位字段,说明符int是指定与signed int相同的类型还是与unsigned int相同的类型是实现定义的。

4szc88ey

4szc88ey3#

  • 是 *. unsignedsignedshortlonglong long都是XXX int的简单类型说明符。

参见标准中的 7.1说明符[dcl.spec]
3 [注意:由于signed、unsigned、long和short默认隐含int,出现在其中一个说明符之后的类型名称被视为正在(重新)声明的名称。[示例:
void h(unsigned Pc); // void h(unsigned int)
void k(unsigned int Pc); // void k(unsigned int)

  • 结束示例] -结束注解]
    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”

字符串

qpgpyjmq

qpgpyjmq4#

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

cidc1ykv

cidc1ykv5#

是的,它们是一样的。说unsigned long int只是明确地声明它是一个int。
您始终可以通过sizeof(unsigned long int)sizeof(unsigned long)查看类型的大小
希望这对你有帮助。

相关问题