How To Declare Variables And Write An Assignment Statement In C
Programming with variables
To reinforce the ideas discussed so far, let us write a program which adds the numbers 14 and 25 and prints the sum.
We will need storage locations for the two numbers and the sum. The values to be stored in these locations are integer values. To refer to these locations, we make up the names a, b and sum, say. Many other names would do. In C, as in all programming languages, there are rules to follow for making up variable names. For now, we use the rules that a name must start with a letter, consist of letters and/or digits and cannot contain spaces.
One possible algorithm for solving this problem might look like this:
set a to 14 set b to 25 set sum to a + b print sum
The algorithm consists of four statements. The following explains the meaning of each statement
- set a to 14 - store the number 14 in memory location a (this is an example of an assignment statement; we assign a value to a variable;
- set b to 25 - store the number 25 in memory location b;
- set sum to a + b - add the numbers in memory locations a and b and store the sum in memory location sum. The result is that 39 is stored in sum;
- print sum - print (on the screen) the value in sum, i.e. 39.
The following shows how we can write this algorithm as a C program:
#include <stdio.h> main() { int a, b, sum; a = 14; b = 25; sum = a + b; printf("%d + %d = %d\n", a, b, sum); }
When run, this program will print
14 + 25 = 39
Declaring variables and the assignment statement
In C, variables are declared as integer using the required word int (in programming terminology, we say that int is a reserved word). Thus, the statement:
int a, b, sum;
‘declares’ that a, b and sum are integer variables. In C, all variables must be declared before they are used in a program. Note that the variables are separated by commas, with a semicolon after the last one. If we were declaring just one variable (a, say), we would write:
int a;
The statement:
a = 14;
is C’s way of writing the assignment statement
set a to 14
It is sometimes pronounced “a becomes 14”. In C, an assignment statement consists of a variable (a in the example), followed by an equals sign (=), followed by the value to be assigned to the variable (14 in the example), followed by a semicolon. In general, the value can be a constant (like 14), a variable (like b) or an expression (like a + b). Similarly,
“set b to 25” is written as:
b = 25;
and “set sum to a + b” is written as:
>sum = a + b;
One final point: the variable sum is not really necessary. We could, for instance, have omitted sum from the program altogether and used:
int a, b; a = 14; b = 25; printf("%d + %d = %d\n", a, b, a + b);
to give the same result since C lets us use an expression (e.g. a + b) as an argument to printf. However, if the program were longer and we needed to use the sum in other places, it would be wise to calculate and store the sum once (in sum, say). Whenever the sum is needed, we use sum rather than recalculate a + b each time.
References
C Programming – A Beginner’s Course
Related programming references
This post is part of the series: C Programming for Beginners
A straightforward introduction to Programming in C for people with no previous programing experience.
- C Programming For Beginners - Part 1
- C Programming For Beginners - Part 2
- C Programming for Beginners – Part 3
- C Programming for Beginners – Part 4
- C Programming For Beginners - Part 5
- C Programming For Beginners - Part 6
- C Programming For Beginners - Data Types
- C Programming For Beginners - Part 8
- C Programming For Beginners - Part 9
- C Programming For Beginners - Part 10
- C Programming For Beginners - Part 11
- C Programming For Beginners - Part 12
- C Programming For Beginners - Part 13
- C Programming For Beginners - Part 14
- C Programming For Beginners - Integer Data Types
- C Programming for Beginners - Part 16
- C Programming For Beginners - Integer Expressions, Operators and Precedence
- C Programming For Beginners - Part 18
- C Programming For Beginners - Printing Double and Float
- C Programming For Beginners - Mixing double, float and int