/* How to write a swap function in C. */ #include <stdio.h> short i, j; /* Global variable declaration */ swap(i, j) /* Function declaration */ short *i, *j; /* '*' stands for pointer declaration */ { short k; k = *i; *i = *j; *j = k; return(0); } /* End of function swap(i,j) */ main() { clrscr(); printf("C62.C -> The swap function in C!\n\n"); /* Assignment Statements */ printf("Enter Elements\n"); printf("i = "); scanf("%hd", &i); /* Read element from keyboard entry */ printf("j = "); scanf("%hd", &j); swap(&i, &j); /* Call of function swap */ /* Print result */ printf("After Swapping, the Elements are...\n"); printf("i = %d, j = %d\n", i, j); return(0); } /* End of main Program C62 */ /* Input : 4 5 Output : C62.C -> The swap function in C! Enter Elements i = 4 j = 5 After Swapping, the Elements are... i = 5, j = 4 */
Last modified: 22/07/97