文章目錄
- 同步GitHub在此 👉 [https://github.com/TeFuirnever/GXL-Skill-Tree](https://github.com/TeFuirnever/GXL-Skill-Tree)
- 代碼講解
- 代碼運行
- 1)Linux下
- 2)Windows 下
- gcc編譯
- system函式
- 實體1
- 實體2
- 實體3
- VS2013 C4996解決方法
同步GitHub在此 👉 https://github.com/TeFuirnever/GXL-Skill-Tree
代碼講解
如圖即一個簡單的 hello world 程式,

// 1、使用某個函式前,需要包含相應的頭檔案
// 2、可以通過man手冊查詢或者其他資料查詢
// 3、頭檔案類似于選單,頭檔案包含函式的宣告,相當于選單例舉了菜名,函式呼叫,相當于點菜
// 4、<>通過包含系統的頭檔案(標準的頭檔案),""包含自定義的頭檔案
#include <stdio.h>
// 1、C語言由函陣列成,有且僅有一個主函式
// 2、程式運行,先從main函式運行
// 3、return 0,程式正常結束
int main()
{
// 注釋:不是有效代碼
// 1、行注釋, //相應的注釋
// 2、塊注釋,/* 相應的注釋 */
printf("hello world\n");
// 1、這是一個C代碼
// 2、函式呼叫,printf功能往標準輸出設備(螢屏)上列印內容
// 3、\n代表換行
return 0;
}
頭檔案目錄:vi /usr/include/stdio.h

代碼運行
在上一個博客中(LinuxC++開發面試系列(二):權限修改、行程管理與vim)中我們給出了出版本代碼 hello world,接下來,我們將就 Linux 和 Windows 兩個環境下進行程式的編譯,
1)Linux下
運行編譯的可執行程式,
0、切換目錄,cd 即可
1、ls 查看目錄資訊
2、
- gcc hello.c ,默認在當前路徑生成 a.exe
- gcc hello.c -o hello 生成 hello.exe
3、運行,在Linux下運行,當前路徑,前面必須加 ./;非當前路徑,寫上完整路徑即可

2)Windows 下
運行編譯的可執行程式,
0、切換盤符,無需 cd
1、cd 目錄
2、dir 查看目錄資訊
3、
- gcc hello.c ,默認在當前路徑生成 a.exe
- gcc hello.c -o hello 生成 hello.exe
4、運行,在Windows下運行,無需./

gcc編譯
C程式編譯步驟:
1、預處理:gcc -E hello.c -o hello.i

2、編譯: gcc -S hello.i -o hello.s

3、匯編: gcc -c hello.s -o hello.o

4、鏈接: gcc hello.o -o hello

5、運行


Linux查看需要鏈接的動態庫:ldd

Windows查看支持的動態庫:Depends


system函式
system函式:
int system(const char *command);
實體1
01_test.c:

#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("before sys\n");
// 1、需要頭檔案 #include <stdlib.h>
// 2、system功能:呼叫外部程式
system("ls -alh");
printf("after sys\n");
return 0;
}

實體2

02_waibu.c:
#include <stdio.h>
int main()
{
printf("我是小鮮肉,假的\n");
printf("我是外部程式\n");
return 0;
}

03_system.c:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("before sys\n");
// 1、需要頭檔案 #include <stdlib.h>
// 2、system功能:呼叫外部程式
system("./waibu");
printf("after sys\n");
return 0;
}


字符編碼:
1、Windows默認支持的中文編碼為gbk,gb2312,ANSI
2、Linux默認支持的中文編碼為utf-8(unicode)
實體3
calc 計算器
vim:

vscode:

#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("before sys\n");
system("calc");
printf("after sys\n");
return 0;
}
在Linux下無效:

只在Windows下有效:

VS2013 C4996解決方法
由于微軟在 VS2013 中不建議再使用c的傳統庫函式scanf,strcpy,sprintf等,所以直接使用這些庫函式會提示C4996錯誤!
法一:
把這個宏定義放到.c檔案的第一行
#define _CRT_SECURE_NO_WARNINGS
法二:
把這個代碼放在主函式的任意一行
#pragma warning(disable:4996)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/287096.html
標籤:其他
