Pick C語言之重新來過2

Pick C語言之重新來過2

來自專欄 阿飯說

4.1 分析並寫出下面程序運行的結果

(1)

#include<stdio.h>#include<math.h>main(){ char c1 =a,c2 = b,c3 = c; printf("a%cb%cc%c
",c1,c2,c3);}

結果:

(2)

include<stdio.h>#include<math.h>main(){ char a =12,b = 15; printf("a=%d%%,b=%d%%
",a,b);}

結果:

阿飯說:

%%為輸入一個%

(3)

#include<stdio.h>#include<math.h>main(){ int a,b; scanf("%2d%*2s%2d",&a,&b); printf("%d,%d
",a,b);}

結果:

阿飯說:

輸入123456後,34被認為是分隔符,所以出現了12、56。

4.2改正錯誤

#include<stdio.h>#include<math.h>main(){ long a,b; float x,y; scanf("%d,%d
",&a,&b); scanf("%f,%f
",&x,&y); printf("a = %d,b = %d
",a,b); printf("x = %f,y = %f
",x,y);}

4.3填空題

(1)

#include<stdio.h>#include<math.h>main(){ char a,b; int c; scanf("%c%c%d",&a,&b,&c); printf("%c,%c,%d
",a,b,c);}

結果:

(2)

#include<stdio.h>#include<math.h>main(){ char a,b; int c; scanf("%c%c%d",&a,&b,&c); printf("%-2c,%-2c,%d
",a,b,c);}

結果:

阿飯說:

M %md 以寬度m輸出整型數,不足m時,左補空格 (-M則是右補空格)

0m %0md 以寬度m輸出整型數,不足m時,左補零

m,n %m.nf 以寬度m輸出實型小數,小數位為n位

(3)

#include<stdio.h>#include<math.h>main(){ char a,b; int c; scanf("%c,%c,%d",&a,&b,&c); printf("%c,%c,%d
",a,b,c);}

結果:

(4)

#include<stdio.h>#include<math.h>main(){ char a,b; int c; scanf("%c,%c,%d",&a,&b,&c); printf("\%c,\%c,\%d\n",a,b,c);}

結果:

(5)

#include<stdio.h>#include<math.h>main(){ char a,b; int c; scanf("%c%*c%c%*c%d",&a,&b,&c); printf("%c,%2c,%d
",a,b,c);}

結果:

阿飯說:

忽略輸入修飾符使得對應的輸入項在讀入後不賦給任何變數,因此無論用戶輸入什麼都不會對其對應的輸入項(即分隔符)產生影響,也就意味著用戶可以用任意字元作為分隔符來輸入數據。

實驗題:小寫字母轉換大寫並將其輸出還有ASCII碼。

#include<stdio.h>#include<math.h>main(){ char a; scanf("%c",&a); a =a-32; printf(" 大寫字母 = %c
",a); printf(" 大寫字母ASCII = %d",a);}

結果:


推薦閱讀:

TAG:C語言設計習題 | 零基礎學C語言書籍 |