Variables and expressions in C programming

Variables are used to represent some known or unknown quantity in memory. Variables are referential name given to memory location holding our program data.

C variables are typed in nature, means we must explicitly specify data type of the variable at the time of its declaration.

In programming there are two ways to access data stored in memory, either by their original memory address or by their referential name i.e. variable name.

Variable name is an identifier which must strictly follow the identifier naming rules. Apart from the identifier naming rules below are some quick points about variable naming.

  • C is a case sensitive language. In C uppercase and lowercase variables are treated differently. For example – num, Num, NUM, nUm etc. all are different.
  • You cannot have two variables with same name in same scope.
  • Variables name must not be a global identifier.

Declaring variables in C

In C programming a variable must be declared prior to its use, so that compiler can know about the referential name we are using for a memory location.

Important note: Many text on internet advocates that variable declaration allocates memory. However, this is not true. The ANSI C standard haven't specified anything about memory allocation at the time of variable declaration. It is the compiler that plays with situation. Compilers are smart enough to generate efficient code. It may allocate memory for the declared variable or can even ignore the declared variable completely.

Syntax to declare variables

<data-type> <variable-name>;

Data type must be a valid C data type either primitive, derived or user defined.

Read more about - List of all valid C primitive and derived data types

You can also declare more than one variable of same type at once using comma.
<data-type> <variable-name1>, <variable-name2>, ... , <variable-nameN>;

In C it is illegal to declare multiple variables of different type at once. For example, the below statement is illegal in C and results in compilation error.
int roll, float average, percentage;

Example to declare variables

int num1;
double principle;
float time, rate, si;

Programming is an art and so is variable naming. You are free to give any name to a variable. However, it is a good programming practice to give meaningful name to your variables.

For example - Never declare variables like int x, x1, y, z, a; etc. It gives no idea to the programmer about the variable. Instead give some meaningful names such as - int sum, average, num1, num2;.

Initializing variable in C

At the time of variable definition, the C compiler allocates a block of bytes in memory of certain type. Initially the allocated memory contains garbage value.

The previous value of a memory location left by some another program is known as garbage value. This value was used by some other program or may be same program and has been left as residue.

You might think, why the value of newly allocated memory is not reset to 0 or NULL? This is because of performance issue. While many other programming languages resets the newly allocated memory to 0 or NULL, which costs slight performance.

Initializing a variable means assigning some value to it for the very first time. It is considered as a good programming practice to initialize your variables with 0 or NULL to override the default garbage value. In C we initialize a variable using assignment operator.

Syntax to initialize a variable

<variable-name> = <value-or-expression>;

Where value is a valid C literal of variable-name type.

Example to initialize a variable

num = 10;
principle = 5000;
time = 10;
rate = 0.5;
si = (principle * time * rate) / 100;

You can also initialize a variable at the time of its declaration. Variable initialized at the time of its declaration is called as value initialized variable.

Example of value initialized variable

int num = 10;
double principle = 5000;
float time, rate = 0.5;
char ch = ‘a’;

Value initialized variables are not considered as a good programming practice, when initializing more than one variable at a time. We must initialize each variable in separate line as done below.

int num = 10;
double principle = 5000;
float time;
float rate = 0.5;
char ch = ‘a’;

Expressions in C

Expression is a combination of one or more operators, variables and literals. Every expression evaluates to some value of some type which can be assigned to a variable.

Expression could be as simple as sum of two numbers
sum = num1 + num2

Or complex as complex algebraic equations
Algebraic equation

Unlike mathematics, programming language do not follow BODMAS rule to evaluate expressions. Therefore we must know how to convert mathematical equations to programming language expression.

Things to remember before converting algebraic equation in C expression -

  1. There is no power operator. Do not ever confuse ^ as a power or exponential operator. Use pow(base, exponent) library function present in math.h to evaluate powers.

    Read more - Bitwise XOR ^ operator

  2. There is no root operator. Use sqrt(number); function present in math.h to find roots.
  3. Use math.h library to evaluate trigonometry functions.
  4. Do not forget to group expression with in a pair of parenthesis to prioritize its evaluation.

    Read more - Precedence and associativity of operators

  5. Do not forget to cast integers to float or double before division to get fractional results.

Example to convert mathematical equations to C expression

  • Area of circle equation

    A = M_PI * radius * radius
    Here M_PI is a constant defined in math.h

    Read more - How to define constants in C?

  • Algebraic equation3

    1 / ((x*x) + (y*y))
    Or you can also use pow() to evaluate exponents.
    1 / (pow(x, 2) + pow(y, 2))

  • Algebraic equation4

    x = sqrt((b*b) – 4*a*c)
    Or
    x = sqrt(pow(b, 2) – 4 * a * c)

  • Algebraic equation5

    x = (-b + sqrt(pow(b, 2) – 4 * a * c)) / 2 * a

  • Trigonometric equation1

    x = cos(a) * cos(b) – sin(a) * sin(b)

    Here cos() and sin() are trigonometric function present in math.h header file.

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>