Day 2.The Basics of C Language
1. supplement of gcc
1)preprocess
gcc -E 2.c -o 2.i
less 2.i
Main functions:
Handle preprocessing instructions starting with #,such as:
#include insert the header file content into the code
#define → Replace Macro definitions
#ifdef / #ifndef → Conditional compilation
Delete comments, expand macros, and generate pure source code files (2.i)
2)compiling
gcc -S 2.c -o 2.s
Main functions:
Translate the c code into assembly code
Check grammar errors and optimize some of the code
Generate the assembly file 2.s (human readable,but close to machine language)
3)assembly
gcc -c 2.c -o 2.o
hexdump 2.o | less
Main function:
Convert the .s assembly file into a target file .o is in binary format and contains machine instructions,but it cannot be run independently yet.
4)linking
gcc 2.c -o a.out
./a.out
Main functions:
Connect multiple object files(.o) and libraries(such as libc)together.
Solve the address reference problem of function calls.
Generate the final executable file(default name :a.out)
5)print
1.run ./a.out on the terminal
2.Load a .out from the hard disk into memory
3.Direct reading and writing between the cpu and memory.Code and data will be executed by the cpu.
4.Eventually,the data will be written to the hard disk.
2. Principle of Computer
1) Basic Data Units
| Unit | Name | Abbr. | Relation | Description |
|---|---|---|---|---|
| bit | Bit | b | Smallest information unit | Represents 0 or 1 |
| byte | Byte | B | 1 B = 8 bit | One byte can store one English letter or half a Chinese character |
2) Computer Storage Units (Binary Measurement System)
Computers use powers of 2 to represent capacity since all operations are based on binary logic.
| Unit | Abbr. | Equivalent Relation | Power of 2 | Pronunciation |
|---|---|---|---|---|
| 1 KB | Kilobyte | 1 KB = 1024 B | 2¹⁰ B | kilo-byte |
| 1 MB | Megabyte | 1 MB = 1024 KB | 2²⁰ B | mega-byte |
| 1 GB | Gigabyte | 1 GB = 1024 MB | 2³⁰ B | giga-byte |
| 1 TB | Terabyte | 1 TB = 1024 GB | 2⁴⁰ B | tera-byte |
| 1 PB | Petabyte | 1 PB = 1024 TB | 2⁵⁰ B | peta-byte |
3) Number System Conversion
| Number System | Digits Used | Rule | Example |
|---|---|---|---|
| Binary | 0, 1 | Carry every 2 | 1, 10, 11, 100… |
| Octal | 0–7 | Carry every 8 | 7, 10, 11, 12… |
| Decimal | 0–9 | Carry every 10 | 9, 10, 11… |
| Hexadecimal | 0–9, a–f | Carry every 16 | 9, a, b, c… |
| Decimal | Binary | Octal | Hexadecimal |
|---|---|---|---|
| 0 | 0000 | 0 | 0 |
| 1 | 0001 | 1 | 1 |
| 2 | 0010 | 2 | 2 |
| 3 | 0011 | 3 | 3 |
| 4 | 0100 | 4 | 4 |
| 5 | 0101 | 5 | 5 |
| 6 | 0110 | 6 | 6 |
| 7 | 0111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | a |
| 11 | 1011 | 13 | b |
| 12 | 1100 | 14 | c |
| 13 | 1101 | 15 | d |
| 14 | 1110 | 16 | e |
| 15 | 1111 | 17 | f |
4)Basic Data Types in C
| Category | Keyword | Description | Size |
|---|---|---|---|
| Integer types | short, int, long | Represent whole numbers | short: 2B, int: 4B, long: 4B or 8B |
| Character type | char | Store a single character (actually stored as integer) | 1B |
| Floating-point types | float, double | Represent decimal numbers | float: 4B, double: 8B |
| Boolean type | _Bool or bool (need <stdbool.h>) | Represent true (1) or false (0) | 1B |
| Void type | void | Indicates no return value or no parameter | None |
In C language:
- short == signed short
- int == signed int
- long == signed long
- Prefixing with unsigned means the variable can only store non-negative integers.
5) Sign Representation: Original Code, One’s Complement, Two’s Complement
| Type | Description |
|---|---|
| Positive number’s complement | Same as its original code |
| Negative number’s complement | Original → Invert bits (one’s complement) → Add 1 |
Notes
- The highest bit is the sign bit
- 0 → positive
- 1 → negative
- The remaining bits store the magnitude.
Example in 8-bit system:
+5 (original) = 0000 0101
-5 (original) = 1000 0101
-5 (one’s complement) = 1111 1010
-5 (two’s complement) = 1111 1011
3. Memory and size
1)common data types in 64-bit operating systems
type integer
| data type | size | numerical range | explaination |
|---|---|---|---|
| unsigned short | 2 | 0 ~ 65,535(2¹⁶ − 1) | all 16 bits are data bits |
| short | 2 | −32,768 ~ 32,767(−2¹⁵ ~ 2¹⁵ − 1) | the most significant bit is the sign bit,15 bits are data bits |
| unsigned int | 4 | 0 ~ 4,294,967,295(2³² − 1) | all 32 bits are data bits |
| int | 4 | −2,147,483,648 ~ 2,147,483,647(−2³¹ ~ 2³¹ − 1) | the most significant bit is the sign bit,31 bits are data bits |
| unsigned long | 8 | 0 ~ 2⁶⁴ − 1 | 64 bits are data bits |
| long | 8 | −2⁶³ ~ 2⁶³ − 1 | the most significant bit is the sign bit,63 bits are data bits |
character types
| data type | space size | numerical range | explaination |
|---|---|---|---|
| char | 1 | −128 ~ 127 | the most significant bit is the sign bit(1 bit),the remaining 7bits are data bits |
| unsigned char | 1 | 0 ~ 255 | 8 bits are data bits |
explanation
-
Chararter types are essentially integer types.
-
Character literals such as ‘a’ and ‘b’ represent individual chararters and can be input from the keyboard(letters ,symbols)
-
The first 32 chararters in the ASCII table are control characters(invisible).
-
example:
'A' → BIN:01000001 → DEC:65 -
command for viewing ASCII table:
man ascii
floating- point type
| data type | space size | numerical range | compositin and structure | significant digits |
|---|---|---|---|---|
| float | 4 | ±3.4 × 10⁻³⁸ ~ ±3.4 × 10³⁸ | sign bit(1 bit) + index(8 bit) + mantissa(23 bit) | 6 ~ 7 bits |
| double | 8 | ±1.07 × 10⁻³⁰⁸ ~ ±1.07 × 10³⁰⁸ | sign bit(1 bit) + index(11 bit) + mantissa(52 bit) | 15 ~ 16 bits |
IEEE 754 standard
- The most significant bit is the sign bit
- The middle part is the index
- The tail is the last digit(significant figure)
void type
| purpose | example |
|---|---|
| a function without a return value | void func(void); |
| untyped pointer | void* p; |
explaination:
- void indicates ‘untyped’
- It is often used for function parameters,return values and generic pointers.
boolean type
| data type | data range | explaination |
|---|---|---|
| _Bool / bool( #include <stdbool.h>) | true (1) / false (0) | logical truth/falsehood |
examples:
#include <stdbool.h>
bool result = 5 < 3; // result = false (0)
4. Constants, Variables, and Expressions
1) Constants
Objects that do not change during program execution and can be used directly in code.
1. Integer Constants
123; // int
-123; // int
0123; // int, octal
0x123; // int, hexadecimal
123u; // unsigned int
123U;
123l; // long
123L;
123ul; // unsigned long
123UL;
2. Floating-Point Constants
3.1415; // double
3.14f; // float
3.14e15; // 3.14 × 10^15
3.14e-15; // 3.14 × 10^-15
3.14E15; // 3.14 × 10^15
3. Character Constants
Represented using single quotes ’ '.
'a';
'b';
'c';
'!';
'#';
' '; // space
'\''; // single quote (escaped)
'\\'; // backslash
'\n'; // newline
'\t'; // tab
'\r'; // carriage return
4. String Constants
Represented using double quotes " ".
In C, every string literal must end with a null terminator ‘\0’.
"hello"; // actually stored as: 'h' 'e' 'l' 'l' 'o' '\0'
"hi\0"; // occupies 3 bytes
"a"; // 'a' '\0'
"hello\0"; // equivalent to "hello"
"hello0"; // includes character '0' (ASCII 48), not '\0'
"hi\055aa"; // octal escape example
2) Defined Constants
Defined using the macro definition directive.
#define PI (3.14)
#define P2 (4.2 + 3)
Macro Definition Notes
-
Improves readability and maintainability — change once, update globally.
-
Always use parentheses around macro expressions to prevent calculation errors:
12 * P2 != 12 * (4.2 + 3); -
Use uppercase names for macros (e.g., MAX_SIZE, PI).
3. Variables
Quantities whose values can change during program execution.
1. Variable Declaration Format
data_type variable_name;
int a;
2. Naming Rules (Identifiers)
-
Must consist of letters, digits, and underscores, and cannot start with a digit.
int a, b, _ab; // valid int 123a; // invalid -
Cannot use keywords or system function names.
-
Use meaningful names:
int sum; int age; int zhangsan_age; int lisi_age;
Each variable occupies memory — the variable name is an alias for that memory space.
4) Expressions
Combinations of variables, constants, or function calls connected by operators.
int num = 20;
1 + 2; // 3
num + 20; // 40
num + num; // 40
Expression Characteristics
- Every expression has a type.
- Every expression has a value (result).
5. Mixed Data Type Operations
1. Implicit Type Conversion
- If operands have the same type → result is of that type.
- If different types → C automatically converts to the higher precision type.
- Floating-point > Integer.
- double > float > long > int > short > char.
- Unsigned types > Signed types.
- char and short are automatically promoted to int.
- float is often promoted to double in expressions.
2. Explicit Type Conversion
Use the type cast operator (type):
int a = 10;
float b = 3.5;
int result = (int)b + a; // forcefully convert b to int
6. Operators
1. Arithmetic Operators
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (remainder) |
| ++ | Increment |
| – | Decrement |

被折叠的 条评论
为什么被折叠?



