創(chuàng)建一個(gè)程序
#include <stidio.h>
int main (void)
{
printf(" Hello World ! " );
return 0;
}
C語言的源代碼的擴(kuò)展名是 .c
C++的擴(kuò)展名是 .cpp
編輯一個(gè)程序
#include <stdio.h>
int main(void)
{
printf("\"If at first you don't succeed, try, try, try );
return 0;
}
注釋
位于/* 和 /之間的任意文本都是注釋
//后面的內(nèi)容也是注釋违寞,這是兩種記號,兩種方式
/
- Written by Ivor Horton
- Copyright 2012
*/
也可以修飾注釋算撮,使它們比較突出
/********************************************* *
- This is a vary important comment *
- so please read this. *
********************************************** */
// 是行注釋
/* / 是塊注釋
行注釋 說明 本行 // 之后的所有內(nèi)容都是注釋
/ 是一個(gè)整體
*/ 也是一個(gè)整體
就像是一對括號
括號里面的都是注釋
- 抄課本跟自己理解之后區(qū)別
預(yù)處理指令(preprocessing directive)
常見的標(biāo)準(zhǔn)庫頭文件
math.h ctype.h stdbool.h stdio.h string.h
定義main()函數(shù)
是兩個(gè)括號之間執(zhí)行某組操作的一段代碼
每個(gè) c 程序都必須有一個(gè)main()函數(shù)
關(guān)鍵字int表示main()函數(shù)的返回值的類型
關(guān)鍵字
int char double long short signed unsigned sizeof const auto for else default do while if goto switch case break return (常用到的)
函數(shù)體
在函數(shù)名稱后面位于起始及結(jié)束的兩個(gè)大括號之間的代碼塊
輸出信息
printf()是一個(gè)標(biāo)準(zhǔn)的庫函數(shù)
參數(shù)
包含在函數(shù)名后的圓括號內(nèi)的項(xiàng)稱為參數(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;
} 準(zhǔn)則 早起 工作晚
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;
}
試試看:將所學(xué)的知識用于實(shí)踐
下面的例子將前面學(xué)到的知識用于實(shí)踐蛉幸。首先张抄,看看下面的代碼革半,檢查自己是否理解它的作用碑定。然后輸入這些代碼,編譯又官、鏈接并執(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;
}
標(biāo)準(zhǔn)庫函數(shù)printf()
空兩行是由3個(gè)轉(zhuǎn)義序列\(zhòng)n生成的
習(xí)題1. 1 編寫一個(gè)程序赏胚,用兩個(gè)printf()語句分別輸出自己的自己的名字及地址访娶。
習(xí)題1. 2 將上一個(gè)練習(xí)改成所有的輸出只用一個(gè)printf()語句。
習(xí)題1. 3 編寫一個(gè)程序觉阅,輸出下列文本崖疤,格式如下所示:
"It's freezing in here," he said coldly.