当前位置:网站首页>重温c语言一
重温c语言一
2022-06-11 21:45:00 【yjhklxx】
#include<stdio.h> //define,include是预处理指令
int main() //仅能有一个main
{
printf("hello");//函数调用操作符() printf 打印
return 0;
}类型及大小:
char 字符数据类型 大小 1 字节 %c
short 短整型 2 %d
float 单精度浮点数 小数 4 %f
double 双精度浮点数 8 %lf
long 长整型 4 %d
int 整形 4 %d
long long 更长的整型 8 %d
局部变量和全局变量:在大括号{}内为局部变量,外为全局变量,两者名字相同时局部优先
#include<stdio.h>
int b=0 //全局变量
int main()
{
int a=0 //局部变量
return 0;
}范例:求和
#include<stdio.h>
int main()
{
int a;
int b;
int sum; //scanf是输入函数
scanf("%d %d", &a, &b); //一个%d对应一个整数第一个整数放到a,第二个放到b
sum = a + b;
printf("sum=%d\n", sum);
}作用域:一段程序代码中所用到的名字并不总是有效可用的,
而限定这个名字的可用性的代码范围就是这个名字的作用域。
局部变量的作用域:就是变量的局部范围
全局:整个工程
生命周期:变量的创建和销毁之间的时间段
局部变量:进入局部范围生命开始,出局部范围生命结束
全局变量:程序的生命周期
常量
#define=100 //定义的常量在内部定义也行
#include<stdio.h>
int main()
{
1字面常量
/*3
a
3.14*/
2 const修饰的常变量
const int sum=10//sum是常变量被const修饰不能修改
#define 定义的标识符常量
枚举常量 可以一一列举的
}
enum Sex 枚举关键词
{
这种枚举类型的变量的未来取值
MALE=3 //赋初值
FEMALE
SECRET
};
字符串就是一串字符——用双引号括起来的一串字符
字符串在结尾隐藏一个\0的字符
\0是字符串结束标志
求字串符的长度 strlen 用头文件 #include<string.h>
\0 不计算长度
#include<stdio.h>
int main()
{
"hello";
}//选择语句
#include<stdio.h>
int main()
{
int a = 0;
printf("请输入数字");
scanf("%d", &a);
if (a < 60)
printf("不及格");
else
printf("及格");
return 0;
}
//循环语句
#include<stdio.h>
int main()
{
int line = 0;
while (line < 3000)//循环
{
printf("好好学习:%d\n",line);
line++;
}
if (line == 3000)
{
printf("努力学习\n");
}
return 0;
}
//算数普通和函数
//普通
#include<stdio.h>
int main()
{
int sum1 = 0;
int sum2 = 0;
scanf("%d %d", &sum1, &sum2);
int sum = sum1 + sum2;
printf("%d\n", sum);
return 0;
}
//函数
#include<stdio.h>
int Add(int x, int y)
{
int z = 0;
z = x + y;
return z;
}
int main()
{
int sum1 = 0;
int sum2 = 0;
scanf("%d %d", &sum1, &sum2);
int sum = Add(sum1, sum2);
printf("%d\n", sum);
return 0;
}
//数组:一组相同类型的元素组合
//10整型
#include<stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
每个元素都有下标用时用下标如用1:arr[0]从0依次到9 下标引用操作符[]
存字符用char
想要全部访问
int i = 0;
while (i < 10)
{
printf("%d\n", i);
i++;
}
return 0;
}操作符
算数操作符:+,-,*,/,%(%是取模,也是取余)
移位操作符:>>(有移动符),<<(左移动符)
位操作符:&按位于,|按位或,^按位异或
赋值操作符=,+=,-=,*=,/=,&=,^=,|=,>>=,<<=
int a=2; a=a+5这也可以表达为a+=5其他一样
单目操作符:!逻辑反操作,-负值,+正直,&取地址,*间接访问操作符
sizeof操作数的类型长度,~对一个熟的二进制按位取反
++,--,(类型)强制类型转换
a+b +有两个操作数,双目操作符。单目操作符只有一个操作数
0表示假,1表示真,
#include<stdio.h>
int main()
{
printf("%d",sizeof(int......))//sizeof是计算大小括号能省略
if (a) 也能计算数组大小单位字节
{
a为真这个
}
if (!a)
{
a为假这个
}
}整数在内存中储存的的是补码。一个整数在二进制中有:原码;反码;补码
//~的头文件是include<stdio.h>
#include<stdio.h>
int main()
{
int a = 0;
printf("%d", ~a);//按位取反把所有二进制位中数字,1变成0,0变成1
return 0;
}负数的计算 -1 正整数的计算
10000000000000000000000000000001(原码) 原码,反码,补码相同
11111111111111111111111111111110(反码) 开头的表示正负
11111111111111111111111111111111(补码) 1为负,0为正
关于++和--
#include<stdio.h>
int main()
{
int a = 10;
int b = ++a;//前置++ 先++,后使用
printf("%d\n", b);//11
printf("%d\n", a);//11
return 0;
}#include<stdio.h>
int main()
{
int a = 10;
int b = a++;//后置++ 先使用,后++
printf("%d\n", b);//10
printf("%d\n", a);//11
return 0;
}--与++相同不在写
类型强制转换
#include<stdio.h>
int main()
{
int a = (int)3.14;//强制类型转换
printf("%d", a);
return 0;
} 操作符
大于 大于等于 小于 小于等于 不等于 等于
关于操作符: > >= < <= != ==
逻辑操作符:&&逻辑与(都是真才是真),||逻辑或(一个为真就是真)
条件操作符(三目操作符):? : exp1?exp2:exp3
exp1如果成立,exp2计算,整个表达式的结构:exp2的结果
exp1如果不成立,exp3计算,整个表达式的结构:exp3的结果逗号表达式:逗号隔开的一串表达式;从左向右依次计算
整个表达式的结果是最后一个结果
#include<stdio.h>
int main()
{
int a = 0;
int b = 3;
int max = 0;
if (a > b) 不用条件操作符
max = a;
else
max = b;
max = a > b ? a : b;//用条件操作符
printf("%d", max);
return 0;
}
#include<stdio.h>
int main()
{
int a = 1;
int b = 2;
int c = 3; //a=8 c=4 b=9
int d = (a = b + 6, c = a - 4, b = c + 5);//9
printf("%d\n", d);
return 0;
}结构成员. ->
c语言提供的关键字,不能做变量名
auto是自动——每个局部变量都是auto修饰,基本上省略掉
extern是用来声明外部符号
register寄存器关键字
static静态的 union联合体 void无
常见关键字auto,break,case,char,const,continue,default,do,
if,else,double,enum,extern,flaot,for,goto,long,int,register
return,short,signed,sizeof,static,struct,switch,union
typedef,unsigned,void,volatile,while
typedef 类型定义 类型重定义
#include<stdio.h>
typedef unsigned int u_int;
int main()
{
unsigned int num1 = 100;
u_int num2 = 100; //这俩一样
return 0;
}static静态的 //全局变量在整个工程中都能应用当设一个全局变量在另一个文件中要用extern来声明
1.修饰局部变量;2.修饰全局变量;3.修饰函数
修饰局部变量改变局部变量的生命周期(本质上改变了变量的存储类型)
当修饰全局变量时,修饰的那个变量只能在自己所在的源文件内部使用其它源文件不能使用
全局变量在其他源文件能使用是因为他具有外部链接属性
被static修饰后变成了内部连接属性其它源文件不能接收到这个静态的全局变量
修饰函数与全局变量类似
#include<stdio.h>
void test()
{
static int i = 1;//没用static十个2
i++; //用了2~11
printf("%d ", i);
}
int main()
{
int a = 0;
while (a < 10)
{
test();
a++;
}
return 0;
}
//#define定义常量和宏 预处理指令
//define定义符号(常量)
#include<stdio.h>
#define max 1000
int main()
{
printf("%d", max);
return 0;
}//define定义宏 宏是替换
#include<stdio.h>
#define Add(x,y) ((x)+(y))
int main()
{ //无4*时是11
printf("%d", 4*Add(5, 6));//44
return 0;
}初始指针
指针就是地址;使用指针式就是使用指针里面的地址
#include<stdio.h>
int main()
{
int a = 10;//a在内存中分配空间-4个字节
printf("%p\n", &a);//%p专门来打印地址的 &为取地址操作符
int* pa = &a;//pa是用来存放地址的,在c语言中pa叫做指针变量
//*说明pa是指针变量
//int说明pa执行的对象是int类型的;为char时是char类型
*pa = 20; //*解引用操作 *pa是通过pa里的地址找到a
printf("%d", a);
return 0;
}#include<stdio.h> //指针大小在32位平台是4个字节(byte),在64位平台是8个字节(byte)
int main()
{
printf("%d\n", sizeof(char*)); //4
printf("%d\n", sizeof(short*)); //4
printf("%d\n", sizeof(int*)); //4
printf("%d\n", sizeof(long*)); //4
printf("%d\n", sizeof(long long*)); //4
printf("%d\n", sizeof(double*)); //4
printf("%d\n", sizeof(float*)); //4
return 0;
}结构体可以让c语言创建新的类型出来
#include<stdio.h>
struct stu
{
char name[20];//指定大小
int age;
double score;
};
int main()
{
struct stu s = { "小三",20,68.5 };//结构体初始化
printf("1:%s %d %lf\n", s.name, s.age, s.score);//结构体变量、成员变量
struct stu* pa = &s;
printf("2:%s %d %lf\n", (*pa).name, (*pa).age, (*pa).score);
printf("3:%s %d %lf\n", pa->name, pa->age, pa->score);
//1,2,3的打印结果相同;2与3使用了指针
return 0;
}边栏推荐
- How to realize double speed playback and fast forward for restricted ckplayer players
- win11怎么看电脑显卡信息
- 揭秘爆款的小程序,为何一黑到底
- 类与对象(3)
- Nmap performs analysis of all network segment IP survivals in host detection
- How to use RPA robot to start the first step of digital transformation of freight forwarding industry?
- 每日一题-删除有序数组的重复项
- Win10弹出USB时出现该设备正在使用的解决方法
- Cdr2022 serial number coreldraw2022 green key
- 二分查找 - 学习
猜你喜欢

Matlab: solution of folder locking problem

The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace
![[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)](/img/73/2ec957d58616d692e571a70826787f.jpg)
[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)

Leetcode-43- string multiplication

Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.4

R language book learning 03 "in simple terms R language data analysis" - Chapter 10 association rules Chapter 11 random forest

The network connection is normal, but Baidu web page can not be opened and displayed. You can't access this website solution

Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?

Leetcode-322- change exchange
![[niuke.com] ky41 put apples](/img/55/cc246aed1438fdd245530beb7574f0.jpg)
[niuke.com] ky41 put apples
随机推荐
Nmap performs analysis of all network segment IP survivals in host detection
crontab中定时执行shell脚本
带有 ceph-csi 的静态 PVC
Leetcode-43- string multiplication
win10字体模糊怎么调节
每日一题-删除有序数组的重复项
Popular science | what are the types of NFT (Part 1)
Introduction à endnotex9 et instructions pour les tutoriels de base
为什么需要微服务
Cdr2022 serial number coreldraw2022 green key
Go encoding package
Classes and objects (4)
C语言实现八种排序 - 归并排序
How to view the installation date of the win system
Relatively perfect singleton mode
R语言书籍学习03 《深入浅出R语言数据分析》-第十章 关联规则 第十一章 随机森林
Leetcode-155-minimum stack
206.反转链表
快速排序的优化
D. Game With Array