2

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 ?

Community
  • 1
  • 1
Siraj Alam
  • 9,217
  • 9
  • 53
  • 65

2 Answers2

8

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Good one. Should mention that `(long int)` is the C-way while `static_cast` is the C++ one. – Irminsul Jun 24 '16 at 10:58
  • @Benoit: Both are valid in C++. – Lightness Races in Orbit Jun 24 '16 at 10:59
  • You seem really experienced in c++. I wonder what do you mean by 'valid'. Working ? Acceptable ? or perfectly suitable ? All references that i found said 'do not use C-like cast in c++'. I can't open a question because it will be tagged as duplicate but i read a lot of your posts and i am really curious about your opinion on that. – Irminsul Jun 24 '16 at 12:18
  • @Benoit: It's not vastly idiomatic, particularly for complex types, but it's completely fine for an arithmetic type as far as I'm concerned. Some people will insist on `static_cast` for absolutely everything but in this case it's unnecessarily verbose. In fact this came up just the other day: http://stackoverflow.com/a/37996003/560648 – Lightness Races in Orbit Jun 24 '16 at 14:06
  • Thanks ! That's exactly the kind of explanation and 'fight' I was looking for. – Irminsul Jun 24 '16 at 14:22
0

Use brackets for this. eg.

c = a * (long int) (b)

As type casted data type have multi-words.

neeh
  • 2,777
  • 3
  • 24
  • 32