初识C语言-关键字(typedef、static),#define定义变量和宏,指针,结构体

关键字

关键字-typedef

typedef 顾名思义是类型定义,这里应该理解为类型重命名。

#include <stdio.h>

//将unsigned int 重命名为uint, 所以uint也是一个类型名
typedef unsigned int uint;

int main()
{
	//观察a和b,这两个变量的类型是一样的
	unsigned int a = 10;
	uint b = 20;
	printf("%d\n", a);
	printf("%d\n", b);
	return 0;
}
关键字-static

在C语言中:static是用来修饰变量和函数的

修饰局部变量-称为静态局部变量
修饰全局变量-称为静态全局变量
修饰函数-称为静态函数
修饰局部变量
//代码1
#include <stdio.h>    
void test()
{
	int a = 1;
	a++;
	printf("%d\n", a);
}
int main()
{
	int i = 0;
	while (i < 10)
	{
		test();
		i++;
	}
	return 0;
}
//代码2
#include <stdio.h>    
void test()
{
    //static修饰局部变量
	static int a = 1;
	a++;
	printf("%d\n", a);
}
int main()
{
	int i = 0;
	while (i < 10)
	{
		test();
		i++;
	}
	return 0;
}

对比代码1和代码2的效果理解static修饰局部变量的意义。
结论:

static修饰局部变量改变了变量的生命周期
让静态局部变量出了作用域依然存在,到程序结束,生命周期才结束。
修饰全局变量
//代码1
//add.c
int g_val = 411;
//test.c
#include <stdio.h>    
int main()
{
	printf("%d ", g_val);
	return 0;
}
//代码2
//add.c
static int g_val = 411;
//test.c
#include <stdio.h>    
int main()
{
  printf("%d\n", g_val);
  return 0;
}

代码1正常,代码2在编译的时候会出现连接性错误。
结论:

一个全局变量被static修饰,使得这个全局变量只能在本源文件内使用,不能在其他源文件内使用。
修饰函数
//代码1
//add.c
int Add(int x, int y)
{
  return c+y;
}
//test.c
#include <stdio.h>    
int main()
{
  printf("%d\n", Add(2, 3));
  return 0;
}
//代码2
//add.c
static int Add(int x, int y)
{
  return c+y;
}
//test.c
#include <stdio.h>    
int main()
{
  printf("%d\n", Add(2, 3));
  return 0;
}

代码1正常,代码2在编译的时候会出现连接性错误.
结论:

一个函数被static修饰,使得这个函数只能在本源文件内使用,不能在其他源文件内使用。

#define 定义常量和宏

//define定义标识符常量
#define MAX 100
#include <stdio.h>
int main()
{
	printf("%d\n", MAX);
	int a = MAX;
	printf("%d", a);
	int arr[MAX] = { 0 };

	return 0;
}
#define ADD(x,y)((x)+(y))
#include <stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	int c = ADD(a,b);
	printf("%d\n", c);
	c = 10 * ADD(a, b);
	printf("%d\n", c);
	return 0;
}

指针

内存

内存是电脑上特别重要的存储器,计算机中程序的运行都是在内存中进行的 。
所以为了有效的使用内存,就把内存划分成一个个小的内存单元,每个内存单元的大小是1个字节
为了能够有效的访问到内存的每个单元,就给内存单元进行了编号,这些编号被称为该内存单元的地

变量是创建内存中的(在内存中分配空间的),每个内存单元都有地址,所以变量也是有地址的。
取出变量地址如下:

#include <stdio.h>
int main()
{
	int num = 10;
	&num;
    //取出num的地址
  	//注:这里num的4个字节,每个字节都有地址,取出的是第一个字节的地址(较小的地址)
	printf("%p\n", &num);
    //打印地址,%p是以地址的形式打印
	return 0;
}

那地址如何存储,需要定义指针变量。

int num = 10;
int *p;
//p为一个整形指针变量
p = &num;

指针的使用实例:

#include <stdio.h>
int main()
{
	int num = 10;
	int *p = &num;
	*p = 20;
	return 0;
}

以整形指针举例,可以推广到其他类型,如:

#include <stdio.h>
int main()
{
	char ch = 'w';
	char* pc = &ch;
	*pc = 'q';
	printf("%c\n", ch);
    return 0;
}
指针变量的大小
#include <stdio.h>
//指针变量的大小取决于地址的大小
//32位平台下地址是32个bit位(即4个字节)
//64位平台下地址是64个bit位(即8个字节)
int main()
{
	printf("%d\n", sizeof(char *));
  	printf("%d\n", sizeof(short *));
  	printf("%d\n", sizeof(int *));
  	printf("%d\n", sizeof(double *));
  	return 0;
}

结论:指针大小在32位平台是4个字节,64位平台是8个字节。

结构体

结构体是C语言中特别重要的知识点,结构体使得C语言有能力描述复杂类型。
比如描述学生,学生包含: 名字+年龄+性别+学号 这几项信息。
这里只能使用结构体来描述了。
例如:

struct Stu
{
	char name[20];//名字
  	int age;    //年龄
  	char sex[5];  //性别
  	char id[15]//学号
};

结构体的初始化:

//打印结构体信息
struct Stu s = {"张三"20"男""20180101"};
//.为结构成员访问操作符
printf("name = %s age = %d sex = %s id = %s\n", s.name, s.age, s.sex, s.id);
//->操作符
struct Stu *ps = &s;
printf("name = %s age = %d sex = %s id = %s\n", ps->name, ps->age, ps->sex, ps->id);
  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bowen…

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值