1.#include<stdio.h> //include库函数 standard input output 标准输入输出函数
输入: scanf
#include<stdio.h>
Int main()
{
Int num1=0;
Int num2=0;
Int sum=0;
Scanf(“%d%d”,&num1,&num2); //输入 把0 0放到num1num2的地址内
Sum=num1+num2;
Printf(“sum = %d\n”,sum);
Return 0;
}
输出:printf
2.Char -字符类型 char a=”A”; 向内存申请内存空间
Short age=20;向内存申请两个字节 short申请 16个比特位
%d - 打印整型; %c - 打印字符; %f - 打印浮点数;
%p -以地址的形式打印; %x - 打印16进制数字
Printf(“%d\n”,sizeof(int/short/long/double/char/));看各类型的内存空间。 char -1字节;short-2字节;int-4字节;long-4/8字节
Longlong-8字节; float-4字节;double-8字节;
Bit-比特位 放一个二进制 0 1
Byte-字节 1字节=8个比特位
Kb 1kb=1024个字节
Mb 1mb=1024kb
3. 类型是来创建变量的
局部变量:生命周期在作用域内; 全局变量:生命周期为整个程序。
#include<stdio.h> #include<stdio.h> #include<stdio.h>
Int a=10;//全局 int main() int main()
Int main() { {
{ int a=10; //局部 int a=10;//局部 作用域只在
Printf(“%d\n”,a); printf(“%d\n”,a); } main函数中
} //ok } //ok printf(“%d\n”,a); //error
<C语言中定义变量要放在前面>
4.常量
(1)字面常量:一个数字值3 4
(2)Const修饰的常变量,int赋值后就不可以在修改了
Const int n=10; n为变量加上const 为常变量 依旧为变量
Int arr[n]={0}; // error所以内存无法分配
Int arr[10]={0}; //OK
n=20; //error const修饰不可改变值
(3)#define 定义的标识符常量
#define MAX 10
Int main()
{
Int arr[MAX]={0};
Printf(“%d\n”,MAX);
Return 0;
}
(4)枚举常量(枚举常量不可改变) 枚举关键–enum
Enum
{
Red; //系统内部自动赋值为 0
Yellow; // 1
Blue; // 2
}
Int main()
{
enum Color color=Blue;
Color = Yellow; //变量可以改值
Blue =6; //error 不可改变枚举常量由系统自动赋值 不可改变
Return 0;
}
C语言学习
最新推荐文章于 2024-08-05 23:28:12 发布