An entity that may vary during program execution is called a variable. Variable names are names given to locations in memory. These locations can contain integer, real or character constants. An integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant.
Rules for Variable Names:
Constants are stored in memory locations using names. Since the value stored in each location may change the names given to these locations are called variable names. Let’s assume a value 10 is stored in memory location and a name x is given to it. The location x can hold different values at different times. Here x is a variable and 10 is a constant.
Variable Definition:
A variable definition tells the compiler where and how much storage to create for the variable.
Here,
Example:
The line
Variables can be initialized (assigned an initial value) in their declaration as follows.
For definition without an initializer: the initial value of variables is undefined or it contains garbage value.
Variable Declaration:
To declare is nothing but to represent a variable. Only Variable name and its data type are represented in declaration. Declaration is just used for identification of data type. Memory is neither allocated nor reserved after declaration.
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 declaration has its meaning at the time of compilation only, the compiler needs actual variable declaration 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. 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.
Type of Variable:
Simple Variable, Arrays, Pointers, Enumeration Variables, Structures, Unions
Rules for Variable Names:
- A variable name is any combination of 1 to 31 alphabets (A to Z, a to z), digits (0 to 9) or underscore (_).
- The first character in the variable name must be an alphabet (A to Z, a to z) or underscore (_).
- No commas or blanks are allowed within a variable name.
- No special symbol other than an underscore can be used in a variable name.
- Variable name should not be keywords.
- Variables name is case sensitive.
test_x, _result, y_20
Constants are stored in memory locations using names. Since the value stored in each location may change the names given to these locations are called variable names. Let’s assume a value 10 is stored in memory location and a name x is given to it. The location x can hold different values at different times. Here x is a variable and 10 is a constant.
Variable Definition:
A variable definition tells the compiler where and how much storage to create for the variable.
type variable_list;
Here,
type
must be a valid C data type including char, int, float
, or any user-defined type; and variable_list
may consist of one or more identifier names separated by commas.Example:
char c, ch;
int i, j, k;
float f, salary;
double d;
The line
int i, j, k;
declares and defines the variables i, j,
and k;
which instruct the compiler to create variables named i, j
and k
of type int
.Variables can be initialized (assigned an initial value) in their declaration as follows.
char c= 's', ch='d';
int i=1, j=2, k=3;
float f=1.3, salary= 400.00;
For definition without an initializer: the initial value of variables is undefined or it contains garbage value.
Variable Declaration:
To declare is nothing but to represent a variable. Only Variable name and its data type are represented in declaration. Declaration is just used for identification of data type. Memory is neither allocated nor reserved after declaration.
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 declaration has its meaning at the time of compilation only, the compiler needs actual variable declaration 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. 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.
Type of Variable:
Simple Variable, Arrays, Pointers, Enumeration Variables, Structures, Unions
Related topics:
Data Types - Type Specifiers in C | Const and Volatile - Type Qualifiers in C | Local and Global Variables in C | Constants in C | Initialization in C
List of topics: C Programming
No comments:
Post a Comment