“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute every day to remember the basics of C language.
“Series of 100 Essential Knowledge Points for C Language Beginners“
8. Declaration and Usage of Integer Variables: int
1. Basic Characteristics of int Type
int is the most commonly used integer type in C language, with the following characteristics:
- 1. Storage size: typically 4 bytes (32-bit system)
- 2. Value range: -2,147,483,648 to 2,147,483,647
- 3. Default is signed type (signed)
- 4. High computational efficiency, the type that the CPU handles best
2. Declaration Methods for int Variables
1. Basic Declaration Form
int variable_name;
Example:
int age; // Declare an uninitialized int variable
int count = 0; // Declare and initialize to 0
int a, b, c; // Declare multiple variables at once
2. Declaration with Modifiers
unsigned int positiveNum; // Unsigned integer (0 to 4,294,967,295)
short int smallNum; // Short integer (typically 2 bytes)
long int bigNum; // Long integer (typically 4 or 8 bytes)
3. Representation Methods for int Constants
- 1. Decimal representation:
int dec = 42; // Normal decimal - 2. Octal representation (starts with 0):
int oct = 052; // Equals decimal 42 - 3. Hexadecimal representation (starts with 0x):
int hex = 0x2A; // Equals decimal 42 - 4. Long integer suffix:
long int big = 1000000L; // L indicates long type
4. Input and Output of int
1. Formatted Output (printf)
int num = 123;
printf("Decimal: %d\n", num); // 123
printf("Octal: %o\n", num); // 173
printf("Hexadecimal: %x\n", num); // 7b
printf("Unsigned: %u\n", num); // 123
2. Formatted Input (scanf)
int input;
scanf("%d", &input); // Input decimal integer
5. Arithmetic Operations on int
1. Arithmetic Operations
int a = 10, b = 3;
printf("Addition: %d\n", a + b); // 13
printf("Subtraction: %d\n", a - b); // 7
printf("Multiplication: %d\n", a * b); // 30
printf("Division: %d\n", a / b); // 3 (integer division)
printf("Modulus: %d\n", a % b); // 1
2. Increment and Decrement
int n = 5;
n++; // Post-increment (n becomes 6)
++n; // Pre-increment (n becomes 7)
n--; // Post-decrement (n becomes 6)
--n; // Pre-decrement (n becomes 5)
6. Storage Principle of int
Example of binary representation of a 32-bit int:
Decimal: 42
Binary: 00000000 00000000 00000000 00101010
Hexadecimal: 0x0000002A
Negative numbers are represented in two’s complement:
Decimal: -42
Binary: 11111111 11111111 11111111 11010110
7. Typical Application Scenarios of int
- 1. Loop Counter:
for(int i = 0; i < 10; i++) { printf("%d\n", i); } - 2. Array Index:
int arr[5] = {1,2,3,4,5}; int index = 2; printf("%d\n", arr[index]); // 3 - 3. Status Flag:
int isReady = 1; // 1 indicates true, 0 indicates false
8. Common Issues and Precautions
- 1. Integer Overflow:
int max = 2147483647; max = max + 1; // Overflow to -2147483648 - 2. Integer Division Truncation:
int result = 5 / 2; // Result is 2, not 2.5 - 3. Value of Uninitialized Variables:
int uninitialized; // Value is uncertain, could be any number - 4. Type Conversion Issues:
int a = 300; char b = a; // Data may be lost (truncated)
9. Best Practice Recommendations
- 1. Always initialize variables:
int count = 0; // Good habit - 2. Check for arithmetic overflow:
// Check if a + b will overflow if(a > INT_MAX - b) { printf("Possible overflow!"); } - 3. Include limits.h to get extreme values:
#include <limits.h> printf("INT_MAX = %d\n", INT_MAX); - 4. Use type modifiers appropriately:
unsigned int positiveOnly = 100; // Clearly no need for negative numbers
———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author, some content and images are sourced from the internet and AI, please feel free to consume, opinions are for learning reference only~~]

“If you like C, please like it”
Click the bottom right corner to see
“