-4
auto x = -2147483648; // (unsigned long)
auto y = -2147483647; // (int)

In C language, the range of int type is "-2,147,483,648 ~ 2,147,483,647".

I think -2147483648 can be represented as an int type but represented as an unsigned long. Furthermore -2147483648 is out of range of the unsigned long type.

Why...?

(I'm using Visual Studio 2017)


Edited!

Wow. I read comments and I realized that my question should have been more precise.

1) Yes, In C++ language not in C language. (sorry)

2) int type range? : https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx

INT_MIN is define in limits.h

//
// limits.h
//
//      Copyright (c) Microsoft Corporation. All rights reserved.
//
// The C Standard Library <limits.h> header.
//

#define SHRT_MIN    (-32768)        // minimum (signed) short value
#define SHRT_MAX      32767         // maximum (signed) short value
#define USHRT_MAX     0xffff        // maximum unsigned short value
#define INT_MIN     (-2147483647 - 1) // minimum (signed) int value
#define INT_MAX       2147483647    // maximum (signed) int value
#define UINT_MAX      0xffffffff    // maximum unsigned int value
#define LONG_MIN    (-2147483647L - 1) // minimum (signed) long value
#define LONG_MAX      2147483647L   // maximum (signed) long value
#define ULONG_MAX     0xffffffffUL  // maximum unsigned long value
#define LLONG_MAX     9223372036854775807i64       // maximum signed long long int value
#define LLONG_MIN   (-9223372036854775807i64 - 1)  // minimum signed long long int value
#define ULLONG_MAX    0xffffffffffffffffui64       // maximum unsigned long long int value
wonsch
  • 37
  • 8

2 Answers2

0

There is no automatic type deduction in C. Lone auto without type specifiers either means auto int or is ill-formed, depending on what vintage of the C standard you are talking about. The implicit int feature was removed from the language in 1999.

Sample 1

Sample 2

If your tools are telling you that x is of type unsigned long, they are either buggy or set up for a language other than C (or both).

Note, this answer was written when the question was tagged with the C tag and didn't mention C++.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
-2

I found the answer.

Why does the smallest int, −2147483648, have type 'long'?

The answer explains that -2147483648 is same to -(2147483648).

wonsch
  • 37
  • 8