Sunday, March 30, 2014

Contents Swap – C Program

Problem Question


Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.

Explanation of Problem


We need the user to enter 2 numbers, which should be stored in two variables ‘C’ and ‘D’. Our program should be able to interchange the values held by these variables.

Code


#include <stdio.h>
/*Content Swap*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: CodeBlocks12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 30-03-2014**/
int main()
{
    printf("\n\nEnter two numbers: ");
    int C, D;
    scanf("%d%d", &C, &D);
    int temp;
    temp = C;
    C = D;
    D = temp;
    printf("\n\nC = %d, D = %d\n", C, D);
    system("pause");
    return 0;
}

Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This functions has to named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

int temp; -> Introduces a new variable which will help in the transition of the values of the variables during the swapping process.

temp = C;
C = D;
D = temp;

Using the above lines of code, we swap the values. The value of variable ‘C’ is stored in the variable ‘temp’. Then, in the variable ‘C’ we store the value of variable ‘D’. Now, in the variable ‘C’ we have the value of ‘D’. Now we need to store the original value of ‘C’ into ‘D’. We do this by using the variable ‘temp’. We now allocate the value of ‘temp’ (the original value of ‘C’) to the variable ‘D’. Now we just output the result, since we have successfully swapped the values of the two variables.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)



No comments:

Post a Comment

Need help?