Home > front end >  Is Visual Studio 2022 handle type promotion incorrectly?
Is Visual Studio 2022 handle type promotion incorrectly?

Time:12-22

AFAIK in C if you multiply two different type, the result will be in the larger type. But in Visual Studio 2022, this code:

#include <iostream>

int main()
{
    long a = 7182L * 300000;
    std::cout << a << '\n';
}

the output is -2140367296, surely this is wrong ? or maybe something is wrong with my installation of Visual Studio ? I try this with clang online and clang output 2154600000

CodePudding user response:

Thanks everyone for answerting. I guess the moral of the story is I shouldn't use long but int64_t.

CodePudding user response:

int main()
{
    // long a = 7182L * 300000;
    unsigned long a = 7182l * 300000;
    std::cout << a << '\n';
}

This result: 2154600000 for me. Windows 10 x64 PC Visual Studio 2022
So I think maybe long doesn't fit the result.
Correct me if I'm wrong.

  • Related