Technology
A Simple C Program: Adding Two Integers Using printf and scanf
A Simple C Program: Adding Two Integers Using `printf` and `scanf`
Learning to write C programs is a fundamental skill in computer science. One of the easiest and most useful programs you can create is one that allows the user to input two integer values and prints their sum. This guide will walk you through the process of creating such a program. If you're new to C programming, you'll learn the basics of using scanf() for user input and printf() to display output on the screen.
Step 1: User Input and Integer Variables
The first step in any program that requires user input is to initialize variables to store the values. In C, the int data type is used to store integer values. You'll need two variables to store the input values, which we'll call a and b.
int a, b;
Next, you need to ask the user to input the values. This is done using the scanf() function. scanf() reads values from the standard input (usually the keyboard) and stores them in the specified variables. Here is the syntax for using scanf():
scanf("%d %d", a, b);
Note that is used before a and b. This is because the scanf() function requires the address of the variables to store the input. This is where pointers come in. Pointers in C are variables that store the memory address of another variable. In this case, a and b give the addresses of a and b, allowing scanf() to modify the values of the variables.
Step 2: Calculating the Sum
Now that you have the integers stored in variables a and b, you can perform the addition. In C, the addition operation is performed using the operator. You simply set the variable to store the result (let's call it c) and use the sum of a and b to initialize it.
int c a b;
This line tells the computer to add the values stored in a and b and assign the result to c.
Step 3: Displaying the Result
The last step is to display the result on the screen. This is where the printf() function comes in. printf() is used to output text or data to the standard output (usually the screen). Here is how you would use printf() to print the result:
printf("%d %d %d ", a, b, c);
The format string %d %d %d tells printf() to expect three integer values. The values of a, b, and c are substituted into the string, and the result is printed to the screen. The at the end is a newline character, which moves the output to the next line.
Putting It All Together
Here is a complete C program that demonstrates how to add two integers using user input and display the result:
#include stdio.h int main() { int a, b, c; // Ask the user to input the values printf("Enter the value of a: "); scanf("%d", a); printf("Enter the value of b: "); scanf("%d", b); // Calculate the sum c a b; // Display the result printf("%d %d %d ", a, b, c); return 0; }
This program will prompt the user to enter the values of a and b, compute their sum, and then display the result.
Conclusion
Through this simple program, you've learned how to use scanf() for user input, pointers for storing memory addresses, and printf() for displaying results. These are essential skills in C programming and will serve as a foundation for more complex programs in the future. Happy coding!