Essential Knowledge Points for C Language Beginners: The Most Common int Type – Selection and Pitfalls

“From today on, study hard and make progress every day”

Repetition is the best method for memory; spend one minute each day to remember the basics of C language.

Series of 100 Essential Knowledge Points for C Language Beginners

11. The Most Common int Type: Selection and Pitfalls of Integer Variables

I. Overview of C Language Integer Types

The C language provides various integer data types, mainly divided into two categories:

Basic Integer Types

int         // Basic integer (typically 32 bits)
short       // Short integer (typically 16 bits)
long        // Long integer (typically 32 or 64 bits)
// Introduced in C99 standard
long long   // Long long integer (typically 64 bits)

Qualified Types

unsigned    // Unsigned type (only represents non-negative numbers)
signed      // Signed type (default)

II. Comparison of Storage Space for Each Type

(Based on typical 32/64-bit systems)

Type Byte Size Value Range
short 2 -32,768 ~ 32,767
unsigned short 2 0 ~ 65,535
int 4 -2,147,483,648 ~ 2,147,483,647
unsigned int 4 0 ~ 4,294,967,295
long 4/8 Depends on the system
unsigned long 4/8 Depends on the system
long long 8 -9.2×10¹⁸ ~ 9.2×10¹⁸
unsigned long long 8 0 ~ 1.8×10¹⁹

III. Principles for Type Selection

1. Choose Based on Value Range

short age = 25;              // Small range positive integer
unsigned int population = 8000000; // Large range non-negative integer
long long global_debt = 1000000000000LL; // Very large value

2. Choose Based on Memory Constraints

In memory-constrained environments like embedded systems:

short temp;    // Save memory (2 bytes)
unsigned char flag; // Minimum memory usage (1 byte)

3. Consider Platform Compatibility

Cross-platform code should use fixed-width types:

// Introduced in C99 standard
#include <stdint.h>

int32_t fixed_size;  // Guarantees 32-bit signed integer
uint64_t big_number; // Guarantees 64-bit unsigned integer

IV. Common Pitfalls and Solutions

1. Integer Overflow

unsigned char count = 255;
count++;  // Overflow becomes 0

Solution: Use a sufficiently large type or add boundary checks

2. Implicit Type Conversion

int a = -10;
unsigned int b = 5;
if (a < b) { /* This will yield unexpected results */ }

Solution: Standardize the types being compared

3. Literal Suffixes

long big = 1000000000;   // May be treated as int
long correct = 1000000000L; // Explicitly specify long

Correct usage:

long long = 100LL;
unsigned long = 100UL;

V. Practical Application Examples

1. Loop Counter Selection

// Small range loop
for (short i = 0; i < 100; i++) { /*...*/ }

// Large range loop
for (uint32_t i = 0; i < 100000; i++) { /*...*/ }

2. Bit Manipulation Scenarios

unsigned int flags = 0x0F;  // Unsigned is more suitable for bit manipulation
flags = flags << 4;

3. System Interface Compatibility

// File size handling
off_t file_size;  // Use system-defined off_t type

VI. Best Practice Recommendations

  1. 1. Default to <span>int</span> for most scenarios
  2. 2. Use <span>unsigned</span> when unsigned is explicitly needed
  3. 3. Use <span>long long</span> for large values
  4. 4. Consider <span>short</span>/<span>char</span> in embedded environments
  5. 5. Use <span><stdint.h></span> types for cross-platform code

VII. Type Size Detection Method

#include <stdio.h>

int main() {
    printf("short: %u bytes\n", sizeof(short));
    printf("int: %d bytes\n", sizeof(int));
    printf("long: %ld bytes\n", sizeof(long));
    return 0;
}

———- End ———-

[Special Statement: All articles in this public account are original or authorized by the author, with some content and images sourced from the internet and AI. Please feel free to consume, and the views are for learning reference only~~]

Essential Knowledge Points for C Language Beginners: The Most Common int Type - Selection and Pitfalls

“If you like C, please like it”Essential Knowledge Points for C Language Beginners: The Most Common int Type - Selection and Pitfalls Click the bottom right to seeEssential Knowledge Points for C Language Beginners: The Most Common int Type - Selection and Pitfalls

Leave a Comment