【C语言】32个关键词

目录

一、auto

二、short

三、int

四、long

五、float

六、double

七、char

八、struct

九、union

十、enum

十一、typedef

十二、const

十三、unsigned

十四、signed

十五、extern

十六、register

十七、stastic

十八、volatile

十九、void

二十、if

二十一、else

二十二、swich

二十三、case

二十四、for

二十五、do

二十六、while

二十七、goto

二十八、continue

二十九、break

三十、default

三十一、sizeof

三十二、return


一、auto

声明自动变量

  • 在任何类、结构、枚举、联合和函数中定义的变量视为局部变量,所有局部变量默认都是auto,一般省略不写。
#include<stdio.h>

int main()
{
	auto int a = 100;
	printf("%d",a);
}

但有的编译器不允许出现这种情况,会认为重复。

 

  • 只使用auto修饰变量,默认类型为整型。
#include<stdio.h>

int main()
{
	auto a = 100;
	printf("%d",a);
}

 

二、short

声明短整型变量或函数

#include<stdio.h>

int main()
{
	short a = 100;
	printf("%d",a);
}

short fun();

三、int

声明整型变量或函数

四、long

声明长整型变量或函数

五、float

声明浮点型变量和函数

六、double

声明双精度变量或函数

七、char

声明字符型变量和函数

八、struct

声明结构体变量或函数

struct student{
	char name[15];//名字
	int age;//年龄
	int grade;//年级
	int score;//成绩
};
struct student{
	char name[15];
	int age;
	int grade;
	int score;
};

struct student return_student(char* name,int *age,int* grade,int* score);

九、union

声明共用体变量

  • 共用体变量的字节长度跟内存最大的成员变量的长度有关,且为4的倍数
#include<stdio.h>

union student{
	char name[16];
	int age;
	int grade;
	int score;
};

int main()
{
	student stu;
	printf("%d\n",sizeof(stu));
}

 

#include<stdio.h>

union student{
	char name[15];
	int age;
	int grade;
	int score;
};

int main()
{
	student stu;
	printf("%d\n",sizeof(stu));
}

  • 共用体使用了内存覆盖技术,同一时刻只能保存一个成员的值,如果对新的成员赋值,就会把原来成员的值覆盖掉。
#include<stdio.h>

union student{
	char name[15];
	int age;
	int grade;
	int score;
};

int main()
{
	student stu;
	stu.age = 100;
	printf("%d\n",stu.age);
	stu.score = 650;
	printf("%d\n",stu.grade);
	printf("%d\n",stu.age);
}

 

 

十、enum

声明枚举型变量

枚举中的变量实际是整型。

#include<stdio.h>

enum Month {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,dec};


int main()
{
	Month m = Feb;
	printf("%d\n",m);
	if(m==Feb)
	{
		printf("二月\n");
	}
}

如果不写Jan=1的话,默认Jan = 0 

十一、typedef

给数据类型取别名

#include<stdio.h>

typedef enum Month {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,dec}abc;


int main()
{
	abc m = Feb;
	printf("%d\n",m);
}

 

十二、const

声明只读变量

#include<stdio.h>


int main()
{
	const int a = 100;
	a = 99;
}

如果企图更改a的值,就会发生错误

十三、unsigned

声明无符号类型变量或函数

#include<stdio.h>

int main()
{
	unsigned short a = -1;
	printf("%d",a);
}

 

十四、signed

声明有符号类型变量或函数

#include<stdio.h>

int main()
{
	signed short a = -1;
	printf("%d",a);
}

一般不写,因为默认。 

十五、extern

引用变量或函数

  • 引用本文件的全局变量变量或函数(位于main之后且main之前未声明)
#include<stdio.h>

int main()
{
	extern int a;
	printf("%d",a);
}

int a = 100;

  • 引用其它文件的全局变量或函数

三个文件: demo.h demo.cpp main.cpp

demo.h

#ifndef _DEMO_H_ 
#define _DEMO_H_
 
