当前位置:网站首页>初识C语言 -- 操作符和关键字,#define,指针
初识C语言 -- 操作符和关键字,#define,指针
2022-07-28 01:28:00 【稚子】
C面向过程,C++面向对象
1.求两个数的较大值
注:自定义函数可以放在主函数下面,但是要在前面进行声明,放在主函数下面则不需要。
#include <stdio.h>
// 函数声明
int Max(int x, int y);
// int Max(int x, int y)
// {
// if (x > y)
// return x;
// else
// return y;
// }
int main()
{
int num1 = 10;
int num2 = 20;
int max = 0;
max = Max(num1, num2);
printf("max = %d\n", max);
return 0;
}
// 函数定义
int Max(int x, int y)
{
if (x > y)
return x;
else
return y;
}2.移位操作符 >> <<
(类型) 强制类型转换
int 为去除小数部分,不是四舍五入
sizeof 可以计算操作数的类型长度(以字节为单位)
* 间接访问操作符(解引用操作符)
~ 按位取反(二进制位)
#include <stdio.h>
int main()
{
int a = 0;
int b = ~a; // b为有符号的整型
printf("%d\n",b);
return 0;
}输出为-1,负数在内存中存储的为二进制的补码
注:只要是整数,内存中存储的都是二进制的补码
正数 原码、反码、补码 三者相同
负数 原码直接按照正负写出二进制,反码:原码的符号位不变,其他位按位取反,补码为反码+1
a++与++a
a--与--a
#include <stdio.h>
int main()
{
int a = 10;
int b = a++; //后置++,先使用,再++
printf("a = %d\n b = %d\n", a, b); // a = 11,b=10
return 0;
}#include <stdio.h>
int main()
{
int a = 10;
int b = ++a; //前置++,先++,后使用
printf("a = %d\n b = %d\n", a, b); // a = 11,b=11
return 0;
}3.计算数组内容的个数
#include <stdio.h>
int main()
{
int a = 10;
int arr[] = {1, 2, 3, 4, 5};
printf("%d\n",sizeof(a)); //int为4个字节
printf("%d\n",sizeof(int));
printf("%d\n",sizeof(arr)); //计算数组的大小,应该为4*5=20
printf("%d\n",sizeof(arr)/sizeof(int)); // 计算数组内容的个数,应为5
return 0;
}4.关系操作符
>
>= <=
逻辑与&&
5.条件操作符(三目操作符)
exp1? exp2:exp3;
表达式1结果为真,执行exp2,否则执行exp3
#include <stdio.h>
int main()
{
int a = 10;
int b =8;
int max = 0;
max = (a>b?a:b);
printf("%d\n",max);
return 0;
}6.逗号表达式
7.下标引用、函数调用和结构成员
[] () . ->
8.C语言中常见关键字

auto 一般用在局部变量中,通常省略
enum 枚举
register 寄存器关键字
#include <stdio.h>
int main()
{
int a = 10; // 存储进内存
register int a = 10; // 存储进寄存器,访问速度更快,把a定义为寄存器变量
return 0;
}访问速度:寄存器>高速缓存器>内存>硬盘
int 定义的变量是有符号变量,也为 signed int
unsigned无符号
static --静态的
static 定义的变量只执行一次
修饰局部变量,局部变量的生命周期变长
注意:声明的静态全局变量只能在自己所在的源文件内部使用,在文件外使用extern进行声明调用时会出错。
static 修饰函数,改变了函数的链接属性
#include <stdio.h>
void test()
{
static int a = 1; //a 是一个静态的局部变量,只执行一次
a++;
printf("a = %d\n", a);
}
int main()
{
int i = 0;
while (i < 5)
{
test();
i++;
}
return 0;
}输出结果:2 3 4 5 6
struct ---结构体关键字
typedef --类型定义(类型重定义,常用)

