C - Variables



A variable in C is a user-assigned name to a certain location in computer’s memory. In the modern computer’s architecture, the memory is a collection of a large number of randomly accessible locations capable of holding a single bit. Each location in the memory is identified by a unique address, expressed in binary (or Hexa-decimal for convenience) format. Since it is extremely cumbersome to store and process the data in the memory by referring the locations in binary form, the high-level languages such as C let the locations be identified by user-defined names.

Memory

Instead of identifying a free memory location and assigning it a value, you can find a suitable mnemonic identifier and assign it a value. The C compiler will choose an appropriate location and bind it to the identifier specified by you. In the above diagram, an integer 18 is assigned to a variable named as age. The compiler internally assigns a memory location to the name of the variable.

Naming Conventions

  • The name of the variable must start with an alphabet (upper or lowercase) or an underscore (_)

  • It may consist of alphabets (upper or lowercase), digits and underscore character.

  • No other characters can be a part of the name of a variable.

  • Names are case-sensitive, as a result age is not the same as AGE

  • ANSI standard recognizes a length of 31 characters for a variable name. Although you can choose a name with more characters, only the first 31 will be recognized.

  • Using a descriptive name for a variable, that reflects the value it intends to store is considered to be a good practice.

  • Avoid using very short variable names that might cause confusion.

  • C is a statically typed language. Hence, the data type of the variable must be mentioned in the declaration before its name.

  • A variable may be declared inside a function (local variable) or globally.

  • More than one variables of the same type may be declared in a single statement.

Based on the above set of rules/conventions, here are some valid and invalid variable names −

int _num = 5; // valid integer variable
float marks = 55.50; // valid float variable.
char choice = '0'; // valid char variable.
int sub-1 = 35; //invalid because name con't contain - character
avg = 50; //invalid. must have data type
int choice = 0; // invalid as name can be used for declaration only once in a function.
int sal_of_employee = 20000; // Valid integer name
int phy, che, maths; //valid because all are of same type
int sal, float tax; //error because variables of different types in same statement

In C, the variable stores data belonging to any of the types it recognizes. Hence there as many number of types of variables as the number of data types in C.

Sr.No Type & Description
1

char

Typically a single octet(one byte). It is an integer type.

2

int

The most natural size of integer for the machine.

3

float

A single-precision floating point value.

4

double

A double-precision floating point value.

5

void

Represents the absence of type.

6

Derived types

C variables can be of (a) Pointer type, (b) Structure type, (c) Union type, and (d) enum type.

Variable Declaration

As per ANSI C standard, all the variables must be declared in the beginning. Variable declaration after the first processing statement is not allowed. Although the C99 and C11 standard revisions have removed this stipulation, it is still considered a good programming practice. You can declare a variable to be assigned a value later in the code, or you can initialize it at the time of declaration.

int x = 10;        // declaration with initialization
int y;             // declaration
y = 20;            // assignment later
int d = 3, f = 5;  // definition and initializing d and f. 
char x = 'x';      // the variable x has the value 'x'.

Once a variable of a certain type is declared, it cannot be assigned a value of any other type. In such a case the C compiler reports a type mismatch error.

A variable declaration provides assurance to the compiler that there exists a variable with the given type and name so that the compiler can proceed for further compilation without requiring the complete detail about the variable. A variable definition has its meaning at the time of compilation only, the compiler needs actual variable definition at the time of linking the program.

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. You will use the keyword ‘extern’ to declare a variable at any place. Though you can declare a variable multiple times in your C program, it can be defined only once in a file, a function, or a block of code.

Lvalues and Rvalues in C

There are two kinds of expressions in C −

  • lvalue − Expressions that refer to a memory location are called "lvalue" expressions. An lvalue may appear as either the left-hand or right-hand side of an assignment.

  • rvalue − The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.

Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −

int g = 20; // valid statement
10 = 20;    // invalid statement; would generate compile-time error

Variables in C can be classified on different basis −

  • Data types − int, float, char or struct types.

  • Scope − global or local variables.

  • Storage type − automatic, static, register or extern.

We shall learn about local/global types and storage types later in this tutorial.

Advertisements