PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 21, 2022

[FIXED] Why does MSVC pick a long long as the type for -2147483648?

 July 21, 2022     c++, integer, literals, types, visual-c++     No comments   

Issue

My snippet:

auto i = -2147483648;
int j = 3;
std::swap(i, j); // Compile error about mismatched types here. 

The compiler states that the literal i is a long long. Why is that? -2147483648 fits in an int on MSVC x64.

My compiler is MSVC, target is 64 bits.


Solution

Contrary to popular belief, -2147483648 is not a literal: C++ does not support negative literal values.

It is, in fact, a compile-time evaluable constant expression consisting of a unary negation of the literal 2147483648.

On MSVC x64, which has 32 bit ints and longs, 2147483648 is too big for either of those so it fails over to the long long type that you observe.



Answered By - Bathsheba
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing