1.常量、变量
2.字符串、转义字符、注释
3.选择语句
4.函数
5.数组
6.操作符
7.关键字
8.指针
9.结构体
1.1常量:
常量可以分为以下几种:
.字面常量如:3.14,5,-10,“abcdrd”;
.1.2const:
修饰的常变量:这时候const修饰的相对于一个常属性的变量,下面的num1本质还是一个变量,但是由于const修饰所以变成了常变量。
#include <stdio.h>
int main()
{
//const修饰的常变量
int num = 10;
num = 20;//num是变量可以发生变化
printf("%d\n",num);
const int num1 = 20;//当用const修饰,就不能发生改变。
num1 = 70;
printf("%d",num1);
return 0;
}
.1.3#define
定义的标识符常量:写法——#define MAX(自己定义)1000(自己输入的数值)
#include <stdio.h>
#define num 5
int main()
{
int arr[num]={};//这里与const有所不同!!!
return 0;
}
.1.4枚举常量——一一列举:enum是枚举的关键字
#include <stdio.h>
enum colok
{
red,//0 里面的内容用逗号,隔开!!!
yellow,//1
blue,//2 里面的初始值可以修改
green=9 //这时green值就为9
};
int main()
{
enum colok s=red;
enum colok s1=blue;
printf("%d %d\n",s,s1);//默认输出red yellow blue 为0 1 2.所以s为0,s1为2
return 0;
}
2.1字符串
用一对单引号引起来的就是字符如:'A','abc','$','3'(字符3,区分数字3),电脑是用二进制输出的。一对双引号引起来的叫字符串如:“zhangxi”,"大大"。注意:字符串的结束标志为\0的转义字符,是结束标志,不算内容
#include <stdio.h>
int main()
{
//字符串结束标志为/0
char arr[]={"abcde"};
char arr1[]={'a','b','c','d','f'};
printf("%s",arr);输出为abcde
printf("%s",arr1);s输入为错误形式,这是因为字符串以/0为结束标志,如果字符后面加上'/0',就是一样的结果
return 0;
}
.2.2转义字符——转变意思
.2.3注释
不会在编译器上出现的内容,不需要的部分,可以删除也可以注释。有难懂的代码可以注释解释文字。可以用//注释掉单行内容,/* */可以单行也可以多行,不支持嵌套。
3.1选择语句——用if switch语句
if语句
#include <stdio.h>
int main()
{
int a=0;//if语句
int b=0;
scanf("%d %d",&a,&b);//取地址就是可以自己在编译器上输入内容
if(a>b)
printf("haha\n");
else
printf("hehe\n");
return 0;
}
switch语句
.4.1函数
#include <stdio.h>
int add(int x, int y)//函数求两个数相加
{
int z = x + y;
return z;
}
int main()
{
int a=0;
int b=0;
scanf("%d %d",&a,&b);
int sum = add(a,b);
printf("%d",sum);
return 0;
}
.5.1数组定义
int arr[5]={1,2,3,4,5};//定义一个整型数组,最多存放5个元素。
.5.2数组的下标
C语言规定:数组的每一个元素都有一个下标,下标从0开始
int arr[10]={0};//有十个元素,但是内容都为0
.6.1操作符
算数操作符:+ - * / %
#include <stdio.h>
int main()
{
// /(除号)两端的操作符都是整数执行的是整数除法,至少有一个操作符是浮点型执行的才是浮点型的除法
int a=7/2; //结果是3
float b=7/2; //结果还是3.000000
float c=7/2.0; //结果是3.500000
printf("%d %d %d\n",a,b,c);
//%(取整符号就是余数部分),两个操作符必须都是整数
return 0;
}
移位操作符:>> <<
单目操作符:
! 逻辑反操作
& 取地址
sizeof 操作符的类型长度(以字节为单位)
~ 对一个二进制按位取反
-- 减号在前,先减后用。减号在后,先用后减
++ 加号在前,先加后用。加号在后,先用后加
#include <stdio.h>
int main()
{
int a=5;
//printf("%d %d",a++,a);//这时a++为5,a为6
printf("%d %d",++a,a);//++a为6,a为6
return 0;
}
条件操作符:exp1?exp2:exp3 ,exp4 解释为:比较exp1与exp2的大小,如果exp1大结果就为exp3,否则为exp4。 可以理解为if条件语句
#include <stdio.h>
int main()
{
//条件操作符——可以理解为if语句
int a=3;
int b=0;
if(a>5)
b=-1;
else
b=1;
//相当于(a>5)?(b=-1):(b=1)
//b=(a>5)?-1:1
return 0;
}
逗号表达式:exp1,exp2,exp3,exp4......expn
注意:逗号表达式计算是从左往右计算,结果为表达式最后一个表达式的结果
#include <stdio.h>
int main()
{
int a = 3;
int b = 5;
int c = 0;
int d = (a+=2,b=b-c+a,c=a+b);
printf("%d %d %d %d",a,b,c,d);//结果为5 10 15 15
return 0;
]
.7.1关键字typedef
typedef:意思是类型定义,类型重命名
#include <stdio.h>
typedef unsigned int uint;
int main()
{
//typedef 类型重定义
unsigned int num;
uint num1;//num与num1类型一样的
return 0;
}
.7.2关键字static
static是用来修饰函数和变量的:
1.修饰局部变量称为静态局部变量
2.修饰全局变量称为静态全局变量
3.修饰函数称为静态函数
#include <stdio.h>
void test()
{
int a=3;当变成static int a=4 此时输出的结果为4 5 6 7 8 9 10 11 12 13 14
a++;
printf("%d",a);//结果为10个4.因为test这个函数的声明周期每一次就结束了,此时是一个局部变量
}
int main()
{
int i=0;
while(i<10)
{
test();
i++;
}
return 0;
}
.8.1指针
#include <stdio.h>
int main()
{
int a = 1;
int* pa = &a;//pa 是创建出来存放a地址的,pa叫做指针变量 地址=编号=指针 int* 整体是一个类型叫做整形指针类型
printf("%d\n",*pa);//结果为1
printf("%d\n", sizeof(*pa));//结果为4
return 0;
}