C Primer Plu学习笔记【1-4章节】
第一章 复习题
1.对于编程而言,可移植性意味着什么:
答:完善的可移植性程序是,其源代码无须修改就能在不同计算机系统中成功编译的程序。
2.解释源代码文件、目标代码文件和可执行文件有什么区别?
答:源代码文件包含程序员使用的任何编程语言编写的代码。目标代码文件包含机器语言代码,它不必是完整的程序代码。可执行文件包含组成可执行程序的完整机器语言代码。
3.编程的7个步骤
答:1定义程序目标,2设计程序,3编写程序,4编译程序,5运行程序,6测试和调试程序,7维护和修改程序
4.编译器的任务是什么
答:编译器把源代码(如,用C语言写的代码)翻译成等价的机器语言代码(也叫做目标代码)
5.链接器的任务是什么
链接器把编译器编译好的源代码以及库代码和启动代码组合起来,生成一个可执行程序。
2.11复习题
1.C语言的基本模块是什么?
函数
编程题
1。编写程序按照要求输出
#include <stdio.h> int main(void){ printf("Gustav Mahler\n"); printf("Gustav\nMahle\nr"); printf("Gustav Mahler\n");
return 0; }
2. 编写一个程序,打印你的姓名和地址
#include <stdio.h> int main(void){ printf("My name is sidian,age is 18.\n"); return 0; }
3. 编写一个程序把你的年龄转换成天数
#include <stdio.h> int main(void){ printf("Days is %d.\n", 18*365); return 0; }
4.编写三个函数,用于输出
For he's a jolly good fellow!
For he's a jolly good fellow!
For he's a jolly good fellow!
Which nobody can deny!
#include <stdio.h> void jolly(void); void deny(void); void jolly(void) { printf("For he's a jolly good fellow!\n"); } void deny(void) { printf("Which nobody can deny!\n"); } int main(void) { jolly(); jolly(); jolly(); deny(); return 0; }
5.编写一个程序,生成一下输出:
Brazil, Russia, India, China
India, China,
Brazil, Russia
#include <stdio.h> void br(void); void ic(void); void br(void) { printf("Brazil, Russia"); } void ic(void) { printf("India, China"); } int main(void) { br(); printf(", "); ic(); printf("\n"); ic(); printf(","); printf("\n"); br(); printf("\n"); return 0; }
6编写一个程序,创建一个整数变量toes,并将toes设置为10.程序中还有计算toes的两倍和toes的平方。该程序打印3个值,并分别描述已区分。
#include <stdio.h> int main(void) { int toes; toes = 10; printf("toes is %d, double toes is %d, toes square is %d.\n",toes,toes*2,toes*toes); return 0; }
7.研究表明,微笑好处多多。编写下面的输出
Smile!Smile!Smile!
Smile!Smile!
Smile!
#include <stdio.h> void smile(void); void smile(void) { printf("Smile!"); } int main(void) { smile();smile();smile(); printf("\n"); smile();smile(); printf("\n"); smile(); printf("\n"); return 0; }
8.按照标准要求输出
starting now:
one
two
three
done!
#include <stdio.h> void one_three(void); void two(void); void one_three(void) { printf("one\n"); two(); printf("three\n"); } void two(void) { printf("two\n"); } int main(void) { printf("starting now:\n"); one_three(); printf("done!\n"); return 0; }
第三章 数据和C
// platinum.c #include <stdio.h> int main(void) { float weight; float value; printf("Are you worth you weight in platinum?\n"); printf("Let's check it out.\n"); printf("Please enter your weight in pounds: "); scanf("%f", &weight); value = 1700.0 * weight * 14.5833; printf("Your weight in platinum is worth $%.2f.\n",value); printf("You are easily worth that! If platinum prices drop,\n"); printf("eat more to maintain your value.\n"); return 0; }
书中的第一个实例代码
格式话参数少些的情况
#include <stdio.h> int main(void) { int ten = 10; int two = 2; printf("Doing it right: "); printf("%d minus %d is %d.\n", ten, 2, ten-two); printf("Doing it wrong: "); printf("%d minus %d is %d.\n", ten); /* 少写了两个参数 */ return 0; }
数值范围的测试
#include <stdio.h> int main(void) { int i = 2147483647; unsigned int j= 3144960000; printf("%d, %d, %d\n", i, i+1, i+2); printf("%d, %d, %d\n", j, j+1, j+2); /* 范围与有符号的一致*/ return 0; }
格式化8进制,16进制输出
#include <stdio.h> int main(void) { int x = 100; printf("dec = %d; octal = %o; hex = %x \n",x, x, x); printf("dec = %d; octal = %#o; hex = %#x \n",x, x, x); /* 数字输出带前置标志格式化输出 */ return 0; }
打印short,long,long long和unsigned类型
#include <stdio.h> int main(void) { unsigned int un = 3000000000; short end = 200; long big = 65537; long long verybig = 1234567890864200; char str1[30]; printf("un = %u and not %d\n", un, un); /* %h是short输出方式 */ printf("end = %hd and %d\n", end, end); /* %u是无符号输出,%ld实际测试与lld输出有效位差不多*/ printf("big = %ld and not %hd\n", big, big); printf("verybig = %lld and not %ld\n", verybig, verybig); return 0; }
一般用%d就够用了,整数位情况。
3.4.3 使用字符:char类型
char类型用于存储字符(如,字母或标点符号),但是从技术层面看,char是整数类型。因为char类型实际上储存的是整数而不是字符。
打印字符
#include <stdio.h> int main(void) { char ch; printf("Please enter a character. \n"); scanf("%c", &ch); printf("The code for %c id %d.\n", ch, ch); return 0; }
使用了%d格式化输出char
使用inttypes.h文件格式化输出信息
#include <stdio.h> #include <inttypes.h> int main(void) { int32_t me32; me32 = 45933945; printf("First, assume int32_t is int: "); printf("me32 = %d\n", me32); printf("Next, Let's not make any assumptions.\n"); printf("Instead, use a \"macro\" from inttyoe.h: "); printf("me32 = %" PRId32 " \n", me32); return 0; }
格式化输出浮点值 %f输出float和double类型浮点数,%e打印指数输出,%a16进制输出
#include <stdio.h> int main(void) { float aboat = 32000.0; double abet = 2.14e9; long double dip = 5.32e-5; // 用科学记数法的输出表示浮点数 printf("%f can be written %e\n",aboat, aboat); // 控制精度输出 printf("%.2f can be written %.2e\n",aboat, aboat); // 用16进制的输出表示 printf("And it's %a in hexadecimal, powers of 2 notation\n", aboat); printf("%f can writen %e\n", abet, abet); printf("%Lf can be written %Le\n", dip, dip); return 0; }
输出
32000.000000 can be written 3.200000e+04 32000.00 can be written 3.20e+04 And it's 0x1.f4p+14 in hexadecimal, powers of 2 notation 2140000000.000000 can writen 2.140000e+09 0.000053 can be written 5.320000e-05 Program ended with exit code: 0
查看各种类型的大小,用内置的函数sizeof
#include <stdio.h> int main(void) { printf("Type int has a size of %zd bytes.\n", sizeof(int)); printf("Type char has a size of %zd bytes.\n", sizeof(char)); printf("Type long has a size of %zd bytes.\n", sizeof(long)); printf("Type long long has a size of %zd bytes.\n", sizeof(long long)); printf("Type float has a size of %zd bytes.\n", sizeof(float)); printf("Type double has a size of %zd bytes.\n", sizeof(double)); printf("Type long double has a size of %zd bytes.\n", sizeof(long double)); return 0; }
输出
Type int has a size of 4 bytes. Type char has a size of 1 bytes. Type long has a size of 8 bytes. Type long long has a size of 8 bytes. Type float has a size of 4 bytes. Type double has a size of 8 bytes. Type long double has a size of 16 bytes. Program ended with exit code: 0
最后是演示格式化的时候,参数不匹配的情况
#include <stdio.h> int main(void) { int n = 4, m = 5; float f = 7.0f; float g = 8.0; // 多了一个 printf("%d\n", n , m); printf("%d %d %d\n", n); // 格式不匹配 printf("%d %d\n", f, g); return 0; }
输出
4
4 0 -1125646195
0 0
Program ended with exit code: 0
3.11 编程练习
1.通过试验观察系统如何处理整数上溢、浮点数上溢和浮点数下溢情况。
#include <stdio.h> #include <math.h> int main(void) { short s1 = 32767; short res, add_num = 1; res = s1 + add_num; float f1 = 999999999.0f; // 整数上溢 printf("%hd + %d = %d\n", s1 , add_num, res); // f printf("%f\n", f1 + 1e399); printf("%e\n", f1 / 1e399); printf("\n%f.\n", pow(2.0, 16.0)); printf("short size is %zd.\n", sizeof(short)); return 0; }
输出
32767 + 1 = -32768 inf 0.000000e+00 65536.000000. short size is 2. Program ended with exit code: 0
2.编写一个程序,要求提示输入一个ASCII码值(),然后打印输出这个字符。
#include <stdio.h> #include <math.h> int main(void) { int in_num; printf("Please enter a number:\b\b\b"); scanf("%d", &in_num); printf("This number %d char is %c.\n",in_num, in_num); return 0; }
输出
Please enter a number:99 This number 99 char is c. Program ended with exit code: 0
3 编写一个成后续,发出一个声警报,然后打印下面的文本:
Startled by the sudden sound, Sally shouted,
"By the Great Pumkin, what was that!"
#include <stdio.h> int main(void) { // \a为输出警报音乐 printf("\a"); printf("Startled by the sudden sound, Sally shouted,\n"); printf("\"By the Great Pumkin, what was that!\"\n"); return 0; }
输出
Startled by the sudden sound, Sally shouted, "By the Great Pumkin, what was that!" Program ended with exit code: 0
4.编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,在打印成p计数法,按照下面格式输出
Enter a floating-point value: 64.25
fixed-point notation: 64.250000
exponential notation: 6.425000e+01
p notation 0x1.01p+6
#include <stdio.h> int main(void) { double f_num; printf("Enter a floating-point value: "); scanf("%lf", &f_num); printf("fixed-point notation: %f\n", f_num); printf("exponential notation: %e\n", f_num); printf("p notation %a\n", f_num); return 0; }
输出
Enter a floating-point value: 64.25 fixed-point notation: 64.250000 exponential notation: 6.425000e+01 p notation 0x1.01p+6 Program ended with exit code: 0
5.一年大约有3.156X10的7次方秒。编写一个程序,提示用户输入年龄,然后显式该年龄对应的秒速。
#include <stdio.h> int main(void) { double year_second = 3.156e7; short age; printf("请输入你的年龄:"); scanf("%hd", &age); printf("你已经过了%.0f秒了.\n", year_second * age); return 0; }
输出
请输入你的年龄:30 你已经过了946800000秒了. Program ended with exit code: 0
6一个水分子的质量为3.0X10的负23次方克。一夸脱水大约950克。编写一个程序,提示用户输入水的夸脱数,变显式水分子的数量。
#include <stdio.h> int main(void) { double l_quality = 3.0e-23; double in_number; printf("请输入多少夸脱水:"); scanf("%lf", &in_number); printf("你输入的%f夸脱水包含%f数量的水分子.\n", in_number,in_number * 950 / l_quality); return 0; }
输出
请输入多少夸脱水:.1 你输入的0.100000夸脱水包含3166666666666666881974272.000000数量的水分子. Program ended with exit code: 0
7.一英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英寸),然后以厘米为单位显式身高。
#include <stdio.h> int main(void) { double in_cm; printf("请输入你的身高(cm):"); scanf("%lf", &in_cm); printf("你的身高%f(cm)相当与(%f)(英寸).\n", in_cm,in_cm / 2.54); return 0; }
输出
请输入你的身高(cm):180 你的身高180.000000(cm)相当与(70.866142)(英寸). Program ended with exit code: 0
8.一品脱等于2杯,1杯等于8盎司,1盎司等于2汤勺,一汤勺等于3茶勺。编写一个程序,提示用户输入杯数,显式另外不同的等价容量。
#include <stdio.h> int main(void) { double cup_num; printf("请输入杯数:"); scanf("%lf", &cup_num); printf("等于%f杯\n", cup_num / 2); printf("等于%f盎司\n", cup_num * 8); printf("等于%f汤勺\n", cup_num * 8 * 2); printf("等于%f茶勺\n", cup_num * 8 * 2 * 3); return 0; }
输出
请输入杯数:1.5 等于0.750000杯 等于12.000000盎司 等于24.000000汤勺 等于72.000000茶勺 Program ended with exit code: 0
第4章 字符串和格式化输入/输出
4.1前导程序
#include <stdio.h> #include <string.h> #define DENSITY 62.4 //人体密度 int main() { float weight, volume; int size; unsigned long letters; char name[40]; // name是一个可容纳40个字符的数组 printf("Hi! What's you 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 first name has %lu lettrts, \n", letters); printf("and we have %d bytes ti store it.\n", size); return 0; }
输出
Hi! What's you first name?
sidian
sidian, what's your weight in pounds?
150
Well, sidian, your volume is 2.40 cubic feet.
Also, your first name has 6 lettrts,
and we have 40 bytes ti store it.
Program ended with exit code: 0
4.2字符串的介绍,用""告诉编辑器,这是一个字符串。
记住是双引号,单引号就是字符。
4.2.1插入类型数组和null字符
C中的字符串都是以\n结束,这个空字符占用了一个字节。C的字符串是一个数组,数组是同类数据元素的有序序列。
声明字符串如下
char name[40];
跟字符差不多,只不过变量名后面需要添加[]符号
4.2.2使用字符串
重点是scanf当读到制表符,空格,换行后就读取了
示例代码
#include <stdio.h> #define PRAISE "You are an extraordinary being." int main(void) { char name[40]; printf("What's your name? "); scanf("%s", name); printf("Hello, %s. %s\n", name, PRAISE); return 0; }
输出
What's your name? shi jian
Hello, shi. You are an extraordinary being.
Program ended with exit code: 0
"x"与'x'的区别,一个是字符串,一个是字符,最大的区别是字符串里面有两个字符组成。
4.2.3 strlen()函数
#include <stdio.h> #include <string.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); printf("Your name of %zd letters occupies %zd memory cells.\n", strlen(name), sizeof name); /* strlen 查的是实际的字符串大小,不包括\0, */ printf("The phrase of praise has %zd letters", strlen(PRAISE)); printf("and occupies %zd memory cells. \n", sizeof(PRAISE)); return 0; }
输出
what's your name? sidian
Hello, sidian. You are an extraordinary being.
Your name of 6 letters occupies 40 memory cells.
The phrase of praise has 31 lettersand occupies 32 memory cells.
Program ended with exit code: 0
strlen来至string.h文件,可以测试处字符串的实际长度,sizeof可以查寻具体对象或者初始定义的变量空间。
4.3常量和C预处理。
书中介绍了define
#include <stdio.h> #define PI 3.14159 int main(void) { float area, circun, radius; printf("What is the radius of your pizza?\n"); scanf("%f", &radius); area = PI * radius * radius; circun = PI * 2 * radius; printf("Your basic pizza parameters are as follows:\n"); // 格式化输出通过1.2f保持两位有效小数点,长度为1 printf("circumference = %1.2f, area = %1.2f\n", circun, area); return 0; }
输出
What is the radius of your pizza?
6
Your basic pizza parameters are as follows:
circumference = 37.70, area = 113.10
Program ended with exit code: 0
4.3.1 const限定符
const int MONTHS=12;
与define完全不一样的用法
4.3.2 明示常量
C头文件limis.h与float.h分别提供了与整数类型和浮点类型大小限制相关的详细信息。
示例代码
#include <stdio.h> #include <limits.h> #include <float.h> int main(void) { printf("Some number limits for this system:\n"); printf("Biggest int: %d\n", INT_MAX); printf("Smallest long long: %lld\n", LLONG_MAX); printf("One byte = %d bits on this system", CHAR_BIT); printf("Largest double: %e\n", DBL_MAX); printf("Smallest normal float: %e\n", FLT_MIN); printf("float precision = %d digits\n", FLT_DIG); printf("float epsilon = %e\n", FLT_EPSILON); return 0; }
输出
Some number limits for this system:
Biggest int: 2147483647
Smallest long long: 9223372036854775807
One byte = 8 bits on this systemLargest double: 1.797693e+308
Smallest normal float: 1.175494e-38
float precision = 6 digits
float epsilon = 1.192093e-07
Program ended with exit code: 0
4.4 printf()和scanf()
4.4.2 使用printf()
示例代码
#include <stdio.h> #define PI 3.141593 int main(void) { int number = 7; float pies = 12.75; int cost = 7800; printf("The %d contestants ate %f berry pies.\n", number, pies); printf("The value og pi is %f.\n", PI); printf("Farewell! thou art too dear for my possessing,\n"); printf("%c%d\n", '$', 2*cost); return 0; }
输出
The 7 contestants ate 12.750000 berry pies.
The value og pi is 3.141593.
Farewell! thou art too dear for my possessing,
$15600
Program ended with exit code: 0
4.4.3 printf()的转换说明修饰符
示例代码
#include <stdio.h> #define PAGES 959 int main(void) { printf("*%d*\n", PAGES); printf("*%2d*\n", PAGES); printf("*%10d*\n", PAGES); printf("*%-10d*\n", PAGES); // 左对齐 return 0; }
输出
*959*
*959*
* 959*
*959 *
Program ended with exit code: 0
浮点数的格式化输出示例代码:
#include <stdio.h> int main(void) { const double RENT = 3852.99; // 定义常量 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; }
输出
*3852.990000*
*3.852990e+03*
*3852.99*
*3853.0*
* 3852.990*
* 3.853E+03*
*+3852.99*
*0003852.99*
Program ended with exit code: 0
其他的多种格式化输出
#include <stdio.h> int main(void) { 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; }
输出情况
1f 1F 0x1f
**42** 42**-42**
** 6** 006**00006** 006**
Program ended with exit code: 0
格式化字符串输出,示例代码
#include <stdio.h> #define BLURE "Authentic imitation" int main(void) { printf("[%2s]\n", BLURE); printf("[%24s]\n", BLURE); printf("[%24.5s]\n", BLURE); // .用来限制输出字符串长度 printf("[%-24.5s]\n", BLURE); return 0; }
输出
[Authentic imitation]
[ Authentic imitation]
[ Authe]
[Authe ]
Program ended with exit code: 0
学以致用作业
#include <stdio.h> int main(void) { char name[40] = "sidian"; double cash = 1918.66; printf("The %s family just may be $%.2f richer!\n", name, cash); return 0; }
输出
The sidian family just may be $1918.66 richer!
Program ended with exit code: 0
4.4.4转换说明的意义
1、转换不匹配
示例代码,转移不匹配的代码
#include <stdio.h> #define PAGES 336 #define WORDS 65618 int main(void) { short num = PAGES; short mnum = -PAGES; printf("num as short and unsigned short: %hd %hu\n", num, num); printf("-num as short and unsigned short: %hd %hu\n", mnum, mnum); printf("num as int and char: %d %c\n", num, num); printf("WORDS as int, short,and char: %d, %hd, %c\n",WORDS,WORDS,WORDS); return 0; }
输出
num as short and unsigned short: 336 336
-num as short and unsigned short: -336 65200
num as int and char: 336 P
WORDS as int, short,and char: 65618, 82, R
Program ended with exit code: 0
上面的是整形与字节不匹配,下面是浮点数与整数的不匹配。
#include <stdio.h> int main(void) { float n1 = 3.0; double n2 = 3.0; long n3 = 2000000000; long n4 = 1234567890; printf("%.le %.le %.le %.le \n", n1, n2, n3, n4); printf("%ld %ld\n", n3, n4); // 就这个全部正常输出 printf("%ld %ld %ld %ld \n", n1, n2, n3, n4); return 0; }
3e+00 3e+00 0e+00 0e+00
2000000000 1234567890
2000000000 1234567890 -6868094452202405870 77992
Program ended with exit code: 0
2.printf()的返回值
正常返回的是字符数量,错误返回负数
#include <stdio.h> int main(void) { int bph2o = 212; int rv; rv = printf("%d F is water's boiling point.\n", bph2o); /* 包含空格,换行符*/ printf("The printf() function printed %d characters.\n", rv); return 0; }
输出
212 F is water's boiling point.
The printf() function printed 32 characters.
Program ended with exit code: 0
3.打印较长的字符串
3中方法,一种是多次printf打印输出,2是通过\的方式,3是两个字符串之间会自动链接。
#include <stdio.h> int main(void) { printf("Here's one way to print a "); printf("long string.\n"); printf("here's another way to print a \ long string.\n"); printf("Here's the newest way to print a " "long string.\n"); return 0; }
有意思的三种写法,Python里面也是一样的。
4.4.5使用scanf()
一个规则
如果使用scanf()读取基本变量类型的值,再变量名前面加上一个&
如果用scanf()把字符串读取字符串数组中,不要使用&
示例代码
#include <stdio.h> int main(void) { int age; float assets; char pet[30]; printf("Enter your age, assets, and favorute pet.\n"); scanf("%d %f",&age, &assets); scanf("%s", pet); /* 这个不需要加&符号 */ printf("%d $%.2f %s \n",age, assets, pet); return 0; }
输出:
Enter your age, assets, and favorute pet.
22 100 cat
22 $100.00 cat
Program ended with exit code: 0
scanf()函数使用空白(换行符、制表符和空格)把输入分成多个字段。在依次把转换说明和字段匹配时跳过空白。
1.从scanf()角度输入
接下来,我们更详细的研究scanf()如何读取输入。假设scanf()根据一个%d转换说明读取一个整数。scanf()函数每次读取一个数字字符或者一个符号(+或-)。如果找到一个数字或者符号,它便保存该字符,并读取下一个字符。如果下一个字符是数字,它便保存该数字并读取下一个字符。scanf()不断地读取和保存字符,直至遇到非数字字符。如果遇到一个非数字字符,它便认为读到了整数的末尾。然后,scanf()把非数字字符放回输入。这意味这程序再下一次读取输入时,首先读到的是上一次读取丢弃的非数字字符。最后,scanf()读取已读取数字(可能还有符号)相应的数值,并将计算后的值放入指定的变量中。
...
3.scanf()的返回值
scanf函数返回成功读取的项数。如果没有读取任何项,且需要读取一个数字而用户却输入一个非数值字符串,scanf()便返回0;
4.4.6printf()和scanf()的修饰符[不知道Python里面能不能用这个格式化输出]
通过输入参数来设定指定的输出宽度
#include <stdio.h> int main(void) { unsigned width, precision; int number = 256; double weight = 242.5; printf("Enter a field width.\n"); scanf("%d", &width); printf("The number is :%*d:\n", width, number); /* 格式化指定输出字符长度*/ scanf("%d %d", &width, &precision); printf("Wright = %*.*f\n", width, precision, weight); printf("Done!\n"); return 0; }
输出
Enter a field width.
6
The number is : 256:
8 3
Wright = 242.500
Done!
Program ended with exit code: 0
scanf的*用法。
#include <stdio.h> int main(void) { int n; printf("Please enter three integers:\n"); scanf("%*d %*d %d", &n); // 会忽略前面输入的两个数字 printf("The last integer was %d\n", n); return 0; }
输出
Please enter three integers:
2014 2015 2016
The last integer was 2016
Program ended with exit code: 0
4.4.7printf()的用法提示
1.通过%数字d来对齐格式化输出
2.浮点数格式话通过%.2f这样就可以了。
4.7 复习题
1. 只能读到名,因为除了%c另外所有的scanf碰到空格都会认为,读到这里结束。[错误]
2.打印的输出为
a. He sold the painting for %234.50.
b. Hi! \41表示8进制下字符的数字表示 ;\x41表示16进制下字符的数字表示
c. His Hamlet was funny weithout being vulgar.
has 42 characters.
d. Is 1.20e3 the same as 1201.00?
3.添加转移\"的符号
4.正确的代码
#include <stdio.h> #define B "booboo" #define X 10 int main(void) { int age, xp; char name[40]; printf("Please enter your first name.\n"); scanf("%s", name); printf("All right, %s, what's your age?\n", name); scanf("%d", &age); xp = age + X; printf("That's a %s! You must be as least %d.\n", B, xp); return 0; }
5.代码如下
# include <stdio.h> #define BOOK "War and Peace" int main(void) { float cost = 12.99; float percent = 80.0; printf("This copy of \"War and Peace\" sell for $ %.2f.\n", cost); printf("That is %.0f%% of list.\n", percent); return 0; }
6.
%d
%4x -> %4X
%10.f -> %10.3f
%012.2e
%-30s
7
%15lu
%4x -> %#4x
%-12.2E
%10.3f ->%+10.3f
%8s -> %8.8s
8
%6.4d
%*o
%2c
%+f -> %+0.2
%-7.5s
9
int num;
scanf("%d", &num);
double num -> float num1, num2;
scanf("%f", &num); scanf("%f %f", &num1, &num2)
char str[40];
scanf("%s", str);
char actiong[20];
int value;
scanf("%s %d", action, &value)
int num;
scanf("%* %d", &num); -> scnaf("%*s %d", &num);
10
空白就是换行符,制表符,空格
编程练习
1.
#include <stdio.h> int main(void) { char second_name[40]; char first_name[40]; printf("请输入你的名与姓:\n"); scanf("%s %s", second_name, first_name); printf("%s,%s\n",second_name, first_name); return 0; }
输出
请输入你的名与姓:
stenv liu
stenv,liu
Program ended with exit code: 0
2.
#include <stdio.h> #include <string.h> int main(void) { char name[40]; int len; printf("Please input your name:\n"); scanf("%s", name); printf("\"%s\"\n", name); printf("\"%20s\"\n", name); printf("\"%-20s\"\n", name); len = (int)strlen(name)+3; printf("\"%*s\"\n",len, name); return 0; }
输出
Please input your name:
sd
"sd"
" sd"
"sd "
" sd"
Program ended with exit code: 0
3
#include <stdio.h> int main(void) { float number; printf("input a float nunber:\n"); scanf("%f", &number); printf("The input is %.1f or %.1e\n", number, number); printf("The input is %+.3f or %.3E\n", number, number); return 0; }
输出
input a float nunber:
21.29
The input is 21.3 or 2.1e+01
The input is +21.290 or 2.129E+01
Program ended with exit code: 0
稍微有点不一样,书中要求,2,129E+001
4.
#include <stdio.h> #define change_unit 12 int main(void) { float height; char name[40]; printf("Please input you name:\n"); scanf("%s", name); printf("please input you height(inches):\n"); scanf("%f", &height); printf("%s, you are %.3f feet tall.\n", name, height/change_unit); return 0; }
输出
Please input you name:
sd
please input you height(inches):
70
sd, you are 5.833 feet tall.
Program ended with exit code: 0
5.
#include <stdio.h> int main(void) { float file_size, speed; printf("input you file_size, speed:\n"); scanf("%f,%f", &file_size, &speed); printf("At %.2f megabits per second, afile of %.2f megabytes\n", speed, file_size); printf("downloads in %.2f sencond.\n", file_size / (speed / 8)); return 0; }
输出
input you file_size, speed:
2.2,18.12
At 18.12 megabits per second, afile of 2.20 megabytes
downloads in 0.97 sencond.
Program ended with exit code: 0
6.
#include <stdio.h> #include <string.h> int main(void) { char second_name[40], first_name[40]; printf("input you second_name:\n"); scanf("%s", second_name); printf("input you first_name:\n"); scanf("%s", first_name); printf("%s %s\n", second_name, first_name); printf("%s %s\n%*d %*d\n", second_name, first_name, strlen(second_name), strlen(second_name), strlen(first_name), strlen(first_name)); printf("%s %s\n%-*d %-*d\n", second_name, first_name, strlen(second_name), strlen(second_name), strlen(first_name), strlen(first_name)); return 0; }
输出
input you second_name:
Melissa
input you first_name:
Honeybee
Melissa Honeybee
Melissa Honeybee
7 8
Melissa Honeybee
7 8
Program ended with exit code: 0
7
#include <stdio.h> #include <float.h> int main(void) { float a=100.0, b=3.0; double c=100.0, d=3.0; printf("float: %.6f %.12f %.16f\n", a/b, a/b, a/b); printf("double: %.6f %.12f %.16f\n", c/d, c/d, c/d); printf("FLT_DIG = %d, DBL_DIG = %d.\n", FLT_DIG, DBL_DIG); return 0; }
输出
float: 33.333332 33.333332061768 33.3333320617675781
double: 33.333333 33.333333333333 33.3333333333333357
FLT_DIG = 6, DBL_DIG = 15.
Program ended with exit code: 0
8.
#include <stdio.h> #define Gallon_conversion_liter 3.785 #define Miles_to_kilometers 1.609 int main(void) { double miles,gallon; printf("Please enter mileage and gas:\n"); scanf("%lf, %lf", &miles, &gallon); printf("%.1lf miles to the gallon.\n", miles/gallon); printf("The fuel consumption of 100 kilometers is %.1lf liters.\n", (gallon * Gallon_conversion_liter) * 100 / (miles * Miles_to_kilometers)); return 0; }
输出
Please enter mileage and gas:
100, 13
7.7 miles to the gallon.
The fuel consumption of 100 kilometers is 30.6 liters.
Program ended with exit code: 0