c语言之字符串和格式化输入输出

字符串和格式化输入输出

 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 {
 6     char 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类型的最大值

float.h

 

明示常量含义
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类型值之间的差值

 

把明示常量名中的FLT分别替代成DBL和LDBL,即可分别表示double和long double类型对应的明示常量。

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 }

 

 scanf()中*的用法与此不同,把*放在%和转换符之间时,会使得scanf()跳过相应的输入项。

 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

 

posted @ 2019-03-24 16:42  Mingle_Yuan  阅读(658)  评论(0编辑  收藏  举报