A simple exchange of two variable values

Source: Internet
Author: User

Given two integers, swap functions to exchange two numbers of content
#include <stdio.h>
void swap (int *x,int *y);/* Function declaration */
int main ()
{
int A;
int b;
printf ("Please enter the value of a, b respectively \ n");
printf ("a=");
scanf ("%d", &a);
printf ("b=");
scanf ("%d", &b);
Swap (&A,&B);
printf ("The value of A/b after swapping is \ n");
printf ("a=%d\nb=%d\n", A, b);
return 0;
}
void swap (int *x,int *y)/* Swap function */
{
int tmp;
Tmp=*x;
*x=*y;
*y=tmp;
}



Do not create a third variable, using a function to exchange two numbers of content
#include <stdio.h>
int main ()
{
void swap (int *x,int* y);//function declaration
int A;
int b;
printf ("Please enter the value of a, b respectively \ n");
printf ("a=");
scanf ("%d", &a);
printf ("b=");
scanf ("%d", &b);
Swap (&A,&B);
printf ("The value of A/b after swapping is \ n");
printf ("a=%d\nb=%d\n", A, b);
return 0;
}
void swap (int *x,int* y)//Exchange function
{
*x=*x^*y;
*y=*x^*y;
*x=*x^*y;
}


A simple exchange of two variable values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.