1.static定义静态变量用法
分析:
静态存储方式:指在程序运行时,给变量分配固定的存储空间的方式
全局变量
:在程序开始执行时给全局变量分配存储区,程序运行完毕之后释放。在程序运行过程中它们占据固定的存储单元而不动态进行分配和释放。静态变量
:有时希望变量的值在函数调用结束后不消失而保留原值,这时就应该指定变量为“静态变量”,用关键字static
进行命名
源代码:
#include<stdio.h>
#include<stdlib.h>
void func()
{
int var = 0;
static int static_var = 0;
printf("\40:var equal %d\n",var);
printf("\40:static var equal %d\n",static_var);
printf("\n");
var++;
static_var++;
}
int main()
{
for(int i=0;i<3;i++)
{
func();
}
system("pause");
return 0;
}
运行结果:
2.使用auto定义变量用法
分析:
源代码:
#include<stdio.h>
int main()
{
int i, num;
num = 2;
for (i = 0;i < 3;i++)
{
printf("\40: The num equal %d \n", num);
num++;
{
auto int num = 1;
printf("\40: The internal block num equal %d \n", num);
num++;
}
}
}
运行结果:
3.使用static的另一种用法
分析:
源代码:
#include<stdio.h>
int main()
{
int i, num;
num = 2;
for (i = 0;i < 3;i++)
{
printf("\40: The num equal %d \n", num);
num++;
{
static int num = 1;
printf("\40: The internal block num equal %d \n", num);
num++;
}
}
}
运行结果:
4.使用external的用法
分析:
源代码:
#include<stdio.h>
#include<stdlib.h>
int a, b, c;
void add()
{
int a;
a=3;
c = a+b;
}
int main()
{
a=b=4;
add();
printf("The value of c is equal to %d\n", c);
system("pause");
return 0;
}
运行结果:
5.使用register定义变量方法
分析:
register变量必须是能被CPU所接受的类型
不能用“&”来获取register变量的地址
局部静态变量不能定义为寄存器变量
不能定义任意多个寄存器变量
源代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
register int i;
int temp=0;
for(i=1;i<=100;i++){
temp+=i;
printf("The sum is %d\n",temp);
}
}
运行结果:
6.宏#define命令练习(1)
参考来源:http://t.csdnimg.cn/C6xym
源代码:
#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
#define SQ(x) (x)*(x)
int main()
{
int num;
int again = 1;
printf("\40: Program will stop if input value less than 50.\n");
while(again) {
printf("\40:Please input number==>");
scanf("%d", &num);
printf("\40:The square for this number is %d \n", SQ(num));
if(num >= 50) {
again =TRUE;
} else {
again = FALSE;
}
}
getch();
}
运行结果:
7.宏#define命令练习(2)
源代码:
#include "stdio.h"
#include "conio.h"
// 宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"
// 就是说:两行命令以上组成的一个宏定义需要用\来进行连接
#define exchange(a,b) { \
int t;\
t=a;\
a=b;\
b=t;\
}
int main(void)
{
int x = 10;
int y = 20;
printf("x = %d; y = %d\n", x, y);
exchange(x, y);
printf("x = %d; y = %d\n", x, y);
getch();
}
运行结果:
8.宏#define命令练习(3)
源代码:
#include<stdio.h>
#include<conio.h>
#define LAG >
#define SMA <
#define EQ ==
int main(void)
{
int i = 10;
int j = 20;
if(i LAG j) {
printf("\40: %d larger than %d \n",i,j);
} else if(i EQ j) {
printf("\40: %d equal to %d \n",i,j);
} else if(i SMA j) {
printf("\40:%d smaller than %d \n",i,j);
} else {
printf("\40: No such value.\n");
}
getch();
}
运行结果:
9.#if#ifdef和#ifndef的综合应用
源代码:
// 可以看一下这篇文章
// https://blog.csdn.net/wordwarwordwar/article/details/84932183
// #ifdef 判断某个宏是否被定义,若已定义,执行随后的语句
// #ifndef 与#ifdef相反,判断某个宏是否未被定义
// #else 与#if, #ifdef, #ifndef对应, 若这些条件不满足,则执行#else之后的语句,相当于C语法中的else
// #endif #if, #ifdef, #ifndef这些条件命令的结束标志.
// defined 与#if, #elif配合使用,判断某个宏是否被定义
#include "stdio.h"
#include "conio.h"
#define MAX
#define MAXIMUM(x, y) (x > y) ? x : y
#define MINIMUM(x, y) (x > y) ? y : x
int main()
{
int a = 10, b = 20;
#ifdef MAX
printf("\40: The larger one is %d\n", MAXIMUM(a, b));
#else
printf("\40: The lower one is %d\n",MINIMUM(a, b));
#endif
#ifndef MIN
printf("\40: The lower one is %d\n",MINIMUM(a, b));
#else
printf("\40: The larger one is %d\n",MAXIMUM(a, b));
#endif
// #undef 是在后面取消以前定义的宏定义
#undef MAX
#ifdef MAX
printf("\40: The larger one is %d\n",MAXIMUM(a, b));
#else
printf("\40: The lower one is %d\n",MINIMUM(a, b));
#endif
#define MIN
#ifndef MIN
printf("\40: The lower one is %d\n",MINIMUM(a, b));
#else
printf("\40: The larger one is %d\n",MAXIMUM(a, b));
#endif
getch();
}
运行结果:
10.#include的应用练习【未完整】
源代码:
main.c文件:
#include <stdio.h>
// 这个不能改为<test.h>
#include "test.h"
int main()
{
int i = 10;
int j = 20;
if(i LAG j) {
printf("\40: %d larger than %d \n", i, j);
} else if (i EQ j) {
printf("\40: %d equal to %d \n", i, j);
} else if (i SMA j) {
printf("\40:%d smaller than %d \n", i, j);
} else {
printf("\40: No such value.\n");
}
return 0;
}
test.h文件:
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
#define LAG >
#define SMA <
#define EQ ==
#endif // TEST_H_INCLUDED