6.初识c语言——常见关键字、#define定义常量和宏

常见关键字

关键字是c语言本身内置的,关键字不是自己创造出来的,也不能由自己创建。

变量的名字不能是关键字

 

auto  break   case   char  const-(常属性)  continue  default  do  double  else  enum-(枚举)   extern-(声明外部符号)  float  for  goto  if  int   long  register-(寄存器)   return-(函数返回值)   short  signed-(有符号的)  sizeof-(计算大小的)  static-(静态的)  struct-(结构体)  switch  typedef-(类型重命名)  union-(联合体)   unsigned -(无符号的)  void-(空/无<函数的返回类型>)  volatile  while

 

auto是一个被省略的关键词,自动创建,自动销毁。auto int a=10;

break是用来跳出循环的。用于for循环、while循环、do while循环;

case也是用于跳出循环。用于swtich循环。

 

变量的命名

1.有意义

Int age;

Float salary;

 

2.名字必须是字母、数字、下划线组成。

不能有特殊字符,同时不能以数字开头。

Int 2b; //err

Int _2b;//ok

 

3.变量名不能是关键字

1.关键字typrdef

顾名思义就是类型定义,可以理解为类型重命名。

代码

typedef unsigned int uint;        //重新命名类型,为了更加方便

typedef struct Node

{

int data;

struct Node* next;

}Node;

 

int main()

{

unsigned int num = 0;

uint num1 = 1;

struct Node n;

Node n2;

 

return 0;

}

 

2。关键字static

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

1.修饰局部变量--称为静态局部变量

static修饰局部变量时,局部变量出了作用域,不销毁。

本质上,static修饰局部变量时,改变的变量的储存位置。

影响了变量的生命周期,生命周期变长,和程序生命周期一样。

(局部变量原先放置于栈区,加入static后成为静态局部变量,存放在静态区。)

2.修饰全局变量--称为静态全局变量

static修饰全局变量的时候,这个全局变量的外部链接属性就变成了内部连接属性。

其他的源文件(.c)就不能再使用到这个全局变量。

3.修饰函数--称为静态函数

static修饰函数的时候,这个函数的外部链接属性就变成了内部连接属性。

其他的源文件(.c)就不能再使用到这个函数。

 

代码

 

void test()

{

int a = 1;//a为局部变量,使用完后自动销毁

    -->a=1-->a=2==>销毁-->a=1-->a=2;

a++;

printf("%d ", a);

}

int main()

{

int i = 0;

while (i < 10)

{

test();

i++;

}

return 0;

}

 

void test()

{

static int a = 1;        //a为静态局部变量,使用完后不自动销毁

                                -->a=1-->a=2-->a=3-->a=4;

a++;

printf("%d ", a);

}

int main()

{

int i = 0;

while (i < 10)

{

test();

i++;

}

return 0;

}

 

void定义的函数不需要返回,也就是return

 

3.关键字register

 

int main()

{

//寄存器变量

register int num = 3;        //建议:3存放在寄存器中

 

return 0;

}

#define定义常量和宏

1. define定义标识常量

 

#define NUM 100

int main()

{

printf("%d\n", NUM);

int n = NUM;

printf("%d\n", n);

int arr[NUM] = { 0 };

return 0;

}

 

 

 

 

2. define定义宏

宏有参数

#define ADD(x,y) ((x)+(y))         //ADD为宏名,x,y为参数,((x)+(y))为宏体

int main()

{

int a = 10;

int b = 20;

int c = ADD(a, b);

printf("%d\n", c);

return 0;

}

 

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值