21

unsigned long int 相当于unsigned long?_ 在 C++ 中

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

#include <stdio.h>

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

5 回答 5

24

是的。

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

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

在此处输入图像描述

于 2012-08-02T14:35:10.550 回答
4

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

  • 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

还有以下几点:

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

于 2012-08-02T14:44:35.400 回答
1

的。unsigned, signed, short, long,long 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”
于 2012-08-02T14:44:54.697 回答
0

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

于 2012-08-02T14:35:43.850 回答
0

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

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

希望这可以帮助。

于 2012-08-02T14:36:34.550 回答