c語言printf函數(shù)
#define _CRT_SECURE_NO_WARNINGS // visral studio使用標(biāo)準(zhǔn)c某些函數(shù)時(shí)需要做此設(shè)置 定義常量_CRT_SECURE_NO_WARNINGS
#define DENSITY 62.4 // 定義常量DENSITY為62.4
#include <stdio.h>
void jolly(void); // 函數(shù)原型prototype ,告知編譯器在程序中要使用該函數(shù),頭文件中也存放的如printf()等函數(shù)的函數(shù)原型,具體的函數(shù)定義在main函數(shù)后面
void deny(void); // 如果不聲明函數(shù)原型,在程序執(zhí)行時(shí)可能報(bào)錯(cuò)
int main(void)
{
int age; //定義int整型變量age,c推薦將所有定義放在函數(shù)體內(nèi)前面,變量命名可使用數(shù)字、字母、下劃線,不能以數(shù)字開頭,以下劃線開頭通常為操作系統(tǒng)和C庫的標(biāo)識符,
int toes = 10; // 在定義變量toes時(shí)直接賦值,int類型為有符號整數(shù),至少占2字節(jié)/16bit
? int a, b; //同時(shí)定義多個(gè)同類型變量,用逗號隔開,如果寫成 int a, b = 3;則只會給b賦值3
printf("Yu\n"); // 轉(zhuǎn)義字符\n換行符,使光標(biāo)移至下一行首位
printf("Gan\n");
printf("Please\t"); //沒有輸入\n換行符,光標(biāo)停留在字符串的末尾,下一個(gè)字符會從末尾繼續(xù)顯示,\t制表符
printf("enter your age:\n"); // 兩次輸入在同一行
scanf("%d", &age); // scanf()格式化輸入,scan掃描,%d轉(zhuǎn)換說明(conversion specification)digit數(shù)字,指定將接收到的輸入內(nèi)容作為十進(jìn)制整數(shù)decimal,賦值給變量age
printf("%d age = %d days\n", age, age * 365); // printf(格式字符串,參數(shù)列表),格式字符串中使用轉(zhuǎn)換說明的數(shù)量應(yīng)和后面?zhèn)魅氲膮?shù)數(shù)量、順序一致
printf("toes = %d\ttoes^2 = %d\ttoes^3 = %d\n", toes, toes * toes, toes * toes * toes); // printf()和scanf()參數(shù)數(shù)量可變,根據(jù)格式字符串中轉(zhuǎn)換說明的數(shù)量傳入對應(yīng)的參數(shù)
/* 結(jié)果
Yu
Gan
Please enter your age:
29
29 age = 10585 days
toes = 10? ? ? ?toes^2 = 100? ? toes^3 = 1000
*/
jolly(); // 調(diào)用定義的函數(shù)jolly(),具體定義在后面,函數(shù)執(zhí)行結(jié)束后,控制權(quán)被返回至主調(diào)函數(shù),這里是通過main()函數(shù)調(diào)用的jolly()函數(shù),所以主調(diào)函數(shù)是main()
jolly();
jolly();
deny(); //jolly()和deny()函數(shù)都只打印一句字符串,結(jié)果為輸出了四行內(nèi)容
return 0;
}
void jolly(void) //void無返回值 jolly(void)無參數(shù) 該函數(shù)只打印一句字符串
{
printf("For he's a jolly good fellow!\n");
}
void deny(void)
{
printf("Which nobody can deny!\n");
}