1.标识符常量和宏函数(宏函数是简单替换所以需要把括号加到位)
#include<stdio.h>
#define MAX 1000//标识符常量
#define num 10
//#define SUM(X,Y) X+Y //不对
#define SUM(X,Y) ((X)+(Y))
int max(int a, int b)
{
return a>b?a:b;
}
int main()
{
int m = MAX;
//NUM = 20;标识符常量不能修改
printf("%d\n",m);
int a=10,b=20;
int sum = 10*SUM(a,b);//打印120宏替换是单纯替换
printf("%d\n",sum);
return 0;
}
2.指针语法
#include<stdio.h>
//32跟地址线\数据线,高低电平对应01二进制位,所有2^32个地址编号
//一个编号管理一个字节的空间,所以产生了内存地址的概念
int main()
{
int a = 10;
int* pa = &a;
char ch='w';
char* pch = &ch;//取地址
*pa = 20;//解引用
printf("%d\n",a);
printf("%d\n",sizeof(a));//系统a开辟的空间,首字节地址的编号即为a的地址
printf("%p\n",&a);//打印a的地址
return 0;
}
#include<stdio.h>
#include<unistd.h>
int main()
{
int a = 10;
char ch ='C';
int* pa = &a;
char* pc = &ch;
printf("sizeof(pa)=%p\n",(int) sizeof(pa));
printf("sizeof(pc)=%p\n",(int)sizeof(pc));
return 0;
}
3. 结构体变量和结构体指针:
#include<stdio.h>
#include<unistd.h>
struct Stu
{
char name[20];
short age;
char sex[5];//一个汉字需要两个字符空间
char id[13];
};
int main()
{
int a = 10;
struct Stu s={"张三",20,"男","20221111"};
struct Stu* ps = &s;
printf("%s,%d,%s,%s\n",ps->name,ps->age,ps->sex,ps->id);
//printf("%s,%d,%s,%s\n",s.name,s.age,s.sex,s.id);
return 0;
}
4.指针的使用
#include<stdio.h>
void Swap(int* x, int* y)
{
int temp = *x;
*x=*y;
*y=temp;
}
int main()
{
int a=10,b=20;
Swap(&a,&b);
printf("a=%d,b=%d\n",a,b);
return 0;
}
5.if else
#include<stdio.h>
int main()
{
int age = 60;
//if(age>=18)
// printf("成年\n");
// else
// printf("未成年\n");
if(age<18)
printf("未成年\n");
else if(18<=age && age<30)
printf("青年\n");
else if(30<=age && age<50)
printf("中年\n");
return 0;
}
6.switch case
#include<stdio.h>
int main()
{
printf("please input day\n");
int day;
scanf("%d",&day);
switch(day)
{
case 1:
printf("星期1\n");
break;
case 2:
printf("星期2\n");
break;
case 3:
printf("星期3\n");
break;
case 4:
printf("星期4\n");
break;
case 5:
printf("星期5\n");
break;
case 6:
printf("星期6\n");
break;
case 7:
printf("星期日\n");
break;
default:
break;
}
return 0;
}
#include<stdio.h>
int main()
{
printf("please input day\n");
int day;
scanf("%d",&day);
switch(day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("星期5\n");
break;
case 6:
case 7:
printf("星期日\n");
break;
default:
printf("日期输入错误\n");
break;
}
return 0;
}
7.循环
#include<stdio.h>
int main()
{
int num=1;
while(num<=100)
{
if(num%2==1)
{
printf("%d ",num);
}
num++;
}
printf("\n");
return 0;
}