Variables in C

6 – Variables in C Language

Today we will study Variables in C.

Variable

A quantity whose value may change during execution of the program is called variable.

Variable represents storage or memory location in the computer location. Data is stored in the memory location. The variable name, remains fixed during program execution but data stored in that location may change from time to time.

Each variable in C has a specific type, which determines the layout & size of the variable’s memory, the range of values can be stored within that memory & set of operations can be applied to the variable.

The Variables in C can be composed of letters, digits & Underscore character. It must begin with a letter or either an underscore. Upper & Lower Case letters are Distinct because C is a Case-sensitive Language.

Basic variable Types

Based on the basic types explained in the previous chapter, Here will be the following basic Variables types.

Sr.NoTypes & Description
1Char
It’s an integer type. A single octet (one byte).
2int
The natural size of integer for the machine.
3float
Single-Precision floating point value
4double
double-Precision floating point value
5void
It represents the absence of type.

Rules for writing Variables Names

Any variable declared in a C program should confirm to the following rules:

  • They must always begin with a letter although some systems permit underscore as a first character.
  • The length of variable name depends upon the compiler of C.
  • Blank Space is not allowed.
  • A variable name should not be a keyword.
  • It should not contain any special characters like, #, ^ etc.
  • A variable name declared for one data type can not be used for a different data type.

C is a case-sensitive language. Therefore, variable names with same spelling but different cases are treated as different.
For Example: Pay & pay are two different variables.

Valid / Invalid Variables Names

Some examples of Valid / Invalid variables Names are following:

Variable NameVaild / InvalidRemarks
numVaild
performVaild
switchInvalidkeyword
lubnaVaild
intInvalidkeyword
3pakInvalidstarts with a numeral
x-yInvalidSpecial character is not allowed except underscore
Roll noInvalidSpace is not allowed

Variable Declaration

Assigning Name & Data type so that a variable can hold is called declaration of variables. All variables that are used in C are declared using variable declaration statement.

Syntax
The syntax to declare a variable in C is:
Data type List of variables

Where,
Data Type specifies data types of variables.
For Example: int, float etc.
List of Variables Specifies a list of variables separated by commas.

Different statements are used to declare variables of different data types.
For Example: to declare variable abc, xyz, d & s of integer type. Then the statement is written as:

int abc,xyz,d,s;

To declare variables a & xy as int type, b has float type, ch as char type & sum as double, the sum are written as:

int a,xy;
float b;
char ch;
double sum;

Example:

Try the below example, where the variables has been declared at the top, but they have been declared & initialized inside the main function.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>

// variable declaration:
extern int a, b;
extern int c;
extern float f;

int main () {
   
    /* variable definition : */
    int a, b;
    int c;
    float f;

    /* actual initialization */
    a = 10;
    b = 20

    c = a + b;
    printf("value of c : %d \n", c);
   
    f = 70.0/3.0    
    printf("value of f : %f \n", f);

    return 0;

}

When the above code is compiled & executed, then it produces the following result:


1
2
value of c : 30
value of f : 23.333334

The same concept applies on function declaration, where you provide a function at the time of its declaration & it is actual definition can be given anywhere.
For Example:


1
2
3
4
5
6
7
8
9
10
11
12
// function declaration
int func();

int main () {
    // function call
    in i = func();
}

// function definition
int func () {
    return 0;
}

Lvalues & Rvalues

There are two kinds of expressions in C:

  • Lvalue – Expressions which refers to a memory location are called ‘lvalue’ expressions. lvalue may appear as either the left hand or right hand of an assignment.
  • rvalue – The term rvalues refers to that data value which is stored at some address in C. rvalue is that expression which cannot have an assigned value which means rvalue may appear on the righ hand but not on left hand side of an assignment.

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


1
2
3
int g = 20 // valid statement.

10    = 20 // invalid statement would generate compile time error.

Subscribe our channels:

Click Here for more Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.