Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C

Learn step by step



C Variables

A variable is nothing but a name given to a storage area that our programs can manipulate. Each and every variable in C has a specific variable type, The variable type determines the size and layout of the variable's memory, the range of values that can be stored in that memory area, and the set of operations that can be applied on the variable.

The name of a variable can be composed of letters, digits, and the underscore character. But it must begin with either a letter or an underscore. C is case-sensitive so upper and lowercase letters are distinct. Based on the basic data types explained in previous chapter, there are following basic variable types:

TypeDescription
charTypically a single byte(one character).
int It contains numbers without precision point.
floatContain single-precision point floating point value.
doubleSame as float but contains higher range values.
voidRepresents the absence of type.

C programming language also allows us to define various other types of variables also, which we will cover in subsequent chapters like Pointer, Array, Structure, Union, Enumeration, etc. In this chapter, let us concentrate on only basic variable types

Variable Definition in C:

A variable definition means defining a variable in any program it to tell the compiler to create a variable of defined type. A variable definition specifies a data type for the variable which specify how much space to be reserved in the memory for variable and contains a list of one or more variables of that type syntax:

type variable_list;

Here, type must be a valid C data type including char, int, float, double, bool, etc. or any user-defined object and variable_list may consist of one or more identifier names separated by commas. Examples of some valid declarations are shown:

int i, j, k;
char c, ch;
float f, salary;
double d;

The line int i, j, k; It declares and defines the variables i, j and k; which instructs the compiler to create variables named i, j and k of type int.

Variables can be initialized (assigned an initial value) in their declaration. Although it is not necessary to initialize the variable at declaration you can initialize the variable at run time. The initializer consists of an equal sign followed by a constant expression as follows:

type variable_name = value;
Variable Declaration in C:

A variable declaration provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable. Declaration of variable has its meaning at the time of compilation only, at the time of linking of the program compiler needs an actual variable declaration.

A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. We use extern keyword to declare a variable at any place. With extern keyword you can declare a variable multiple times in your C program but it can be defined only once in a file, program, a function or a block of code.

Try following example, where variables have been declared at the top of the program, but they have been defined and initialized inside the main function:


#include <stdio.h>

// Variable declaration:
extern int p, q;
extern int r;
extern float s;

int main ()
{
/* variable definition: */
int p, q;
int r;
float s;

/* actual initialization */
p = 8;
q = 12;

r = p + q;
printf("value of r : %d \n", r);

f = 40.0/3.0;
printf("value of s : %f \n", s);

return 0;
} RUN

Lvalues and Rvalues in C:

There are two kinds of expressions in C language:

  • value : An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.
  • rvalue : An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.

Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues only and so may not be assigned and can not appear on the left-hand side. Because of this following is a valid statement:

int g = 20;

But following is not a valid statement because it contain numeric literal at left side and would generate compile-time error:

10 = 20;


Related Videos