# Swap-Function_File-Separation Copyright 2021, [月下麒麟](https://hackmd.io/@YMont/note-catalog) --- ## Purpose 該練習利用自定義swap函式,</br> 使用C語法,實作函式交換兩數字位置,</br> 並且練習引用標頭檔、編譯...等。</br> </br> ex: a=3, b=5 --swap--> a=5, b=3 --- ## 練習目標1 自定義撰寫一個**標頭檔**swap.h ## 練習目標2 自定義撰寫一個**swap**功能在swap.c ## 練習目標3 於main.c上,呼叫swap(),並利用**指標**與**取址**,達成兩數交換的功能 --- ### Complie > gcc -c main.c swap.c </br> > .c **generate** .o ### Output > gcc -o main main.o swap.o </br> > .o **generate** .exe ### Print > a is 5 , b is 4 </br> --- ## Source Code ### swap.h ```c= #ifndef _SWAP_H_ #define _SWAP_H_ void swap(int* , int*); #endif /* swap.h */ ``` ### main.c ```c= #include <stdio.h> #include "swap.h" int main(void){ int a=4, b=5; swap(&a, &b); printf("a is %d , b is %d", a, b); return 0; } ``` ### swap.c ```c= #include "swap.h" void swap(int *a , int *b){ int t = *a; *a = *b; *b = t; } ``` ###### tags: `C` ###### tags: `C learning`