c語言之字符串和格式化輸入輸出
阿新 • • 發佈:2019-03-24
width 打印 不想 weather 指數 als turn 常量 div
字符串和格式化輸入輸出
1 #include<stdio.h> 2 #include<string.h> 3 #define DENSITY 62.4 4 5 int main(void) 6 { 7 float weight, volume; 8 int size, letters; 9 char name[40];//數組 10 11 printf("Hi!What‘s your first name?"); 12 gets(name);//get(sth.)取得地址 13 printf("%s,What‘s your weight in pounds?\n", name); 14 scanf_s("%f", &weight); 15 size = sizeof(name); 16 letters = strlen(name); 17 volume = weight / DENSITY; 18 printf("Well,%s, your volume is %2.2f cubic feet.\n", name, volume); 19 printf("Also, your fist name has %d letters\n", letters); 20 printf("We have %d bytes to store it in.\n", size); 21 return 0; 22 }
字符串
用雙引號表示,且C語言沒有專門的字符串變量類型,而是把它儲存在char數組裏面。數組的最後一個位置顯示空字符\0,用於標記字符串的結束。如 "The weather is so well!"
1 #include<stdio.h> 2 #define PRAISE "You are so good!" 3 4 int main(void) 5 { 6char name[40]; 7 printf("What‘s your name?"); 8 gets(name); 9 printf("Hello,%s,%s", name, PRAISE); 10 getchar(); 11 return 0; 12 }
字符串和字符的區別
- ‘x‘是基本類型,而"x"是派生類型(char數組);
- "x"實際上是由‘x‘和空字符兩部分組成的。
strlen函數
1 #include<stdio.h> 2 #define PRAISE "You are so good!" 3 4 int main(void) 5 { 6 char name[40]; 7 printf("What‘s your name?"); 8 gets(name); 9 printf("Hello,%s,%s", name, PRAISE); 10 printf("Your name of %zd letters occupies %zd memory cells.\n", strlen(name), sizeof name); 11 /*strlen函數給出字符數,sizeof為給出所占內存數量;但是兩者都需要使用"%zd"轉換符來打印。另外sizeof(特定量),如sizeof(char),而一般的類型,不使用圓括號也可以。*/ 12 printf("The phraze of PRAISE has %zd letters", strlen(PRAISE)); 13 printf(" and occupies %zd memory cells.\n", sizeof PRAISE); 14 getchar(); 15 return 0; 16 }
這樣在程序運行時,所有的NAME將會被value替代,這樣定義的常量也稱為明示常量。
1 #include<stdio.h> 2 #define PI 3.14 3 4 int main(void) 5 { 6 float area, circum, radius; 7 printf("What‘s the radius of your pizza?\n"); 8 scanf_s("%f", &radius); 9 area = PI * radius*radius; 10 circum = 2 * PI*radius; 11 printf("Your basic pizza parameters are as follows:\n "); 12 printf("circumference = %1.2f,area = %1.2f\n", circum, area); 13 system("pause"); 14 return 0; 15 }
const限定符
const int = OLD_YEAR;//OLD_YEAR在程序裏面不可修改
明示常量
#include<limits.h> #include<stdio.h> int main(void) { printf("%d\n", INT_MAX); system("pause"); return 0; }
limits.h
含義 | |
---|---|
CHAR_BIT | char類型的位數 |
CHAR_MAX | char類型的最大值 |
CHAR_MIN | char類型的最小值 |
SCHAR_MAX | signed char類型的最大值 |
SCHAR_MIN | signed char類型的最小值 |
UCHAR_MAX | unsiged char類型的最大值 |
SHRT_MAX | short類型的最大值 |
SHRT_MIN | short類型的最小值 |
USHRT_MAX | unsigned short類型的最大值 |
INT_MAX | int類型的最大值 |
INT_MIN | int類型的最小值 |
UINT_MAX | unsiged int的最大值 |
LONG_MAX | long類型的最大值 |
LING_MIN | long類型的最小值 |
ULONG_MAX | unsigned long類型的最大值 |
LLONG_MAX | long long類型的最大值 |
LLONG_MIN | long long類型的最小值 |
ULLONG_MAX | unsigned long long類型的最大值 |
含義 | |
---|---|
FLT_MANT_DIG | float類型的尾數位數 |
FLT_DIG | float類型的最少有效數字位數(十進制) |
FLT_MIN_10_EXP | 帶全部有效數字的float類型的最小負指數(以10為底) |
FLT_MAX_10_EXP | float類型的最大正指數(以10為底) |
FLT_MIN | 保留全部精度的float類型最小正數 |
FLT_MAX | float類型的最大正數 |
FLT_EPSILON | 1.00和比1.00大的最小float類型值之間的差值 |
printf()和scanf()和*修飾符
如果不想預先指定字段寬度,希望通過程序來指定,那麽可以用*修飾符代替字段寬度;如果轉換符%*d,那麽參數列表中應包含*和d對應的值
1 #include<stdio.h> 2 3 int main(void) 4 { 5 unsigned width, precision; 6 int number = 256; 7 double weight = 242.5; 8 printf("Enter a field width:\n"); 9 scanf_s("%d", &width); 10 printf("The number is:%*d:\n", width, number); 11 printf("Now enter a width and a precision:\n"); 12 scanf_s("%d %d", &width, &precision); 13 printf("Weight = %*.*f\n", width, precision, weight); 14 printf("Done!\n"); 15 system("pause"); 16 return 0; 17 }
1 #include<stdio.h> 2 int main(void) 3 { 4 int n; 5 printf("Please enter three integers:\n"); 6 scanf_s("%*d %*d %d", &n); 7 printf("The last integer was %d\n", n); 8 system("pause"); 9 return 0; 10 } 11 12 result: 13 Please enter three integers: 14 1 2 3 15 The last integer was 3
printf()用法提示
1 #include<stdio.h> 2 int main(void) 3 { 4 int val_1 = 12, val_2 = 345, val_3 = 1222; 5 printf("%9d %9d %9d\n", val_1, val_2, val_3);//%nd設置字段寬度 6 system("pause"); 7 return 0; 8 } 9 10 result: 11 12 345 1222
c語言之字符串和格式化輸入輸出