YouTip LogoYouTip

C Examples Add Numbers

## C Program to Add Two Integers In this tutorial, you will learn how to write a simple C program to add two integers. This is a fundamental exercise for beginners to understand basic input/output operations, variable declarations, and arithmetic operators in C. --- ### Introduction To add two integers in C, we perform the following steps: 1. **Declare variables** to store the two input numbers and their sum. 2. **Read input** from the user using the standard input function `scanf()`. 3. **Perform addition** using the arithmetic addition operator (`+`). 4. **Display the result** on the screen using the standard output function `printf()`. --- ### C Code Example: Adding Two Integers Below is the complete C program. The code reads two integers entered by the user, calculates their sum, and prints the result. ```c #include int main() { int firstNumber, secondNumber, sumOfTwoNumbers; printf("Enter two integers (separated by a space): "); // Read two integers from the user using scanf() scanf("%d %d", &firstNumber, &secondNumber); // Add the two numbers and store the result in sumOfTwoNumbers sumOfTwoNumbers = firstNumber + secondNumber; // Display the calculation and the result printf("%d + %d = %d\n", firstNumber, secondNumber, sumOfTwoNumbers); return 0; } ``` ### Output When you compile and run the program, the interaction will look like this: ```text Enter two integers (separated by a space): 12 23 12 + 23 = 35 ``` --- ### Code Explanation Let's break down how the program works: * **`#include `**: This is a preprocessor command that includes the Standard Input/Output header file. It is required to use functions like `printf()` and `scanf()`. * **`int firstNumber, secondNumber, sumOfTwoNumbers;`**: This line declares three variables of type `int` (integer). These variables will hold the first number, the second number, and their sum, respectively. * **`scanf("%d %d", &firstNumber, &secondNumber);`**: * The `scanf()` function reads formatted input from the standard input (keyboard). * `%d` is the format specifier for integers. * The `&` (address-of) operator is used before the variable names to pass their memory addresses, allowing `scanf()` to store the user's input directly into those variables. * **`sumOfTwoNumbers = firstNumber + secondNumber;`**: The addition operator `+` adds the values of `firstNumber` and `secondNumber`. The assignment operator `=` then assigns the result to `sumOfTwoNumbers`. * **`printf("%d + %d = %d\n", ...)`**: The `printf()` function displays the formatted string on the console. The `%d` placeholders are replaced by the values of `firstNumber`, `secondNumber`, and `sumOfTwoNumbers` in order. --- ### Key Considerations 1. **Integer Overflow**: The `int` data type in C has a limited range (typically $-2,147,483,648$ to $2,147,483,647$ on modern 32-bit and 64-bit systems). If the sum of the two numbers exceeds this range, an integer overflow will occur, leading to unexpected or negative results. For larger numbers, consider using `long` or `long long` data types. 2. **Input Validation**: In production-grade code, it is good practice to check the return value of `scanf()`. `scanf()` returns the number of successfully matched and assigned input items. For example, checking `if (scanf("%d %d", &firstNumber, &secondNumber) == 2)` ensures that the user actually entered two valid integers.
← C Examples Sizeof OperatorFontawesome Icons Text β†’