admin管理员组文章数量:1567552
1.1前导
老规矩,程序开头
//talkback.c ——演示与用户的交互
#include <stdio.h>
#include <string.h> //提供string的函数原型
#define DENSITY 62.4 //人体密度(磅/立方英尺)
int main()
{
float weight,volume;
int size,letters;
char name[40]; //name是一个可容纳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);
letters=strlen(name);
volume=weight/DENSITY;
printf("Well,%s,your volume is %2.2f cubic feet.\n",name,volume);
printf("Also,your fist name has %d letters,\n",letters);
printf("and we have %d bytes to store it.\n",size);
return 0;
}
运行结果:
(1)用数组存储字符串,每个字节存储一个字符值。
(2)使用%s转换说明来处理字符串的输入输出。
(3)使用strlen()获取字符串的长度。
1.2字符串简介
字符串是一个或多个字符的序列,用双引号引起来,如:"hahahhaha";
1.2.1char类型数组和null字符
C语言中没有专门用来存储字符串的变量类型,字符串都被存储在char类型的数组中。数组由连续的存储单元组成,字符串中的元素被连续的存储在这些存储单元中,每个单元存储一个字符。
数组末尾位置的字符\0是空字符,用于标记字符串的结束。
C中的字符串一定以空字符结束,数组的容量必须至少比待存储字符串中的字符数多1。
什么是数组?
数组可以看作是一行连续的多个存储单元,即:数组是同类型数据元素的有序序列。
例:char name[40];
name后[]表明是一个数组,可存放40个数据元素,char是元素类型。
1.2.2使用字符串
/*praisel.c --使用不同类型的字符串*/
#include <stdio.h>
#define PRAISE "You are an extraordinary being."
int main()
{
char name[40];
printf("What's your name?");
scanf("%s",name);
printf("Hello, %s. %s\n",name,PRAISE);
return 0;
}
输出结果:
注:scanf()只读取了前面的一串字符,因为它在遇到第一个空白时就不再读取输入。
字符串和字符
区别:
1.2.3strlen()函数
计算字符串的长度
例:
///*praise2.c --使用不同类型的字符串*/
#include <stdio.h>
#include <string.h> //提供string的函数原型
#define PRAISE "You are an extraordinary being."
int main()
{
char name[40];
printf("What's your name?");
scanf_s("%s", name,40);
printf("Hello, %s. %s\n", name, PRAISE);
printf("Your name of %zd letters occupies %zd memory cells.\n", strlen(name), sizeof(name));
printf("The phrase of praise has %zd letters ", strlen(PRAISE));
printf("and occupies %zd memory cells.\n", sizeof PRAISE);
return 0;
}
程序结果:
注:sizeof是否使用圆括号,当运算对象是类型时,圆括号必不可少,如sizeof(char),但是当运算对象是特定量时,圆括号可有可无,如sizeof name,sizeof 6.8
1.3常量和C处理器
#define NAME value
#define +符号常量名+符号常量值 注:末尾不加分号,常量名与常量值之间不加=号,符号常量命名与变量相同
编译程序时,所有的常量都会被替换成常量值,这一过程被称为编译时替换。
例:
/*pizza.c --在比萨程序中使用已定义的常量*/
#include <stdio.h>
#define PI 3.14159
int main()
{
float area, circum, radius;
printf("What is the radius of your pizza?\n");
scanf("%f", &radius);
area = PI * radius * radius;
circum = 2.0 * PI * radius;
printf("Your basic pizza parameters are as follows:\n");
printf("circumference=%1.2f,area=%1.2f\n", circum, area);
return 0;
}
程序结果:
1.4printf()和scanf()
printf()和scanf()函数是输入输出函数,简称I/O函数。
1.4.1printf()函数
请求printf()函数打印数据的指令要与待打印数据的类型相匹配
转换说明及打印输出结果:
1.4.2使用printf()
格式:printf(格式字符串,待打印项1,待打印项2,……);
格式字符串是双引号引起来的内容,格式字符串包含实际要打印的字符和转换说明。
1.4.3printf()的转换说明修饰符
printf()中的标记
-:待打印项左对齐,从字段左侧开始打印该项。
+:有符号值为正,显示+,为负,显示-。
空格:有符号值为正,前面显示空,为负,显示-并覆盖空格。
#:把结果转换成另一种形式。%o形式,以0开始,%x形式,以0x开始。
0:数值格式,用前导0代替空格填充字段宽度。
1.修饰符和标记示例
/*width.c --字段宽度*/
#include <stdio.h>
#define PAGES 959
int main()
{
printf("*%d*\n", PAGES);
printf("*%2d*\n", PAGES);
printf("*%10d*\n", PAGES);
printf("*%-10d*\n", PAGES);
return 0;
}
程序结果:
解释:第一个%d输出结果与带整数字段宽度转换说明结果相同;
第二个,输出2字段宽度,待打印有3个字段,自动扩大;
第三个,输出10个字段,数字位于右侧,前面空格补齐10个字段;
第四个,输出10个字段,数字位于左侧,后面空格补齐10个字段。
#include <stdio.h>
int main()
{
const double RENT = 3852.99; //const变量
printf("*%f*\n", RENT);
printf("*%e*\n", RENT);
printf("*%4.2f*\n", RENT);
printf("*%3.1f*\n", RENT);
printf("*%10.3f*\n", RENT);
printf("*%10.3E*\n", RENT);
printf("*%+4.2f*\n", RENT);
printf("*%010.2f*\n", RENT);
return 0;
}
程序结果:
/*flags.c --演示一些格式标记*/
#include <stdio.h>
int main()
{
printf("%x %X %#x\n", 31, 31, 31);
printf("**%d**% d**% d**\n", 42, 42, -42);
printf("**%5d**%5.3d**%05d**%05.3d**\n", 6, 6, 6, 6);
return 0;
}
程序结果:
字符串格式
/*stringf.c --字符串格式*/
#include <stdio.h>
#define BLURB "Authentic imitation!"
int main()
{
printf("[%2s]\n", BLURB);
printf("[%24s]\n", BLURB);
printf("[%24.5s]\n", BLURB);
printf("[%-24.5s]\n", BLURB);
return 0;
}
程序结果:
参数传递:程序把传入的值放入被称为栈的内存区域。
1.4.4使用scanf()函数
规则:1.如果用scanf()读取基本变量类型的值,在变量名前加上一个&;
2.如果把字符串读入字符数组,不使用&。
转换说明:
1.4.5printf()和scanf()的*修饰符
printf()和scanf()都可以使用*修饰符来修改转换说明的含义。
当不想预先指定字段宽度,通过程序来指定,使用*修饰符代替字段宽度。
转换说明是%*d,那么参数列表中应包含*和d的对应值,即要用一个参数告诉函数字段宽度是多少。
例:
#include <stdio.h>
int main()
{
unsigned width, precision;
int number = 256;
double weight = 242.5;
printf("Enter a file 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);
printf("Done!\n");
return 0;
}
测试结果:
这里,6就是使用的字段宽度,8和3分别是字段宽度和小数点后3位。
scanf()中的*用法与此不同,把*放在%和转换字符之间,会使scanf()跳过相应输出项。
如:
/*skiptwo.c --跳过输入中的前两个整数*/
#include <stdio.h>
int main()
{
int n;
printf("Please enter three integers:\n");
scanf("%*d %*d %d", &n);
printf("The last integer was %d\n", n);
return 0;
}
测试结果:
跳过两个整数,将第三个整数拷贝给n,在读取特定列的内容很有用。
1.4.6printf()的用法提示
在两个转换说明中间插入一个空白字符,可以确保即使一个数字溢出了自己的字段,下一个数字也不会被覆盖。因为格式字符串中的普通字符(包括空格)会被打印。
如果要在文字中嵌入一个数字,通常指定一个小于或等于该数字宽度的字段,就没必要输入空白。
1.5关键概念
char类型表示单个字符,用字符串表示字符序列。字符串常量是一种字符串形式,用双引号把字符引起来。可以把字符串存储在字符数组(由内存中相邻字节组成)中。字符串无论是字符串常量还是字符数组,都有一个叫作空字符的隐藏字符结尾。
用#define定义数值常量,用const关键字声明的变量为只读变量。程序中使用符号常量(明示常量),提高了程序的可读性和可维护性。
scanf()要在变量名前加上地址运算符&。
本文标签: 字符串
版权声明:本文标题:字符串和格式化输入输出 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1726560379a1075772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论