C Programming to swap two variables

#include <stdio.h>
#include <stdlib.h>

int main()
{
  	//initialize variables
	int num1 = 10;
	int num2 = 9;
  	int tmp;
  	
  	//create the variables needed to store the address of the variables
  	//that we want to swap values
  	int *p_num1 = &num1;
  	int *p_num2 = &num2;
  
  	//print what the values are before the swap
  	printf("num1: %i\n", num1);
    printf("num2: %i\n", num2);
  
  	//store one of the variables in tmp so we can access it later
  	//gives the value we stored in another variable the new value
  	//give the other variable the value of tmp
  	tmp = num1;
  	*p_num1 = num2;
  	*p_num2 = tmp;

  	//print the values after swap has occured
   	printf("num1: %i\n", num1);
    printf("num2: %i\n", num2);
  	
	return 0;
}

4.22
9
John Haire 125 points

                                    #include &lt;stdio.h&gt;

int main()
{
    int x = 20, y = 30, temp;
    temp = x;
    x = y;
    y = temp;
    printf(&quot;X = %d and Y = %d&quot;, x, y);
    
    return 0;
}

4.22 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to swap two integers in c c program to swap two numbers without a third variable using conditionals swapping of two numbers in c swap 2 number in c swap two var c swap by value function in c c program to swap two numbers using function call by value how can we swap in c swapping without third variable in c swap two numbers without using temporary variable in c how to swap two numbers in c without using third variable swap 2 variables without using 3rd variable in c c program to swap two numbers using pass by references swap the pointer of two variables in c swap using function in c swapping of two numbers without using third variable in c how to swap two numbers in c using function c swap to variables c swap values swap to numbers in c swap fuction in c swap two numbers in c using call by value swapping by call by value in c Write a C Program to swap two given numbers using call by reference. swap varaibles C swap function in c programming swapping of two numbers in c using functions program to swap two numbers without using third variable in c swap without third variable in c swape values in c function to swap two numbers in c C Programming to swap two variables swapping number in c swap two values using function in c swap two numbers using function in c swap two integers using function in c swap 2 numbers using functions in c swap numbers in c from a function swapnumbers in c how to define swap in c swap using 3 variables in c swap values without third variable in c swap values in c how to swap two digit in c swapping values in c without using third variable swap two numbers in c using function swap of two numbers in c write a function to swap two integers in c swap program in c swapping values in c different method to swap the variables in c language why can't you swap variables in c swaping function in c how to swap values in c write a program to swap the numbers in c programiz write a program to swap the numbers in c swaping in c swap code in c swap numbers using function in c swap number in c swap using 2 variables c Write a function to swap the values between the variables in c swap function inc C swap function in c c function to swap two numbers how to swap structures in c swap elements in c swap values of two variables without using third variable in c c swap two variables function swap in c language swap variables in c swap 4 numbers in c c to swap swap two number in c swaping numbers in c swap int c is swap a function in c function swap in C c swap integer bytrs how to swap variables in c swapping variable values in c swap by value in c how to swap value in c swap values c swap 2 variables in C function to swap in c swap using two variables in c swap numbers in c c swap ints swping two numbers how to swap in = write a c program to check a swap swap 2 numbers by using address function in c excahnge of integers with tem variable in c how to swap two numbers in c swap using xor operator in c swap logic in c c program exchange two numbers write a program to swap two numbers swap c Write a program which asks the user to enter two integers a and b. Print the values. The values are passed on to two functions which return the swapped values of a and b. swap values in c with two functions swap variable in c code for swapping two numbers in c swap variable values in c swaps example in programming language Write a program to accept two numbers from user and swap their values. WAP to swap the values of two variables using switch case. swapng two variable in c swapping numbers in c swap the value of two variables in c how to make a swap function with void* my swap in c swapping of variables in c swap function in c using pointers program to swap two numbers in c how to swap two numbers swap with pointers in c 1. WAP TO SWAP THE VALUE OF TWO VARIABLE swapping elements in c swap of two numbers using third variable swap two digits in c c swap two integers c swap elements in array create a function that swaps the value of two integers how to use swap in c swapping 2 numbers in c swap the values of 2 given integer variables. swap function in c c program to show interchange of two numbers using functions in c programming c program to show interchange of two numbers in c programming array before swap and after swap program swapping of two numbers c how to swap values with pointer c different methods to swap 2 numbers void swap in c swap two integer function in C how to swap numbers in c How to swap two values in a function swap 2 numbers swapping two variables in c swapping c swaping c swap two numbers in c Swap two variables switching 2 variables in c write a program with a function to swap the values of 2 given integer variables c swap two variables write a program that will make a swap between two defined variables using their addresses swap with two variables in c swap function c program to swap two numbers swap two user given integer swap variable temps swap in c programming c swap function switch 2 variables in c swaping numbers using Swap in C use of swap in C c swap c program to interchange two numbers swap 2 numbers in c Write a program for swapping of two numbers. swap 2 numbers in c programming swap function for integers swapping in c swap in c c program to swap two numbers swapping program in c swap numbers code in c swap two variables in C function that swaps two integers swap program using 2 variables swaping a number in c swapping logic in c c how to make 2 variables swap swap the values of two variables in c how to swap values in variables in c
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source