当前位置:网站首页>[C language practice - exchange the values of two variables]
[C language practice - exchange the values of two variables]
2022-06-09 13:07:00 【Beginners of C language】
Exchange the values of two variables
Preface
This article exercises exchanging the values of two variables , There are generally two types of methods , Create temporary variables and do not create temporary variables , It mainly includes :
- Create temporary variables —— General method
- Create temporary variables —— The pointer
- Do not create temporary variables —— Addition and subtraction
- Do not create temporary variables —— Exclusive or
1、 Create temporary variables —— General method
int main()
{
int a = 10;
int b = 20;
int temp = 0;
printf(" Exchange before :a = %d b = %d\n", a, b);// Before the exchange
temp = a;
a = b;
b = temp;
printf(" After exchanging :a = %d b = %d\n", a, b);// After the exchange
return 0;
}
The results are as follows :

2、 Create temporary variables —— The pointer
Both pointers and general methods need to create variables , The difference is that general methods cannot be written in the form of functions , This is the application of formal parameters and arguments , Already in 【C Language foundation 3—— function (1)3、 The parameters of the function 】 I've learned :
// Implemented as a function , But I can't finish the task
int exchange1(int x, int y)
{
// When an argument is passed to a formal parameter , A formal parameter is a temporary copy of an argument ,
// Modification of a parameter does not affect the argument
int temp = x;
x = y;
y = temp;
}
// The right version
int exchange2(int* pa, int* pb)// Define pointer , Receiving address
{
int temp = *pa;
*pa = *pb;
*pb = temp;
}
int main()
{
int a = 3;
int b = 5;
exchange1(a, b);// The passed parameter is the value
printf("exchange1::a = %d b = %d\n", a, b);// Exchange before
exchange2(&a, &b);// The transmission parameter is the address
printf("exchange2::a = %d b = %d\n", a, b);// After the exchange
// Incoming address , Custom formal parameters and arguments are more closely related , Can change the value stored in the address
// here , The address of the formal parameter is the same as that of the argument
return 0;
}
The results are as follows :

3、 Do not create temporary variables —— Addition and subtraction
int main()
{
int a = 10;
int b = 20;
a = a + b;// hold a+b Assign a value to a
b = a - b;// hold a+b-b, Namely a Assign a value to b
a = a - b;// hold a+b-a Assign a value to a
printf("a=%d\nb=%d\n", a, b);
return 0;
}
The results are as follows :

4、 Do not create temporary variables —— Exclusive or
Use of XOR , stay 【 Primary data structure and algorithm 2】 Time complexity and space complexity (2)—— Runner array 、 Left hand character 、 Missing numbers 7.3 Method 3—— Exclusive or I have described in detail in :
int main()
{
int a = 10;
int b = 20;
printf(" Exchange before :a = %d b = %d\n", a, b);// Before the exchange
a = a ^ b;// hold a^b Assign a value to a
b = a ^ b;// hold a^b^b, It's the rest a Assign a value to b
a = a ^ b;// hold a^b^a, Assign a value to a
printf(" After exchanging :a = %d b = %d\n", a, b);// After the exchange
return 0;
}
The results are as follows :

summary
Exchange the values of two variables , There are two types of methods , Create temporary variables and do not create temporary variables :
- Two methods of creating temporary variables are commonly used
- The method of pointer should be mastered
- It is not easy to think of two methods without creating temporary variables
- Pay attention to the special use of addition and subtraction and XOR
边栏推荐
- 【leetcode周赛记录】第79场双周赛+第295场周赛记录
- 【NEW】WPF窗体中控件移动 + 拖拽大小 + 动画拖动
- Tree difference
- Handlebars template engine usage 2
- Loki: like Prometheus, but for logs
- 市面常用芯片对应的ARM架构
- 生词 生词 生词 生词 生词 生词 生词
- Who has better performance than CK, ES and redisearch
- [interpretation of redis underlying mechanism: Linux operating system file descriptor FD]
- DDD建模方法论之【事件风暴法】
猜你喜欢
随机推荐
Handlebars. JS template engine usage I
数字化转型:如何获得组织的认可?
JSON and JQ implementation of three-level linkage selection box method 1
Who has better performance than CK, ES and redisearch
[memcached and redis]
常见的数据分析误区
【C语言练习——调整数组内奇数偶数的顺序】
【grafana基本使用】
周博磊《模型可解释性年度进展概述》20200805
C语言线性表--静态链表
stc8a8k_ rgb___ Led 888 test code, not tested yet
Drawing arc text in gdi+
软件测试工程师手把手教你如何制定测试计划
数据库day-2
MySQL learning notes
NVIDIA releases the latest version of Tao toolkit to further simplify and accelerate AI model creation
【 日志系统 】
Kolla ansible openstack login certificate is not available
WPF中控件单击双击冲突的解决方案
Loki: like Prometheus, but for logs







