C語言編譯過程
gcc 編譯參數
源文件
a.c
#include<stdio.h>
int main(){
// 九九乘法表
int i,j;
for(i=1; i<=9; i++) {
for(j=1; j<=i; j++) {
printf("%dx%d=%2d ", j, i, i*j);
}
printf("\n");
}
}
預編譯
預編譯將#include包含的頭文件內容替換到C文件中,同時刪除代碼中的註釋部分
gcc -E -o a.e a.c
a.e 部分內容已刪除
typedef unsigned char __u_char;
typedef unsigned short int __u_short;
typedef unsigned int __u_int;
typedef unsigned long int __u_long;
typedef signed char __int8_t;
typedef unsigned char __uint8_t;
typedef signed short int __int16_t;
typedef unsigned short int __uint16_t;
typedef signed int __int32_t;
typedef unsigned int __uint32_t;
extern int fprintf (FILE *__restrict __stream,
const char *__restrict __format, ...);
extern int printf (const char *__restrict __format, ...);
extern int sprintf (char *__restrict __s,
const char *__restrict __format, ...) __attribute__ ((__nothrow__));
int main(){
int i,j;
for(i=1; i<=9; i++) {
for(j=1; j<=i; j++) {
printf("%dx%d=%2d ", j, i, i*j);
}
printf("\n");
}
}
變量分配
int a = 0; 全局初始化區
char *p1; 全局未初始化區
main() {
int b; 棧
char s[] = "abc"; 棧
char *p2; 棧
char *p3 = "123456"; 123456\0在常量區,p3在棧上。
static int c =0; 全局(靜態)初始化區
p1 = (char *)malloc(10);
p2 = (char *)malloc(20); 分配得來得10和20字節的區域就在堆區。
strcpy(p1, "123456"); 123456\0放在常量區,編譯器可能會將它與p3所指向的"123456"優化成一個地方。
}