Simple C Program: Add Two Integers

C How to Program #4

Simple C program: Adding Two Integers

// Addition program
#include <stdio.h>

// function main begins program execution
int main( void )
{
    int integer1; // first number to be entered by user
    int integer2; // second number to be entered by user

    printf( "Enter first integer\n" ); // prompt
    scanf( "%d", &integer1 ); // read an integer

    printf( "Enter second integer\n" ); // prompt
    scanf( "%d", &integer2 ); // read an integer

    int sum; // variable in which sum will be stored
    sum = integer1 + integer2; // assign total to sum

    printf( "Sum is %d\n", sum ); // print sum
} // end function main

Output:

Enter first integer
1
Enter second integer
1
Sum is 2

Variables and Variable Definitions

int integer1; and int integer2; are definitions. The names integer1 and integer2 are the names of variables —locations in memory where values can be stored for use by a program. These definitions specify that variables integer1 and integer2 are of type int, which means that they’ll hold integer values, i.e., whole numbers such as 7, –11, 0, 31914.

Define Variables Before They Are Used

All variables must be defined with a name and a data type before they can be used in a program. The C standard allows you to place each variable definition anywhere in main before that variable’s first use in the code, but you should define variables close to their first use.

Defining Multiple Variables of the Same Type in One Statement

The preceding definitions could be combined into a single definition as follows: int integer1, integer2; but that would have made it difficult to associate comments with each of the variables.

Identifiers and Case Sensitivity

A variable name in C can be any valid identifier. An identifier is a series of characters consisting of letters, digits and underscores ( _ ) that does not begin with a digit.
C is case sensitive —uppercase and lowercase letters are different in C, so a1 and A1 are different identifiers.

Prompting Messages

printf( "Enter first integer\n" ); displays the literal "Enter first integer" and positions the cursor to the beginning of the next line. This message is called a prompt because it tells the user to take a specific action.

The scanf Function and Formatted Inputs

scanf( "%d", &integer1 ); uses scanf (the “f” stands for “formatted”) to obtain a value from the user. The function reads from the standard input, which is usually the keyboard. This scanf has two arguments, "%d" and &integer1.
The first, the format control string, indicates the type of data that should be entered by the user. The %d conversion specifier indicates that the data should be an integer (the letter d stands for “decimal integer”). The % in this context is treated by scanf and printf as a special character that begins a conversion specifier.
The second argument of scanf begins with an ampersand (&) —called the address operator— followed by the variable name. The &, when combined with the variable name, tells scanf the location (or address) in memory at which the variable integer1 is stored. The computer then stores the value that the user enters for integer1 at that location.
Functions printf and scanf facilitate interaction between the user and the computer. This interaction resembles a dialogue and is often called interactive computing.

Assignment Statement

sum = integer1 + integer2; calculates the total of variables integer1 and integer2 and assigns the result to variable sum using the assignment operator =. The statement is read as, “sum gets the value of the expression integer1 + integer2.”
Most calculations are performed in assignments. The ( = ) operator and the ( + ) operator are called binary operators because each has two operands. The + operator’s operands are integer1 and integer2. The = operator’s two operands are sum and the value of the expression integer1 + integer2.

Combining a Variable Definition and Assignment Statement

You can assign a value to a variable in its definition —this is known as initializing the variable. For example, int sum = integer1 + integer2; which adds integer1 and integer2, then stores the result in the variable sum.

Printing with a Format Control String

printf( "Sum is %d\n", sum ); calls function printf to print the literal Sum is followed by the numerical value of variable sum on the screen. This printf has two arguments, "Sum is %d\n" and sum. The first is the format control string. It contains some literal characters to be displayed and the conversion specifier %d indicating that an integer will be printed. The second argument specifies the value to be printed.

Calculations in printf Statements

Calculations can also be performed inside printf statements. For example, printf( "Sum is %d\n", integer1 + integer2 ); in which case the variable sum is not needed.


Memory Concepts

Variable names such as integer1, integer2 and sum actually correspond to locations in the computer’s memory. Every variable has a name, a type and a value.
In the addition when the statement scanf( "%d", &integer1 ); is executed, the value entered by the user is placed into a memory location to which the name integer1 has been assigned. Suppose the user enters the number 16 as the value for integer1. The computer will place 16 into location integer1. Whenever a value is placed in a memory location, the value replaces the previous value in that location and the previous value is lost; thus, this process is said to be destructive. when a value is read from a memory location, the process is said to be nondestructive.

Arithmetic in C

Most C programs perform calculations using the C arithmetic operators. The arithmetic operators are all binary operators. For example, the expression 16 + 6 contains the binary operator + and the operands 16 and 6.

C operationArithmetic operator
Addition+
Subtraction-
MultiplicationText
Division/
Remainder%

Integer Division and the Remainder Operator

Integer division yields an integer result. For example, the expression 7 / 4 evaluates to 1 and the expression 17 / 5 evaluates to 3. C provides the remainder operator, %, which yields the remainder after integer division. The remainder operator is an integer operator that can be used only with integer operands. The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2.

Rules of Operator Precedence

C applies the operators in arithmetic expressions in a precise sequence which are generally the same as those in algebra.

OperatorsOrder of evaluation (Precedence)
()Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they’re evaluated left to right.
* , / , %Evaluated second. If there are several, they’re evaluated left to right.
+ , -Evaluated third. If there are several, they’re evaluated left to right.
\=Evaluated last.

There’s no arithmetic operator for exponentiation in C, so we represent x2 as x * x.
The C Standard Library includes the pow (“power”) function to perform exponentiation.