完成了第三章第一小节

This commit is contained in:
lzy
2024-03-19 17:46:17 +08:00
parent ad3056a8b8
commit 308c9753bb
5 changed files with 421 additions and 15 deletions

View File

@@ -21,7 +21,14 @@ int main()
char str[STRSIZE] = "helloworld";
double dou = 123.456;
long long l = 123456;
// 不加\nbefore while()存入了缓冲区,故不输出
// printf("[%s:%d]before while().", __FUNCTION__, __LINE__);
printf("[%s:%d]before while().\n", __FUNCTION__, __LINE__);
while (1)
{
}
// printf("[%s:%d]after while().", __FUNCTION__, __LINE__);
printf("[%s:%d]after while().\n", __FUNCTION__, __LINE__);
// printf("f = %8.1f\n", f);
// out:f = 123.5
// printf("i = %2d\n", i);
@@ -45,5 +52,37 @@ int main()
// dou = 123.456000
// l = 123456
// printf("hello world!\n");
// 实参过多或者过少都会警告
// printf("#d #e\n", i);
// printf("#d #e\n", i, f, l);
exit(0);
}
}
#if 0
func(FILE *fp, long i)
{
}
int main()
{
FILE *fp;
long a = 11;
fp = fopen();
// 可行a定义了long型与函数要求一致
func(fp, a);
// 早期的编译器不认可这个12没有单位不同编译器默认类型不同
// 需要加上L修饰符
func(fp, 12);
func(fp, 12L);
// 对于func(FILE *fp, long long i)
func(fp, 12LL);
}
#endif
// 定义一年有多少秒
#define SEC_YEAR (60LL * 60LL * 24LL * 365LL)

83
Chapter3/IO/scanf.c Normal file
View File

@@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
/**
* int scanf(const char *format, 地址表);
*/
// #define STRSIZE 32
#define STRSIZE 3
// 除掉尾0占一个
// 相当于只能两个字符
// 但是这时候运行输入hello不会报错
// 实际上是越界了
int main()
{
int i;
float f;
char str[STRSIZE];
int ret;
char ch;
printf("Please enter:\n");
// scanf("%d", &i);
// ch = getchar();
// printf("i = %d, ch = %c\n", i, ch);
/*
out:
Please enter:
4 h
i = 4, ch =
ch被吞掉了
*/
// scanf("%d", &i);
// scanf("%*c%c", &ch);
// 用 '%*c' 吃掉回车
/*
out:
Please enter:
5
h
i = 5, ch = h
*/
printf("i = %d, ch = %c\n", i, ch);
#if 0
// 输入a直接卡死scanf直接放在循环中很危险
// 需要加入校验用ret接收scanf返回值
while (1)
{
// scanf("%d", &i);
ret = scanf("%d", &i);
if (ret != 1)
{
printf("Enter Error!\n");
break;
}
printf("i = %d\n", i);
}
#endif
// printf("Please enter fot str:\n");
// 这种情况下,不能有任何间隔符
// 输入hello world只会得到hello
// 对于scanf不建议'%s'
// scanf("%s", str);
// printf("%s\n", str);
// printf("Please enter for i[int]:\n");
// 这里两个数字之间有',',输入就一定要有','
// 最好不加,空格符可以是空格 回车 tab
// scanf("%d,%f", &i, &f);
// printf("i = %d\n", i);
// printf("f = %f\n", f);
exit(0);
}