標籤:style blog http io color ar os 使用 sp
1.前置程式
#include<stdio.h>#include<string.h> //1提供strlen()的函數原型#define DENSITY 62.4 //2預先處理命令int main(void){ float weight,volume; int size,letters; char name[40]; //3定義一個長度為40的數組 printf("Hi! What‘s your first name?\n"); scanf("%s",name); printf("%s,What‘s your weight in pounds?\n",name); scanf("%f",&weight); size=sizeof name; //4 數組name[]的長度 letters=strlen(name);//5 調用函數strlen() volume=weight/DENSITY; printf("well,%s,your volume is %2.2f cubic feet.\n",name,volume);//%2.2f表示字元寬度為2,精確到小數點後兩位 printf("Also,your first name has %d letters,\n",letters); printf("and we have %d bytes to store it in.\n",size); return 0;}
View Code
2.關於字串
(1)字串是一個或多個字元的序列。如"I am a student!"。
(2)C語言用Null 字元來標記一個字串的結束。數組的單元數必須至少比要儲存的字元數多1。
(3)字串和字元。‘x‘和"x"的區別(後者是一個字串由‘x‘和‘\0‘組成)。
(4)Sizeof()和strlen()函數。
- 同一個字串,sizeof()把標誌字串結尾的Null 字元計算在內,長度比strlen()大1.
- strlen()以字元為單位給出字串的長度。sizeof()給出數組的長度(分配的記憶體單元)。
- 擷取一個類型大大小~擷取一個具體量的大小。sizeof(char)和sizeof (name)=sizeof name。
sizeof()和strlen() #include<stdio.h>#include<string.h>#define PRAISE "What a super marvelous name!"int main(void){ char name[40]; printf("What‘s your name?\n"); scanf("%s",name); printf("Hello,%s.%s\n",name,PRAISE); printf("Your name of %d letrers occupies %d memory cells.\n", strlen(name),sizeof(name));//sizeof name printf("The phrase of praise has %d letters", strlen(PRAISE)); printf("and occupies %d memory cells.\n",sizeof(PRAISE));//sizeof PRAISE return 0;}
sizeof()和strlen()
3.常量和C前置處理器
(1)常量如0.015。float taxrate=0.015。把常量0.015賦值給變數taxrate,但程式可能意外的改變它的值。
(2)兩種方法const修飾符和#define預先處理命令
- const int MONTHS=12;
- #define MONTHS +12;(#define TEE ‘T‘)(#define OOPS "Now you have it!")
4.printf()函數
(1)printf():(“控制描述"+變數列表)~(變數使用的是值,無論該值是變數、常量、還是運算式)。
(2)printf()轉換說明符:%c--一個字元、%d--有符號十進位整數、%e--浮點數e記數法、%、f--浮點數十進位、%p--指標、%%--列印一個%、%s--字串...:
(3)printf()標誌符:-(靠左對齊)、+(帶符號)、#(...)、0(對所有數字格式,用前置0填充欄位寬度)
- 列印一個字串的前8個字元,欄位寬度為8字元(%8.8s)
- 列印雙引號\"....\"
- 列印一個欄位寬度在參數列表中給定的八進位整數(%*0).
- %5d(00006)
- 指定固定欄位寬度(有效防止溢出)
(4)用printf()列印較長的字串
a.採用多個printf()函數;
b.在一個printf()中採用(\)和斷行符號鍵
c.採用字串串連方法("Hello""world")
printf()列印較長字串#include<stdio.h>int main(void){ printf("Here‘s one way to print a "); printf("long string.\n");//a printf("Here‘s another way to print a \long string.\n");//b printf("Here‘s the newest way to print a " "long string.\n");//c return 0;}
printf()列印較長字串
(5)printf()的函數傳回值(返回所列印字元的數目,如果輸出有誤則返回-1,常用於檢查輸出錯誤。向檔案中而非螢幕)
printf()的傳回值#include<stdio.h>int main(void){ int bph2o=212; int rv; rv=printf("%d F is water‘s boiling point.\n",bph2o); printf("The printf()function printed %d characters.\n",rv); return 0;}
printf()的傳回值
5.scanf()函數
(1)scanf()會在遇到第一個空白字元空格、定位字元、或者分行符號處停止讀取。~gets()函數可以用來讀取一個字串。
(2)讀取變數類型的值加&,把字串讀進一個字元數組不使用&。
(3)scanf("%d,%d",&n,&m)接受輸入 1,2 {scanf("%c",&ch)讀取在輸入中遇到的第一個字元}
6.關於修飾符*
使用可變寬度的輸出欄位#include<stdio.h>int main(void){ unsigned width,precision; int number=256; double weight=242.5; printf("What field width?\n"); scanf("%d",&width); printf("The number is :%*d:\n",width,number); printf("Now enter a width and a precision:\n"); scanf("%d%d",&width,&precision); printf("Weight=%*.*f\n",width,precision,weight); return 0;}
使用可變寬度的輸出欄位
- scanf()中*使函數跳過相應的輸出項目(scanf("%*d%*d%d",&n);前兩個跳過)
C 字串和格式化輸入與輸出