Kevin321888999

exercise 3 swap function

Feb 24th, 2022
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>// library
  2. void swap(int *a, int *b){ // function and the definition. Void shows that the definition of the function is universal.
  3. int temp= *a;
  4. *a=*b;
  5. *b= temp; }// This is saying that *b=*a but it is inferring it by having it = temp. use transitive property.
  6.  
  7. int main(void) {// main program.
  8.   int M; // declare the variables as integers
  9.   int N;
  10.   printf("Enter values for M: ");
  11.   scanf("%i",&M);
  12.   printf("Enter the values for N: ");
  13.   scanf("%i",&N);
  14.   printf("Before swapping M: %d, N: %d\n",M, N); // This shows the results of the numers before the swap.
  15.   swap(&M,&N); //Function call
  16.   printf("After swapping M: %d, N: %d",M,N);
  17.   return 0;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment