What is the difference between long and int in the C la…

In C language, long and int are two types of data, their differences are as follows:

  1. Different sizes: int types typically take up 4 bytes (32 bits), while long types typically take up 8 bytes (64 bits).
  2. The range of values differs: the range for the int type is usually -2147483648 to 2147483647, while the range for the long type is typically -9223372036854775808 to 9223372036854775807.
  3. Memory usage varies: due to the larger memory space taken up by long data type, using long data type will consume more memory.
  4. Calculation results have different types: when an int variable is operated with a long variable, the result will automatically be promoted to a long type.
  5. Different ways of representing: When outputting, variables of type long need to be displayed using the %ld format, while variables of type int usually use the %d format.

It’s important to note that the actual size and range of integers long and int may vary due to differences in compilers and operating systems. Therefore, when writing cross-platform code, it is advisable to avoid relying on these differences. You can use int64_t defined in the standard header file to ensure the use of a specified size integer type.

bannerAds