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

UnitNameAbbr.RelationDescription
bitBitbSmallest information unitRepresents 0 or 1
byteByteB1 B = 8 bitOne 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.

UnitAbbr.Equivalent RelationPower of 2Pronunciation
1 KBKilobyte1 KB = 1024 B2¹⁰ Bkilo-byte
1 MBMegabyte1 MB = 1024 KB2²⁰ Bmega-byte
1 GBGigabyte1 GB = 1024 MB2³⁰ Bgiga-byte
1 TBTerabyte1 TB = 1024 GB2⁴⁰ Btera-byte
1 PBPetabyte1 PB = 1024 TB2⁵⁰ Bpeta-byte

3) Number System Conversion

Number SystemDigits UsedRuleExample
Binary0, 1Carry every 21, 10, 11, 100…
Octal0–7Carry every 87, 10, 11, 12…
Decimal0–9Carry every 109, 10, 11…
Hexadecimal0–9, a–fCarry every 169, a, b, c…
DecimalBinaryOctalHexadecimal
0000000
1000111
2001022
3001133
4010044
5010155
6011066
7011177
81000108
91001119
10101012a
11101113b
12110014c
13110115d
14111016e
15111117f

4)Basic Data Types in C

CategoryKeywordDescriptionSize
Integer typesshort, int, longRepresent whole numbersshort: 2B, int: 4B, long: 4B or 8B
Character typecharStore a single character (actually stored as integer)1B
Floating-point typesfloat, doubleRepresent decimal numbersfloat: 4B, double: 8B
Boolean type_Bool or bool (need <stdbool.h>)Represent true (1) or false (0)1B
Void typevoidIndicates no return value or no parameterNone

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

TypeDescription
Positive number’s complementSame as its original code
Negative number’s complementOriginal → 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 typesizenumerical rangeexplaination
unsigned short20 ~ 65,535(2¹⁶ − 1)all 16 bits are data bits
short2−32,768 ~ 32,767(−2¹⁵ ~ 2¹⁵ − 1)the most significant bit is the sign bit,15 bits are data bits
unsigned int40 ~ 4,294,967,295(2³² − 1)all 32 bits are data bits
int4−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 long80 ~ 2⁶⁴ − 164 bits are data bits
long8−2⁶³ ~ 2⁶³ − 1the most significant bit is the sign bit,63 bits are data bits

character types

data typespace sizenumerical rangeexplaination
char1−128 ~ 127the most significant bit is the sign bit(1 bit),the remaining 7bits are data bits
unsigned char10 ~ 2558 bits are data bits
explanation
  1. Chararter types are essentially integer types.

  2. Character literals such as ‘a’ and ‘b’ represent individual chararters and can be input from the keyboard(letters ,symbols)

  3. The first 32 chararters in the ASCII table are control characters(invisible).

  4. example:

    'A' → BIN:01000001 → DEC:65
    
  5. command for viewing ASCII table:

    man ascii
    

floating- point type

data typespace sizenumerical rangecompositin and structuresignificant digits
float4±3.4 × 10⁻³⁸ ~ ±3.4 × 10³⁸sign bit(1 bit) + index(8 bit) + mantissa(23 bit)6 ~ 7 bits
double8±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

purposeexample
a function without a return valuevoid func(void);
untyped pointervoid* p;

explaination:

  1. void indicates ‘untyped’
  2. It is often used for function parameters,return values and generic pointers.

boolean type

data typedata rangeexplaination
_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

  1. Improves readability and maintainability — change once, update globally.

  2. Always use parentheses around macro expressions to prevent calculation errors:

    12 * P2 != 12 * (4.2 + 3);
    
  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)

  1. Must consist of letters, digits, and underscores, and cannot start with a digit.

    int a, b, _ab;   // valid
    int 123a;        // invalid
    
  2. Cannot use keywords or system function names.

  3. 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

  1. Every expression has a type.
  2. 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

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder)
++Increment
Decrement
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值