编写到第三章结束
This commit is contained in:
parent
c112cd200a
commit
a8ecaae14b
53
.gitignore
vendored
Normal file
53
.gitignore
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
# .gitignore for C
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.ko
|
||||
*.obj
|
||||
*.elf
|
||||
|
||||
# Linker output
|
||||
*.ilk
|
||||
*.map
|
||||
*.exp
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Libraries
|
||||
*.lib
|
||||
*.a
|
||||
*.la
|
||||
*.lo
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
*.dll
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.i*86
|
||||
*.x86_64
|
||||
*.hex
|
||||
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
*.su
|
||||
*.idb
|
||||
*.pdb
|
||||
|
||||
# Kernel Module Compile Results
|
||||
*.mod*
|
||||
*.cmd
|
||||
.tmp_versions/
|
||||
modules.order
|
||||
Module.symvers
|
||||
Mkfile.old
|
||||
dkms.conf
|
20
.vscode/launch.json
vendored
Normal file
20
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
// 使用 IntelliSense 了解相关属性。
|
||||
// 悬停以查看现有属性的描述。
|
||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "(Windows) 启动",
|
||||
"type": "cppvsdbg",
|
||||
"request": "launch",
|
||||
"program": "输入程序名称,例如 ${workspaceFolder}/a.exe",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${fileDirname}",
|
||||
"environment": [],
|
||||
"console": "externalTerminal"
|
||||
}
|
||||
|
||||
]
|
||||
}
|
36
Chapter_2/2_1_1_details.c
Normal file
36
Chapter_2/2_1_1_details.c
Normal file
@ -0,0 +1,36 @@
|
||||
#include <stdio.h> //C语言并不默认内置输入/输出包
|
||||
|
||||
//这里是函数头哦
|
||||
int main(void) //void main()是不标准写法
|
||||
{
|
||||
/* 函数必须全部声明才能使用
|
||||
旧标准:其必须放在函数块顶部 */
|
||||
|
||||
int doors; //变量名应清楚表达自己的用途
|
||||
int dogs; //变量可以使用大小写、数字和下划线
|
||||
//名称的第一个字符只能是字母或者下划线
|
||||
//如果是数字,会报错,不信试试
|
||||
//int 3b1b;
|
||||
int DOGS; //C语言是区分大小写的
|
||||
doors = 5;
|
||||
dogs = 3;
|
||||
DOGS = 5;
|
||||
dogs = DOGS; //赋值语句
|
||||
printf("We have %d dogs.\n", dogs);
|
||||
//函数有实际参数和形式参数
|
||||
//除了 %d 以外的是实参,%d 是形参
|
||||
|
||||
/* 新标准下怎么放声明都无所谓,但是这样有可能
|
||||
让代码可读性变差 */
|
||||
|
||||
int windows;
|
||||
windows = 404;
|
||||
int cats;
|
||||
cats = 114514;
|
||||
|
||||
return 0;//有返回值的C函数要有这句
|
||||
//int 表示 main() 函数返回一个整数
|
||||
//程序运行到最后时默认会返回0,所以
|
||||
//这句在这个情况下写不写区别不大
|
||||
//但是保留这一句是个好习惯
|
||||
}//花括号括起来的全部是函数体
|
15
Chapter_2/2_2_fathm_ft.c
Normal file
15
Chapter_2/2_2_fathm_ft.c
Normal file
@ -0,0 +1,15 @@
|
||||
//把 2 音寻转换为英寸
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
int feet, fathoms;
|
||||
|
||||
fathoms = 2;
|
||||
feet = 6 * fathoms;
|
||||
printf("There are %d feet in %d fathoms!\n", feet, fathoms);
|
||||
printf("Yes, I said %d feet!\n", 6*fathoms);
|
||||
//给定的传入值不一定要是个变量
|
||||
//所以可以有一个传入时计算的语句
|
||||
|
||||
return 0;
|
||||
}
|
20
Chapter_2/2_3_two_func.c
Normal file
20
Chapter_2/2_3_two_func.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* 一个文件中包含两个函数 */
|
||||
#include <stdio.h>
|
||||
|
||||
void butler(void);//函数原型(声明)
|
||||
//这个是现代写法,早期写法长这样:
|
||||
//void butler();
|
||||
//不推荐上面的写法
|
||||
int main(void)
|
||||
{
|
||||
printf("I will summon the butler function.\n");
|
||||
butler();
|
||||
printf("Yes. Bring me some tea and writeable DVDs.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void butler(void) //函数定义
|
||||
{
|
||||
printf("You rang, sir?\n");
|
||||
}//这种写法有利于厘清程序逻辑
|
19
Chapter_2/2_4_good.c
Normal file
19
Chapter_2/2_4_good.c
Normal file
@ -0,0 +1,19 @@
|
||||
/* 这段程序无错误 */
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
int n, n2, n3;//修正语法错误
|
||||
n = 5;
|
||||
n2 = n * n;
|
||||
n3 = n2 * n;//修正语义错误
|
||||
printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3);//修正语法错误
|
||||
|
||||
return 0;
|
||||
}
|
||||
/* 附上不能用来做变量名的关键字
|
||||
auto break case char const continue default
|
||||
do double else enum extern float for goto
|
||||
if inline int long register restrict return
|
||||
short signed sizeof static struct switch typedef
|
||||
union unsigned void volatile _Bool _Complex _Generic
|
||||
_Imaginary _Noreturn _Static_assert _Thread_local*/
|
14
Chapter_2/2_4_nogood.c
Normal file
14
Chapter_2/2_4_nogood.c
Normal file
@ -0,0 +1,14 @@
|
||||
/* 这段程序有错误 */
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
int n, int n2, int n3;
|
||||
|
||||
/* 该程序有多处错误
|
||||
n = 5
|
||||
n2 = n * n;
|
||||
n3 = n2 * n2;
|
||||
printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3)
|
||||
|
||||
return 0;
|
||||
}
|
24
Chapter_2/2_practice.c
Normal file
24
Chapter_2/2_practice.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
//pr.1
|
||||
printf("\nPR_1_________\n");
|
||||
|
||||
printf("Helix Copex\n");
|
||||
printf("Helix\nCopex\n");
|
||||
printf("Helix ");
|
||||
printf("Copex\n");
|
||||
|
||||
//pr.2
|
||||
printf("\nPR_2_________\n");
|
||||
|
||||
printf("Helix Copex, Harbin.\n");
|
||||
|
||||
//pr.3
|
||||
printf("\nPR_3_________\n");
|
||||
|
||||
int age = 18;
|
||||
int lifetime = age*365;
|
||||
printf("Age:%dy, Lifetime:%dd.", age, lifetime);
|
||||
return 0;
|
||||
}
|
19
Chapter_3/3_10_escape.c
Normal file
19
Chapter_3/3_10_escape.c
Normal file
@ -0,0 +1,19 @@
|
||||
/* 使用转义序列 */
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
float salary;
|
||||
|
||||
printf("\aEnter your desired monthly salary:");
|
||||
printf(" $_______\b\b\b\b\b\b\b");
|
||||
//退格只回退光标,不删除字符
|
||||
scanf("%f", &salary);
|
||||
printf("\n\t$%.2f a month is $%.2f a year.", salary, salary * 12.0);
|
||||
//n新起一行,t添加一个制表位的空大小
|
||||
printf("\rGee!\n");
|
||||
//r使得光标回到起始处
|
||||
|
||||
getchar();
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
22
Chapter_3/3_1_platinum.c
Normal file
22
Chapter_3/3_1_platinum.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* platinum.c 计算与自身体重等重的白金价值 */
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
float weight;
|
||||
float value;
|
||||
|
||||
printf("Are you worth your 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");
|
||||
getchar();
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
11
Chapter_3/3_3_4_int_overflow.c
Normal file
11
Chapter_3/3_3_4_int_overflow.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
int i = 2147483647;
|
||||
unsigned int j = 4294967295;
|
||||
|
||||
printf("%d %d %d\n", i, i+1, i+2);//未定义行为
|
||||
printf("%u %u %u\n", j, j+1, j+2);//未定义行为
|
||||
|
||||
return 0;
|
||||
}
|
10
Chapter_3/3_3_bases.c
Normal file
10
Chapter_3/3_3_bases.c
Normal file
@ -0,0 +1,10 @@
|
||||
#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;
|
||||
}
|
16
Chapter_3/3_4_print2.c
Normal file
16
Chapter_3/3_4_print2.c
Normal file
@ -0,0 +1,16 @@
|
||||
/* 更多 printf() 的特性 */
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
unsigned int un = 30000000000;
|
||||
short end = 200;
|
||||
long big = 65537;
|
||||
long long verybig = 12345678908642;
|
||||
|
||||
printf("un = %u and not %d\n",un,un);
|
||||
printf("end = %hd and %d\n",end,end);
|
||||
printf("big = %ld and not %hd\n",big,big);
|
||||
printf("verybig= %lld and not %ld\n",verybig,verybig);
|
||||
|
||||
return 0;
|
||||
}
|
11
Chapter_3/3_5_charcode.c
Normal file
11
Chapter_3/3_5_charcode.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
char ch;
|
||||
|
||||
printf("Please enter a character.\n");
|
||||
scanf("%c", &ch);
|
||||
printf("The code for %c is %d.\n",ch,ch);
|
||||
|
||||
return 0;
|
||||
}
|
26
Chapter_3/3_6_altnames.c
Normal file
26
Chapter_3/3_6_altnames.c
Normal file
@ -0,0 +1,26 @@
|
||||
/* 本文件主要讲述可移植类型,这些类型引入了两个头文件以
|
||||
确保 C 语言类型在各个系统中功能相同,定义了一批新的类型
|
||||
比如,如果要求一个数精确有32位:
|
||||
int32_t
|
||||
或者要求一个数在可以容纳8位的情况下使用一个最小的类型:
|
||||
int_least8_t
|
||||
或者,如果你需要一个数容量尽可能大(C99):
|
||||
intmax_t / uintmax_t
|
||||
打印时,它们也提供了一些类似的例子。*/
|
||||
#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 inttypes.h: ");
|
||||
printf("me32 = %"PRId32" \n",me32);
|
||||
// 等价于下面这条语句:
|
||||
printf("me32 = %""d"" \n",me32);
|
||||
|
||||
return 0;
|
||||
}
|
15
Chapter_3/3_7_1_floaterr.c
Normal file
15
Chapter_3/3_7_1_floaterr.c
Normal file
@ -0,0 +1,15 @@
|
||||
/* 演示浮点数舍入错误 */
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
float a,b;
|
||||
|
||||
b = 2.0e20 + 1.0;
|
||||
a = b - 2.0e20;
|
||||
printf("%f \n", a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//如果是当前版本 GCC,通常结果为 4008175468544.000000
|
||||
//这个结果表明编译器实现遵循:ISO/IEC/IEEE 60559:2011
|
16
Chapter_3/3_7_showf_pt.c
Normal file
16
Chapter_3/3_7_showf_pt.c
Normal file
@ -0,0 +1,16 @@
|
||||
/* 以两种方式显示 float 类型的值 */
|
||||
#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);
|
||||
// 下面的部分要求 C99 支持
|
||||
printf("And it's %a in hexadecimal, powers of 2 notation\n", aboat);
|
||||
printf("%f can be written %e\n",abet,abet);
|
||||
printf("%Lf can be written %Le\n",dip,dip);
|
||||
|
||||
return 0;
|
||||
}
|
20
Chapter_3/3_8_typesize.c
Normal file
20
Chapter_3/3_8_typesize.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* 打印当前系统类型大小 */
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
//C99为类型大小提供转换说明
|
||||
printf("Type int has a size of %zd types.\n",
|
||||
sizeof(int));
|
||||
printf("Type char has a size of %zd types.\n",
|
||||
sizeof(char));
|
||||
printf("Type long has a size of %zd types.\n",
|
||||
sizeof(long));
|
||||
printf("Type long long has a size of %zd types.\n",
|
||||
sizeof(long long));
|
||||
printf("Type double has a size of %zd types.\n",
|
||||
sizeof(double));
|
||||
printf("Type long double has a size of %zd types.\n",
|
||||
sizeof(long double));
|
||||
|
||||
return 0;
|
||||
}
|
17
Chapter_3/3_9_badcount.c
Normal file
17
Chapter_3/3_9_badcount.c
Normal file
@ -0,0 +1,17 @@
|
||||
/* 函数原型规定了一个函数最多能传入多少个参数,但是
|
||||
对于两个输入输出函数来说,这个规则不管用,因为它们的
|
||||
参数数量是通过第一个参数来确定的。下面的程序演示了参
|
||||
数错误的情况 */
|
||||
|
||||
#include <stdio.h>
|
||||
int main(void)
|
||||
{
|
||||
int n = 4, m = 5;
|
||||
float f = 7.0f, g = 8.0f;
|
||||
|
||||
printf("%d\n",n,m); //参数太多
|
||||
printf("%d %d %d\n", n); //参数太少
|
||||
printf("%d %d\n", f, g); //值的类型不匹配
|
||||
|
||||
return 0;
|
||||
}
|
19
Chapter_3/README.md
Normal file
19
Chapter_3/README.md
Normal file
@ -0,0 +1,19 @@
|
||||
# 本章介绍的内容
|
||||
|
||||
## 关键字
|
||||
|
||||
`int` `short` `long` `unsigned` `char` `float` `double` `_Bool` `_Complex` `_Imaginary`
|
||||
|
||||
## 运算符
|
||||
|
||||
`sizeof()`
|
||||
|
||||
## 函数
|
||||
|
||||
`scanf()`
|
||||
|
||||
## 具体内容
|
||||
|
||||
- 整数类型和浮点数类型的区别
|
||||
- 如何书写整型和浮点型常数,以及如何声明变量
|
||||
- 如何使用 `printf()` 和 `scanf()` 读写不同类型的值
|
8
Chapter_4/README.md
Normal file
8
Chapter_4/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
# 本章介绍的内容
|
||||
|
||||
- 函数:`strlen()`
|
||||
- 关键字: `const`
|
||||
- 字符串
|
||||
- 如何创建、存储字符串
|
||||
- 如何使用 `strlen()` 函数获取字符串的长度
|
||||
- 用 C 预处理器指令 `#define` 和 ANSIC 的 `const` 修饰符创建符号常量
|
Loading…
x
Reference in New Issue
Block a user