Siraj Alam
Siraj Alam

Reputation: 10025

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 ?

Upvotes: 2

Views: 20144

Answers (2)

sahil shelangia
sahil shelangia

Reputation: 11

Use brackets for this. eg.

c = a * (long int) (b)

As type casted data type have multi-words.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

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.

Upvotes: 8

Related Questions