不使用第三个变量在 C/C++ 中交换两个变量值
以下是交换两个变量的一个示例。
示例
#include <stdio.h>
int main() {
int a,b;
printf("Enter the value of a : ");
scanf("%d", &a);
printf("\nEnter the value of b : ");
scanf("%d", &b);
a += b -= a = b - a;
printf("\nAfter Swapping : %d\t%d", a, b);
return 0;
}输出
Enter the value of a : 23 Enter the value of b : 43 After Swapping : 4323
在以上的程序中,两个变量 a 和 b 在运行时是动态声明和初始化的。
int a,b;
printf("Enter the value of a : ");
scanf("%d", &a);
printf("\nEnter the value of b : ");
scanf("%d", &b);无需使用任何第三方变量,就可以交换数字。
a += b -= a = b - a;
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP