Search code examples
c++intlong-integertypecasting-operator

Typecasting from int to long int


Recently I searched the difference between int, long int, long, ... and so on. And I got the answer from here. And I found that long and long int are identical. So the statements c = a *long(b);

and

c = a * long int (b)

should be same in the program

int main()
{
    int a = 10, b = 20;
    long int c;
    
    c = a *long(b);
    cout << c;
    
    return 0;   
}

But the second statement is showing an error

[Error] expected primary-expression before 'long'

So I just want to know, if long and long int are identical, so why there is error in the above two statements ?


Solution

  • Just because they are the same type doesn't mean you can literally exchange the characters in your source code.

    The syntax is confused by a T() cast when T has a space in it.

    Write c = a * (long int)b instead.