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 C programming there are two ways to access data stored in memory, either by their memory address or 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. The C compiler treats uppercase and lowercase variables 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 language, you must declare a variable prior to use. Variable declaration informs the compiler about referential name we are using for a memory location.
Important note: Many text on internet advocates that variable declaration allocates memory. But, 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 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.
We say the previous state (garbage state) of a memory location, left by program used in past as garbage value.
You might think, why the value of newly allocated memory is not reset to 0 or
NULL, which affect performance a bit.
Initializing a variable means assigning some value to it for the very first time. We must override garbage value, just after variable declaration. Hence, it is considered a good programming practice to initialize your variables with 0 or
NULL. 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 could be as simple as sum of two numbers
sum = num1 + num2
Or complex as complex algebraic equations
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 -
- C does not support any exponential operator. Do not ever confuse
^as power or exponential operator. Use
pow(base, exponent)library function present in
math.hto find power.
Read more - Bitwise XOR ^ operator
- There is no root operator. Use
sqrt();function present in
math.h, to find square root of a number.
math.hlibrary to evaluate trigonometry functions.
- Do not forget to group expression with in a pair of parenthesis to prioritize its evaluation.
Read more - Precedence and associativity of operators
- Do not forget to cast integers to
doublebefore division to get fractional results.
Example to convert mathematical equations to C expression
A = M_PI * radius * radius
M_PIis a constant defined in
Read more - How to define constants in C?
1 / ((x*x) + (y*y))
Or you can also use
pow()to evaluate exponents.
1 / (pow(x, 2) + pow(y, 2))
x = sqrt((b*b) – 4*a*c)
x = sqrt(pow(b, 2) – 4 * a * c)
x = (-b + sqrt(pow(b, 2) – 4 * a * c)) / 2 * a
x = cos(a) * cos(b) – sin(a) * sin(b)
sin()are trigonometric function present in