CS Student
CS Student

Reputation: 1633

Which is preferred: 'long int' or 'long'

What is the preferred way of defining a long integer in C? Are there any compatibility concerns?

long int ln;

or

long ln;

Upvotes: 1

Views: 166

Answers (5)

Fiddling Bits
Fiddling Bits

Reputation: 8861

If the company you work for has coding conventions, and they include a requirement on this, you should follow the convention. However, if there is no rule on how you should declare a long int, choose whatever seems best to you.

May I suggest though you use types defined in stdint. For example, long int may be equivalent to int32_t. In a lot of cases its useful to know the bit-width of the variables you're using.

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311028

You forgot to mention also signed long and signed long int:)

According to the C Standard

5 Each of the comma-separated multisets designates the same type, except that for bitfields, it is implementation-defined whether the specifier int designates the same type as signed int or the same type as unsigned int

So only for bitfields there is a difference between for example int and signed int

Take into account that you may write for example the following way

const signed const long const int ln;

It is equivalent to

const long ln;

Upvotes: 2

Izekid
Izekid

Reputation: 185

There is no difference between long and long int. You can use what you want. I would rather use long int to remember what you have done.

Upvotes: 3

wallyk
wallyk

Reputation: 57774

In addition to @unwind's answer, there is also long double and of course long long int. Long might be useful in other rare corners of implementations (long char?) but it is always a modifier, but int is assumed if there is nothing to modify.

C's syntax has traditionally implied int in many places:

myfunction (i, j)
{
     return 6 * i + j;
}

In 1979 implementations on V6 Unix, myfunction() would be interpreted to return type int and both parameters would also be assumed to be int unless further declared:

float myfunction (i, j)
 long i;  float j;
{
     return 6 * i + j;
}

Upvotes: 2

unwind
unwind

Reputation: 399871

There are no "compatibility concerns", no. They are the exact same type, long is a short form of the type name long int. Just like short is a short form of short int.

It's of course very subjective, but I think most C programmers just use long.

Upvotes: 5

Related Questions