創(chuàng)建一個程序
#include <stidio.h>
int main (void)
{
printf(" Hello World ! " );
return 0;
}
C語言的源代碼的擴展名是 .c
C++的擴展名是 .cpp
編輯一個程序
#include <stdio.h>
int main(void)
{
printf("\"If at first you don't succeed, try, try, try );
return 0;
}
注釋
位于/* 和 /之間的任意文本都是注釋
//后面的內容也是注釋,這是兩種記號,兩種方式
/
- Written by Ivor Horton
- Copyright 2012
*/
也可以修飾注釋,使它們比較突出
/********************************************* *
- This is a vary important comment *
- so please read this. *
********************************************** */
// 是行注釋
/* / 是塊注釋
行注釋 說明 本行 // 之后的所有內容都是注釋
/ 是一個整體
*/ 也是一個整體
就像是一對括號
括號里面的都是注釋
- 抄課本跟自己理解之后區(qū)別
預處理指令(preprocessing directive)
常見的標準庫頭文件
math.h ctype.h stdbool.h stdio.h string.h
定義main()函數(shù)
是兩個括號之間執(zhí)行某組操作的一段代碼
每個 c 程序都必須有一個main()函數(shù)
關鍵字int表示main()函數(shù)的返回值的類型
關鍵字
int char double long short signed unsigned sizeof const auto for else default do while if goto switch case break return (常用到的)
函數(shù)體
在函數(shù)名稱后面位于起始及結束的兩個大括號之間的代碼塊
輸出信息
printf()是一個標準的庫函數(shù)
參數(shù)
包含在函數(shù)名后的圓括號內的項稱為參數(shù),它指定要傳送給函數(shù)的數(shù)據(jù)。
三字母序列
控制符
1.4
//program 1.4 Another Simple C program - Displaying a Quotation
#include <stdio.h>
int main (void)
{
printf("My formula for success?\nRise early, work late, strike oil.\n");
return 0;
} 準則 早起 工作晚
1.5
#include <stdio.h>
int main(void)
{
printf("\"It is a wise father that knows his own child.\"\nShakespears\n");
return 0;
}
1.6
#include<stdio.h>
int main(void)
{
printf("\"It is a wise father that knows his own child.\"\nShakespears\n");
return 0;
}
1.7
#include <stdio.h>
int main (void)
{
printf("Be careful!!\n\a");
return 0;
}
試試看:將所學的知識用于實踐
下面的例子將前面學到的知識用于實踐。首先,看看下面的代碼,檢查自己是否理解它的作用。然后輸入這些代碼,編譯、鏈接并執(zhí)行,看看會發(fā)生什么。
#include <stdio.h>
int main (void)
{
printf(" Hi there !\n\n\nThis program is a bit");
printf(" longer than the others.");
printf(" \nBut really it's only more text.\n\n\n\a\a");
printf("Hey,wait a minute!! What was that???\n\n");
printf("\t1.\tA bird ?\n");
printf("\t2.\tA plane?\n");
printf("\t3.\tA control character?\n");
printf("\n\t\t\b\bAnd how will this look when it prints out?\n\n");
return 0;
}
標準庫函數(shù)printf()
空兩行是由3個轉義序列\(zhòng)n生成的
習題1. 1 編寫一個程序,用兩個printf()語句分別輸出自己的自己的名字及地址。
習題1. 2 將上一個練習改成所有的輸出只用一個printf()語句。
習題1. 3 編寫一個程序,輸出下列文本,格式如下所示:
"It's freezing in here," he said coldly.