Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "long" still useful in C?

Tags:

c

  • It's not the largest integer type anymore now that there's "long long".
  • It's not a fixed-width type: It's 32 bits on some platforms and 64 on others.
  • It's not necessarily the same size as a pointer (for example, on 64-bit Windows)

So, does "long" have any meaning anymore? Is there ever a reason to declare a long instead of a ptrdiff_t or int64_t?

like image 488
dan04 Avatar asked Mar 07 '10 22:03

dan04


People also ask

Is there long long in C?

So, yes, this is the biggest integer type specified by C language standard (C99 version).

How big is a long long in C?

Long long signed integer type. Capable of containing at least the [−9,223,372,036,854,775,807, +9,223,372,036,854,775,807] range. Specified since the C99 version of the standard. Long long unsigned integer type.

What is the difference between int64 and long?

There is no difference in the compiled code. They are aliases for the same thing.

What is type long long?

LongLong (LongLong integer) variables are stored as signed 64-bit (8-byte) numbers ranging in value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The type-declaration character for LongLong is the caret (^). LongLong is a valid declared type only on 64-bit platforms.


1 Answers

Is there ever a reason to declare a long instead of a ptrdiff_t or int64_t?

There never was in those cases. If you want a pointer difference, or a specifically 64-bit value, you should be using ptrdiff_t or int64_t. You should never have been using long in the first place, except maybe behind a platform-dependent typedef.

You should use long when you need at least a 32-bit integer, because int is only guaranteed to be at least 16 bits. If you need a native type, but need at least 32-bits, use long. If the 16-bit limitation is acceptable on some old platforms (that probably don't matter and your code probably won't be compiled on ever) then it doesn't particularly matter.

like image 52
Chris Lutz Avatar answered Oct 26 '22 18:10

Chris Lutz