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"优化成一个地方。
}