当前位置:网站首页>初识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;
}边栏推荐
- Lombok prompts variable log error when using JUnit test in idea
- Achievements in science and Technology (XXVIII)
- Leetcode judge whether palindrome number
- Alipay applet authorization / obtaining user information
- 借助Elephant&nbsp;Swap打造的ePLATO,背后的高溢价解析
- 关于Sqli-labs单引号不报错的问题
- Canonical Address
- Three core issues of concurrent programming (glory Collection Edition)
- MySQL explain (glory Collection Edition)
- windbg
猜你喜欢

Emotional drama in the world Zhou Bingkun lost his job because he saw Tu Zhiqiang and was shot

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(1)开发概要

Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B

Leetcode hot topic Hot 100 - > 1. Sum of two numbers

这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】

一文读懂Plato&nbsp;Farm的ePLATO,以及其高溢价缘由

"The faster the code is written, the slower the program runs"

JVM tuning -xms -xmx -xmn -xss

OBS键盘插件自定义diy

Interviewer: what is the factory method mode?
随机推荐
[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
Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
Plato Farm在Elephant Swap上铸造的ePLATO是什么?
2020.7.7 eth price analysis
软件产品第三方测试费用为什么没有统一的报价?
[Star Project] small hat aircraft War (V)
使用BigDecimal类型应该避免哪些问题?(荣耀典藏版)
分层图解决的一些最短路问题
基于stm32的恒功率无线充电
JVM tuning -xms -xmx -xmn -xss
软工必备知识点
埃睿迪再度亮相数字中国峰会 持续深化用科技守护绿水青山
Chapter 3 business function development (batch export of market activities, Apache POI)
Lombok prompts variable log error when using JUnit test in idea
MySQL blocking monitoring script
From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project
Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
LeetCode 热题 HOT 100 -> 1.两数之和
智能合约安全——selfdestruct攻击
Lock mechanism in MySQL database InnoDB storage engine (glory Collection Edition)