extern int a;
extern int b;
int add(int a, int b);
 
#endif
/*
#pragma  once 
extern int a;
extern int b;
*/

demo.cpp

#include "demo.h" 
int a = 10;
int b = 20;
 
int add(int p, int q)
{
    return p + q;
}

main.cpp

#include "demo.h"
#include<stdio.h>
 
int main()
{
	printf("%d",add(a,b));
}

 

 

十六、register

声明寄存器变量

更快

注意事项

  • 只能修饰局部变量,因为CPU的内存有限

  • CPU可以接受此变量
  • 不能使用&对变量取址
  • 不一定成功
 
#include<stdio.h>
 
int main()
{
	register int x = 10*10;
	printf("%d",x);
}

 

十七、stastic

声明静态变量

函数结束时,静态变量的值并未消去。

#include<stdio.h>

int fun()
{
	static int x = 0;
	x++;
	return x;
}

int main()
{
	printf("%d\n",fun());
	printf("%d\n",fun());
	printf("%d\n",fun());
	printf("%d\n",fun());
	printf("%d\n",fun());
}

 

十八、volatile

避免编译器优化而忽略该条指令

	volatile int a;

十九、void

声明无类型指针,声明函数无返回值或无参数

二十、if

条件语句

#include<stdio.h>

int main()
{
	int x = 1;
	if(x==1) printf("SUCCESS");
}

 

二十一、else

条件语句的分支

#include<stdio.h>

int main()
{
	int x = 2;
	if(x==1) printf("SUCCESS");
	else printf("FAIL");
}

 

二十二、swich

条件语句

#include<stdio.h>

int main()
{
	int n;
	scanf("%d",&n);
	switch (n) {
		case 1:case 3:case 5:case 7:case 8:case 10:case 12: printf("这月有31天");
			//TODO
			break;
		case 2:printf("这月有28天或29天");
			//TODO
			break;
		default: printf("这月有30天");
			//TODO
			break;
	}
}

 

二十三、case

参考二十二

二十四、for

循环语句

#include<stdio.h>

int main()
{
	for(int i=1;i<10;i++)
	{
		for(int j=1;j<=i;j++)
		{
			printf("%2d * %2d = %2d ",j,i,i*j);
		}
		printf("\n");
	}	
}

 

二十五、do

循环语句

#include<stdio.h>

int main()
{
	int count = 5;
	do{
		printf("The world is beautiful\n");
	}while(count-->0);
}

 

二十六、while

循环语句

#include<stdio.h>
 
int gcd(int a, int b) {
     int t;
     while(b!=0) {
         t=a%b;
         a=b;
         b=t;
     }
     return a;
}

int main()
{
	int a,b;
	printf("请输入a和b的值:\n");
	scanf("%d %d",&a,&b);
	printf("a和b的最大公约数为%d",gcd(a,b));
}

 

二十七、goto

跳转语句

#include<stdio.h>
#include<string.h>

int main()
{
	char password[16];
	char Password[16];
	strcpy(Password,"aaabbbcccdddeee");
	password[15] = '\0';
	A:
	printf("请输入您的密码(最多15位):\n");
	scanf("%s",&password);
	if(!strcmp(Password,password)){
		printf("密码正确\n");
	}
	else{
		printf("密码错误\n");
		goto A;
	}
	
}

 

二十八、continue

结束当前循环,进入下个循环

#include<stdio.h>

int main()
{
	for(int i=0;i<10;i++)
	{
		if(i==5) continue;
		else printf("%d\n",i);
	}
}

 

二十九、break

结束循环

#include<stdio.h>

int main()
{
	for(int i=0;i<10;i++)
	{
		if(i==5) break;
		else printf("%d\n",i);
	}
}

 

三十、default

参考二十二

三十一、sizeof

计算数据类型长度

#include<stdio.h>

int main()
{
	printf("%d",sizeof(int));
}

 

三十二、return

函数返回

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ོ栖落

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

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

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

打赏作者

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

抵扣说明:

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

余额充值