Advertisement
Tech

How To Declare Variables And Write An Assignment Statement In C

In this article, we begin to delve into the nitty gritty of C programming. We show how to declare variables and how to use the assignment statement.

By Noel Kalicharan
Desk Tech
Reading time 4 min read
Word count 778
Linux Computing Linux commands
How To Declare Variables And Write An Assignment Statement In C
Advertisement
Quick Take

In this article, we begin to delve into the nitty gritty of C programming. We show how to declare variables and how to use the assignment statement.

On this page

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.

Advertisement

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

Advertisement

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:

Advertisement

#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

Advertisement

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:

Advertisement

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:

Advertisement

int a;

The statement:

Advertisement

a = 14;

is C’s way of writing the assignment statement

Advertisement

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,

Advertisement

“set b to 25” is written as:

b = 25;

Advertisement

and “set sum to a + b” is written as:

>sum = a + b;

Advertisement

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);

Advertisement

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

Advertisement

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.

  1. C Programming For Beginners - Part 1
  2. C Programming For Beginners - Part 2
  3. C Programming for Beginners – Part 3
  4. C Programming for Beginners – Part 4
  5. C Programming For Beginners - Part 5
  6. C Programming For Beginners - Part 6
  7. C Programming For Beginners - Data Types
  8. C Programming For Beginners - Part 8
  9. C Programming For Beginners - Part 9
  10. C Programming For Beginners - Part 10
  11. C Programming For Beginners - Part 11
  12. C Programming For Beginners - Part 12
  13. C Programming For Beginners - Part 13
  14. C Programming For Beginners - Part 14
  15. C Programming For Beginners - Integer Data Types
  16. C Programming for Beginners - Part 16
  17. C Programming For Beginners - Integer Expressions, Operators and Precedence
  18. C Programming For Beginners - Part 18
  19. C Programming For Beginners - Printing Double and Float
  20. C Programming For Beginners - Mixing double, float and int
Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux commands
Advertisement