天天看點

C語言快速講解(七)預編譯

前言:作為一個安卓程式員,如果不懂C/C++開發,那麼安卓jni、NDK、視訊解碼、音頻解碼也沒法開發,有需求我們就要學習,加油吧!今天開始我們快速講解C語言,有Java基礎的人适合看本部落格。

-----------分割線---------

我們先來了解一下C語言執行的大緻流程:編譯->連接配接->執行。

編譯:形成目标代碼(.obj);

連接配接:将目标代碼與C函數庫連接配接合并,形成最終的可執行檔案;

執行:執行exe檔案;

-------------分割線-------

include:完成代碼文本的替換工作。

首先在頭檔案建立一個hello.txt檔案。

C語言快速講解(七)預編譯

内容是:

C語言快速講解(七)預編譯

然後在源檔案中引入這個hello.txt,完成代碼文本的替換工作:

#include <stdlib.h>
#include <stdio.h>
void main(){
	#include "hello.txt"
	getchar();
}
           

運作顯示:

C語言快速講解(七)預編譯

--------分割線-------

include定義函數聲明并實作:

在頭檔案建立一個myhead.h檔案,在裡面聲明一個函數:

C語言快速講解(七)預編譯
void say();//聲明函數
           

然後在源檔案裡面建立一個02.c在裡面實作具體的函數内容

C語言快速講解(七)預編譯
void say(){
	printf("hello");
}
           

然後在源檔案01.c中引入myhead.h檔案,然後在main中調用say函數:

#include <stdlib.h>
#include <stdio.h>
#include "myhead.h"//我們定義頭檔案

void main(){
	say();
	getchar();
}
           

運作顯示:

C語言快速講解(七)預編譯

--------分割線--------

include循環嵌套:何謂是循環嵌套,簡單了解就是我們在A裡面引入B的東西,然後又在B裡面引入A的東西,然後我們在主程式中,隻需要引入一個A就可以使用A和B的函數,有點類似于java中的繼承。

ok我們建立一個A.h在頭檔案中:

#ifndef AH
#define AH
#include "B.h"

void printfA();

#endif
           

然後在建立一個B.h在頭檔案中:

#ifndef BH
#define BH
#include "A.h"

void printfB();

#endif
           

當然你在最新的編譯器中B.h也可以這樣寫(A.h同樣):

#pragma once
#include "A.h"

void printfB();
           

然後在源檔案02.c中實作A.h和B.h的兩個函數:

void printfA(){
	printf("print A\n");
}

void printfB(){
	printf("print B\n");
}
           

最後在源檔案01.c中引入A.h,在main中調用兩個函數:

#include <stdlib.h>
#include <stdio.h>
#include "A.h"

void main(){
	printfA();
	printfB();
	getchar();
}
           

運作顯示:

C語言快速講解(七)預編譯

------------分割線-----------------

define替換:

#include <stdlib.h>
#include <stdio.h>
#define MAX 1000
void main(){
	int a = MAX;
	printf("%d\n",a);
	getchar();
}
           

運作顯示:

C語言快速講解(七)預編譯

------------分割線---------

ifdef:上面A.h和B.h中提到了ifdef翻譯起來就是如果....沒有...。來看具體案例:

在01.c中添加如下代碼:

#include <stdio.h>

void main(){

#ifdef __cplusplus
	printf("目前是c++語言");
#else
	printf("目前是c語言");
#endif
	getchar();
}
           

運作:

C語言快速講解(七)預編譯

ok注釋掉此代碼然後在源檔案中建立03.cpp,添加如下代碼:

#include <stdio.h>

void main(){

#ifdef __cplusplus
	printf("目前是c++語言");
#else
	printf("目前是c語言");
#endif
	getchar();
}
           

運作顯示:

C語言快速講解(七)預編譯

--------分割線-------

宏函數1:

//宏函數
void fly_c_add(){
	printf("add\n");
}

void fly_c_div(){
	printf("div\n");
}
//NAME是參數
#define fun(NAME) fly_c_##NAME();

void main(){
	//fly_c_add(10,20);
	fun(add);
	getchar();
}
           

運作:

C語言快速講解(七)預編譯

宏函數2:

void foo(char *str) {
	printf("%s\n", str);
}

void foo2(char *str1, char *str2) {
	printf("%s, %s\n", str1, str2);
}

#define call_optimized(function, arguments) {\
	printf("%s\n", #function); \
	function arguments; \
}

void main() {
	call_optimized(foo, ("hell world"));
	//call_optimized(foo2, ("hell", "world"));
	system("pause");
}
           

運作:

C語言快速講解(七)預編譯

## 連接配接符: 功能是在帶參數的宏定義中将兩個子串(token)聯接起來,進而形成一個新的子串。或者了解為##是将宏定義的多個形參連接配接成一個實際的參數名。

# 符号:連接配接符是把傳遞過來的參數當成字元串進行替代。其隻能用于有傳入參數的宏定義中,且必須置于宏定義體中的參數名前。 

---------完---------