在上一篇文章中我們創建了第一個專案 Hello World ,專案是創建好了,但還不知道這個 Hello World 是個啥玩意?到底怎么運行?

一.Hello World原始碼
我們將hello world.cpp的內容修改如下如下:
// hello world.cpp : 定義控制臺應用程式的入口點,
//
#include "stdafx.h"
#include <stdio.h>
int main(void)
{
printf("HelloWorld!");
return 0;
}
1.#include – 前處理器指令
前處理器發現#include指令后就會去尋找 #include <> 或者 #include “”里的檔案名,詳細文章介紹請參考: #include <> 與 #include “” 區別
//寫法一:推薦 #include <stdio.h> //寫法二:不推薦 #include "stdio.h"
#include <stdio.h> 會直接在系統目錄搜索 stdio.h ,如果系統目錄也搜索不到,直接報錯:No such file or directory!
#include “stdio.h” 首先在工程目錄搜索 stdio.h ,如果工程目錄搜索不到,會繼續在系統目錄搜索 stdio.h ,如果系統目錄也搜索不到,直接報錯:No such file or directory!
對比可以發現:雖然第一種寫法和第二種寫法效果一樣,但是第二種查找頭檔案的時候更耗時間,所以,系統的頭檔案推薦使用第一種寫法!
2.main函式 – 入口函式
main 函式是 C語言 程式的入口函式,必不可少,程式沒有 main 函式就好比人不喝水,不吃飯!(強行記憶)

3.printf函式
printf是頭檔案 stdio.h 里面的一個函式,只有包含了 stdio.h 才能使用,當前printf是在控制臺上格式輸出一條資訊,當前輸出的內容是 HelloWorld! ,所以在控制臺能看到一條 HelloWorld! 陳述句,該函式的使用會繼續在后面的文章講解,

4.return 0
return 意味著 mian 函式結束;main 函式是C語言的主函式,主函式結束,整個程式結束!Game Over!
二.Visual Studio 運行生成專案
代碼有了,說了一天 Hello World 結果毛都沒看到一個,如何使用 Visual Studio 編譯代碼生成 exe 可以執行檔案呢?
1.使用快捷鍵 Ctrl + F5

2.點擊 本地Windows除錯器

點擊 綠色 的三角形按鈕,結果發現一個黑視窗一閃而過,這個是表示代碼執行結束了return 0 了;

你也可以找到工程檔案夾下面有一個 debug 檔案夾,里面有剛剛生成的 hello world.exe,直接使用cmd命令運行也能看到最終效果:

猜你喜歡:
1.安裝 Visual Studio 2015
2.安裝 Visual Studio 插件 Visual Assist
3.設定 Visual Studio字體/背景/行號
4.徹底卸載 Visual Studio 2008
5.徹底卸載 Visual Studio 2013/2015
轉載請注明:猿說編程 ? C語言教程 ? Hello World C語言入門
技術交流、商務合作請直接聯系博主
掃碼或搜索:猿說編程

猿說編程
微信公眾號 掃一掃關注
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/20798.html
標籤:C
上一篇:小白請教!在矩形圖片上畫圓!
下一篇:6.C語言代碼注釋
