- 字符串和格式化的输入/输出
- strlen()
- const
- 字符串
- 创建和存储字符串
- scanf()和print()读取
- strlen()取长度
width.c
#include<stdio.h>
/* 使用修饰符和标志*/
#define PAGES 931
int main(void)
{
printf("*%d*\n", PAGES);
printf("*%2d*\n", PAGES);
printf("*%10d*\n", PAGES);
printf("*%-10d*\n", PAGES);
getchar();
return(0);
}
#include<stdio.h>
/* 使用修饰符和标志*/
int main(void)
{
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);
getchar();
return 0;
}
#include<stdio.h>
/* 使用格式标志示例*/
int main(void)
{
printf("%X %x %#x\n", 31, 31, 31);
printf("**%d**% d**% d**\n", 31, 31,-31);
printf("**%5d**%5.3d**%05d**%05.3d**\n", 6, 6,6,6);
getchar();
return 0;
}
output:
1F 1f 0x1f
**31** 31**-31**
** 6** 006**00006** 006**
#include<stdio.h>
/* 使用格式标志示例*/
#define BLURB "Authentic imitation!"
int main(void)
{
printf("/%2s/\n", BLURB);
printf("/%24s/\n", BLURB);
printf("/%24.5s/\n", BLURB);
printf("/%-24.5s/\n", BLURB);
getchar();
return 0;
}
/Authentic imitation!/
/ Authentic imitation!/
/ Authe/
/Authe /
/*longstrg.c -- 打印较长的字符串*/
#include<stdio.h>
int main(void)
{
printf("Here's one way to print a");
printf("long string.\n");
printf("Here's one way to print a\
long string.\n");
printf("Here's one way to print a"
"long string.\n");
getchar();
return 0;
}
Here's one way to print along string.
Here's one way to print along string.
Here's one way to print along string.
/*input.c -- 打印较长的字符串*/
#include<stdio.h>
int main(void)
{
int age = 0;
float assets = 0.0;
char pet[30];
printf("Enter your age,assets,and favorite pet.\n");
scanf("%d %f", &age, &assets);
scanf("%s", pet);
printf("%d $%.2f %s\n",age,assets,pet);
system("pause");
return 0;
}
/*varwid.c -- 使用可变宽度的输出字段*/
#include<stdio.h>
int main(void)
{
unsigned width, precision;
int number = 256;
double weight = 242.5;
printf("What field withd?\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);
system("pause");
return 0;
}
/*skip2.c --跳过相应的输入项目*/
/* 读取文件的特定列(该文件的数据以统一的列排列)*/
#include<stdio.h>
int main(void)
{
int n=0;
printf("Please enter three integers:\n");
scanf("%*d %*d %d", &n);
printf("The last integer was %d\n", n);
system("pause");
getchar();
return 0;
}
Please enter three integers:
12 12 13
The last integer was 13
4.5 关键概念
编程题:
1.
/*tallback.c --能够为您提供信息的对话程序*/
/* 读取文件的特定列(该文件的数据以统一的列排列)*/
#include<stdio.h>
#include<string.h>
#define DENSITY 62.4 // 人的密度
int main(void)
{
float weight = 0.0, volume = 0.0;
int size = 0, letters = 0;
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 volum is %2.2f cubic feet.\n",
name, volume);
printf("Also, your first name has %d letters,\n",
letters);
printf("and we have %d bytes to store it in.\n", size);
getchar();
getchar();
return 0;
}
output:
Hi!,What’s your first name?
henry
henry,what’s your weight in pounds?
33
well, henry, your volum is 0.53 cubic feet.
Also, your first name has 5 letters,
and we have 40 bytes to store it in.
编程练习
/*name.c --输入姓和名*/
#include<stdio.h>
int main(void)
{
char firstName[20];
char lastName[20];
printf("Please input your first name and lastname\n");
scanf("%s %s", firstName, lastName);
printf("Your name is %s %s\n",firstName, lastName);
getchar();
getchar();
return 0;
}
----
/*name.c --输入姓和名*/
#include<stdio.h>
#include<string.h>
int main(void)
{
char firstName[20];
int width;
char lastName[20];
printf("Please input your first name and lastname\n");
scanf("%s %s", firstName, lastName);
printf("A: \"%s%s\"\n", firstName, lastName);
printf("B: \"%20s\"\n", firstName );
printf("C: \"%-20s%\"\n", firstName);
width = strlen(firstName)+ 3;
printf("D: \" %*s\"\n", width,firstName); // *对应width;
getchar();
getchar();
return 0;
}
---
/*printf.c --按照格式输出*/
#include<stdio.h>
int main(void)
{
float a = 0.0;
printf("Please input a float number\n");
scanf("%f", &a);
printf("a: The input is %.1f or %.1e\n", a, a);
printf("b: The input is %+.3f or %.3e\n", a, a);
getchar();
getchar();
return 0;
}
output:
Please input a float number
23.5
a: The input is 23.5 or 2.4e+01
b: The input is +23.500 or 2.350e+01