#include <stdio.h>
typedef unsigned int u_int; //把unsigned int重新起个名字叫做u_int
int main()
{
u_int a = 20;
return 0;
}
union --联合体/共用体
volatile --是一种类型修饰符,用它声明的类型变量表示可以被某些编译器未知的因素更改
extern --外部变量声明(常用)
用处:在文件1中定义的变量,在文件2中也调用运行
#include <stdio.h>
#include "test2.h" //外部变量所在的文件
int main()
{
extern int g_val; // 声明外部变量
printf("g_val = %d\n",g_val);
return 0;
}#define 定义常量和宏
#include <stdio.h>
// 函数定义
int Max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
// 宏定义
#define MAX(X,Y) (X>Y?X:Y)
int main()
{
int a = 10;
int b = 20;
int max = Max(a, b);
printf("max = %d\n", max);
// 宏的方式
max = MAX(a,b);
printf("max = %d\n",max);
return 0;
}指针(重点)
内存是电脑上特别重要的存储器,计算机中所有程序的运行都是在内存中进行的。所以为了有效的使用内存,就把内存分为一个个小的内存单元,每个内存单元的大小是1个字节。为了能够有效的访问到内存的每个单元,就给内存单元进行了编号,这些编号被称为该内存单元的地址。
生活中通过地址来查找地点位置
电脑如何产生地址:
32位:32根地址线/数据线
一个空间占一个字节
#include <stdio.h>
int main()
{
int a = 10; // 4个字节
//&a;//取地址
int *p = &a; //指针变量
printf("%p\n", p);
printf("%p\n", &a);
*p = 20;// *解引用操作符,a的值会变成20
printf("%d\n", a);
}输出结果:a的地址,a=20,指针是一个变量,用来存放地址
指针的使用实例:可以定义多个变量,然后取地址,把多个传感器读取到的值传递给多个变量的地址,从而实现一个函数返回多个值效果
该方法已测试,可以正常使用!!!
#include <stdio.h>
int main()
{
int num = 10;
int *p = #
*p = 20;
return 0;
}#include <stdio.h>
int main()
{
char ch = 'w';
char *pc = &ch;
*pc = 'a';
printf("%c\n",ch);
return 0;
}边栏推荐
- Interviewer: what is the factory method mode?
- MySQL explain (glory Collection Edition)
- mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。
- Special network technology virtual host PHP version setting
- Sqlserver problem solving: replication components are not installed on this server. Please run SQL Server Setup again and select the option to install replication components
- What is eplato cast by Plato farm on elephant swap?
- Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
- 并发编程的三大核心问题(荣耀典藏版)
- Leetcode hot topic Hot 100 - > 3. longest substring without repeated characters
- Flume (5 demos easy to get started)
猜你喜欢

MySQL是如何利用索引的(荣耀典藏版)

Lock mechanism in MySQL database InnoDB storage engine (glory Collection Edition)

retainface使用报错:ModuleNotFoundError: No module named 'rcnn.cython.bbox'

"Risking your life to upload" proe/creo product structure design - seam and buckle
![[solution] solve the problem of SSH connection being inactive for a long time and being stuck and disconnected](/img/66/99bd61223cbe622db3e28474f4fa15.png)
[solution] solve the problem of SSH connection being inactive for a long time and being stuck and disconnected

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(2)小程序功能

并发编程的三大核心问题(荣耀典藏版)

【LeetCode】13. Linked List Cycle·环形链表

Two ways for wechat applet to realize dynamic horizontal step bar

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(1)开发概要
随机推荐
【HCIP】路由策略、策略路由
获取两个集合相差数据
C#引入WINAPI传递中文字符串参数字符集问题
Achievements in science and Technology (XXVIII)
软件产品第三方测试费用为什么没有统一的报价?
From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project
mysql: error while loading shared libraries: libtinfo.so. 5 solutions
feign调用get和post记录
Canvas 从入门到劝朋友放弃(图解版)
[in depth study of 4g/5g/6g topic -42]: urllc-14 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -8-low delay technology-2-slot based scheduling and
Today in history: the father of database passed away; Apple buys cups code; IBM chip Alliance
Alipay applet authorization / obtaining user information
2020.7.7 eth price analysis
软工必备知识点
LeetCode 热题 HOT 100 -> 1.两数之和
Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
智能合约安全——selfdestruct攻击
pytorch优化器设置
Use of Day6 functions and modules
学会这招再也不怕手误让代码崩掉