C语言入门课程学习笔记8
- 第36课 - 变量的作用域与生命期(上)
- 第37课 - 变量的作用域与生命期(下)
- 实验—局部变量的作用域
- 实验-变量的生命期
- 第38课 - 函数专题练习
- 第39课 - 递归函数简介
- 实验
- 小结
- 第40课 - C 语言中的宏定义
- 实验
- 小结
第36课 - 变量的作用域与生命期(上)
#include <stdio.h>int var = 100; // 全局变量void f(int var) // var <==> 局部变量
{var++;printf("var = %d\n", var);
}int main()
{int var = 10; // 局部变量f(var); // f(10);printf("var = %d\n", var); // var = 10;return 0;
}
// var = 11
// var = 10
#include <stdio.h>int var = 100; // 全局变量int main()
{int var = 10; // 局部变量{int var = 1; // 局部变量printf("var = %d\n", var);}printf("var = %d\n", var); // var = 10;return 0;
}
var = 1
var = 10
#include <stdio.h>int main()
{int i = 0; // 局部变量while( i < 5 ){int j = 10; // 局部变量j++;printf("j = %d\n", j);i++;}printf("i = %d\n", i); // var = 10;return 0;
}
/*
j = 11
j = 11
j = 11
j = 11
j = 11
i = 5
*/
第37课 - 变量的作用域与生命期(下)
实验—局部变量的作用域
#include <stdio.h>int var = 1;//全局变量void func()
{printf("var = %d\n", var);
}int main()
{int var = 2;//局部变量1int i = 0;for(i=0; i<5; i++){int var = 4;//局部变量2,作用域只在{}内var += i;printf("var = %d\n", var);}func();//函数调用,打印的是全局变量printf("var = %d\n", var);//局部变量1return 0;
}
/*
var = 4
var = 5
var = 6
var = 7
var = 8
var = 1
var = 2*/
实验-变量的生命期
#include <stdio.h>int g_var = 1;
static int g_sVar = 2;int main()
{static int s_var = 3;auto int v = 4;register int rv = 5;printf("g_var = %d\n", g_var);//1printf("g_sVar = %d\n", g_sVar);//2printf("s_var = %d\n", s_var);//3printf("v = %d\n", v);//4printf("rv = %d\n", rv);//5return 0;
}
第38课 - 函数专题练习
编译通过,有警告,编译器默认为int
B
如果改成int func(float var){var+=1;} ,返回随机值
C
x=5,y=8, 13+7
B
搞错了z–是先取值,再减一
C 13+8
A
A
12
15
8 10
4
DELPHI TANG
#include <stdio.h>int getNumber(char c)//字符转换为数字
{int ret = -1;if( ('0' <= c) && (c <= '9') )ret = c - '0';return ret;
}int str2int(char str[])
{int ret = 0;int sign = 0;int i = 0;//首个数字的位置if( getNumber(str[0]) != -1 )//是数字 符号为+{sign = 1;i = 0;}else if( str[0] == '+' )//{sign = 1;i = 1;}else if( str[0] == '-' ){sign = -1;i = 1;}while( sign && str[i] ){int n = getNumber(str[i]);if( n != -1 )ret = ret * 10 + n;elsebreak;i++;}ret = sign * ret;return ret;
}int main()
{printf("%d\n", str2int("123"));printf("%d\n", str2int("-12345"));printf("%d\n", str2int("567xyz89"));printf("%d\n", str2int("abc"));printf("%d\n", str2int("-xyz"));return 0;
}
第39课 - 递归函数简介
实验
#include <stdio.h>int sum(int n)
{int ret = 0;if( n == 1 )ret = 1;elseret = n + sum(n-1);return ret;
}int fac(int n)
{int ret = 0;if( n == 1 )ret = 1;else if( n == 2 )ret = 1;else if( n >= 3 )ret = fac(n-1) + fac(n-2);elseret = -1;return ret;
}int main()
{int i = 0;printf("sum(1) = %d\n", sum(1));printf("sum(10) = %d\n", sum(10));printf("sum(100) = %d\n", sum(100));for(i=1; i<=10; i++){printf("%d, ", fac(i));}printf("\n");return 0;
}
小结
第40课 - C 语言中的宏定义
实验
#include <stdio.h>#define ADD(a, b) a + b#define SWAP(a, b) {int t = a; a = b; b = t;}#define MIN(a, b) (a < b ? a : b)void swap(int a, int b)
{int t = 0;// printf("a = %d, b = %d\n", a, b);t = a;a = b;b = t;// printf("a = %d, b = %d\n", a, b);}int main()
{int x = 1;int y = 2;printf("x = %d, y = %d\n", x, y);SWAP(x, y); // {int t = x; x = y; y = t;}printf("x = %d, y = %d\n", x, y);printf("x + y = %d\n", ADD(x, y));printf("MIN(%d, %d) = %d\n", x, y, MIN(x, y)); // printf("MIN(%d, %d) = %d\n", x, y, (x < y ? x : y));return 0;
}