test1
//常量存储在 全局静态区(全局变量 静态变量 常量)
//局部变量 函数的栈//动态空间 堆中
int a;
int main()
{
int b;
char *p = "hello world";
printf("%p\n", "a");
printf("&a=%p &b=%p\n", &a, &b);
printf("%p\n", p);
p[0]='H';
puts(p);
return 0;
}
/*
0x8048544
&a=0x80496fc &b=0xbff9aaa8//a是全局变量存储在全局静态去,b是局部变量,存储在栈中,所以地址相差很远!
0x8048534
段错误 (core dumped) //p[0] 是常量,不可以改变
* */
test2
1 #include <stdio.h>
2 #include <stdint.h>//
3
4
5 int main()
6 {
7 int a;
8
9 // if ( 0 = a ) {//test2.c:8: 错误:赋值运算的左操作数必须是左值
10
11
12 printf("%d %d %d %d\n", sizeof(int8_t), sizeof(int16_t),
13 sizeof(int32_t), sizeof(int64_t)); //1 2 4 8
14
15 return 0;/////man
16 }
~ //有问题不会 建议自己man一下 man的详细用法在分享转载分类中对应的文章
stdint.h
test3
#include <stdio.h>
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <sys/wait.h>
5
6 int main()
7 {
8 pid_t pid;
9 printf("%d %d\n", getpid(), getppid());
10
11 pid = fork();
12 if ( pid == 0 ) {
13 //child process
14 printf("<pid==0> %d %d\n", getpid(), getppid());
15 return 257;
16 }
17 else {
18 int status;
19 //parent process
20 printf("<pid!=0> %d %d\n", getpid(), getppid());
21 wait(&status);
22 //printf("%d\n", (status>>8)&255);
23 printf("%d\n", WEXITSTATUS(status));//和23行等价 man
24
25 }
26
27 getchar();
28
29 return 0;
30 }
31 #if 0
32 redult:
33 5523 5343
34 <pid!=0> 5523 5343
35 <pid==0> 5524 5523
36 1
37 #endif
~
test4
1 ///优先级
2 #include <stdio.h>
3
4 int main()
5 {
6 int a=1, b=2, c=3, d;
7
8
9 d= (a, b, c);
10 // d= a, b, c; //d==1;
11
12 printf("d=%d\n", d);//:3
13
14 return 0;
15 }
test5
//goto用于统一的错误处理
2 #include <stdio.h>
3
4
5 int func(void)
6 {
7
8 ...
9
10 char *p1 = malloc(sizeof(char)*10);
11 if ( p1 == NULL ) {
12 goto ERR1;
13 }
14 ...
15
16 char *p2 = malloc(20);
17 if ( p2 == NULL ) {
18 goto ERR2;
19 }
20
21 ...
22
23 char *p200 = malloc(2000);
24 if ( p200 == NULL ) {
25 goto ERR200;
26 }
27
28 ...
29
30 char *p3 = malloc(30);
31 if ( p3 == NULL ) {
32 goto ERR3;
33 }
34
35
36 free(p3);
37 ERR3:
38 free(p200);
39 ERR200:
40 free(p2);
41 ERR2:
42 free(p1);
43 ERR1:
44 return 0;
45 }
test6 练习题练习:
1.ABCDE五个人去钓鱼,晚上各自去休息,第二天
A先起来,将鱼分成5份,恰巧多了一条丢掉,拿走了自己的一份
B起来,将鱼分成5份,丢掉一条 拿走自己的一份
CDE都是这样的 都以为自己第一个起床
请问:他们最少钓了多少鱼 ?
算法:穷举法
1 #include <stdio.h>
2
3 int main()
4 {
5 int people = 5;
6 int fish=6, i=0, tmp;
7
8 for ( ; ; fish++)
9 {
10 tmp = fish;
11 for ( i=0; i<people; i++)
12 {
13 if ( tmp%people != 1 )
14 {
15 break;
16 }
17 tmp=tmp-1-tmp/5;
18 }
19 if ( i == people )
20 {
21 break;
22 }
23 }
24 printf("fish=%d\n", fish);
25
26 return 0;
27 }
//自己写完一堆烂七八糟的东西 看老师半分钟敲完 感觉弱爆了 IQ IQ IQ
test7
1 //C 没有严格的越界检查
2 #include <stdio.h>
3
4 int main()
5 {
6 int loop=1 , arr[10] = {0}, i;
7 printf("&loop=%p &i=%p arr=%p\n", &loop, &i, arr);
8 getchar();
9 while (loop<4) {
10 for ( i=0; i<=10; i++) {
11 arr[i] = 1;//arr[10]=1越界出现意外使得: loop=1 导致死循环(不同机器可以结果不同
12 printf("1111111111\n");
13 }
14 loop++;
15 printf("222222222\n");
16 }
17
18
19 return 0;
20 }
test8练习:10个人站一圈,从第一个人开始数数,数到3的人退出,下一个人重新数。请问最后剩下的是第几个人。
//int arr[10] = {0}; //0表示在圈里 1表示出去了
1 //练习2
2 #include <stdio.h>
3
4 int main()
5 {
6 int arr[10] = {0};
7 int count=0, i = 0;
8 int num = 0;
9
10 for ( i=0; num<9; i=(i+1)%10) {
11 if ( arr[i] == 0 ) {
12 count++;
13 if ( count == 3 ) {
14 arr[i] = 1;
15 count = 0;
16 num++;
17 }
18 }
19 }
20
21 for ( i=0; i<10; i++ ) {
22 if ( arr[i] == 0 ) {
23 printf("i=%d\n", i);
24 break;
25 }
26 }
27 return 0;
28 }