Defining Variables
A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer.
Naming Variables
The name of variable can be called identifier or variable name in a friendly way. It has to follow these rules:
- The name can contain letters, digits and the underscore but the first letter has to be a letter or the underscore. Be avoided underscore as the first letter because it can be clashed with standard system variables.
- The length of name can be up to 247 characters long in Visual C++ but 31 characters are usually adequate. Keywords cannot be used as a variable name.
Of course, the variable name should be meaningful to the programming context.
Declaring Variables
To declare a variable you specify its name and kind of data type it can store. The variable declaration always ends with a semicolon, for example:
1.
int
counter;
2.
char
ch;
You can declare variables at any point of your program before using it. The best practice suggests that you should declare your variables closest to their first point of use so thesource code is easier to maintain. In C programming language, declaring a variable is also defining a variable.
Initializing Variables
You can also initialize a variable when you declare it, for example:
1.
int
x = 10;
2.
char
ch =
'a'
;
Storage of Variables
Each variable has its own lifetime (the length of time the variable can be accessible) or storage duration. When you declare your variable you implicitly assign it a lifetime. Let take a look at this source code example:
01.
#include <stdio.h>
02.
03.
int
global_variable = 10;
// global variable
04.
05.
void
func();
06.
07.
void
main()
08.
{
09.
int
i;
10.
// test static variable
11.
for
(i = 0; i < 5 ; i++)
12.
{
13.
func();
14.
printf
(
"after %d call \n"
,i);
15.
}
16.
}
17.
void
func()
18.
{
19.
static
int
counter = 0;
// static variable
20.
counter++;
21.
printf
(
"func is called %d time(s)\n"
,counter);
22.
23.
int
local_variable = 10;
24.
25.
}
Explanations
global_variable is a global variable. It is visible and accessible to all functions. It has static life time (static means variable is retained until the program executes). It is suggested that we should avoid using global variable because it is difficult to maintain, and we don’t know exactly the state of global variable because any functions can change it at any time of program execution.
The local_variable and i are only existed until function completed. We cannot access it anymore. In this case it is call automatic lifetimes.
In case you want a local variable has static lifetimes, you can use static keyword like counter variable in func(). It retains until program executes even when the func()completed.
extern and register keywords
You can use extern keyword when declaring a variable or function to imply that the variable is implemented elsewhere and it will be implement later on.
register keyword is used when you want a variable which is accessed many time and required fast memory access. Be noted that, declaring a variable with register keyword acts as a directive. It means it does not guarantee the allocation of a register for storing values.
Scope of Variables
You can define variable in a block of code which specifies by {and} braces. The variable has the scope inside block it is declared.
source:cprogramlanguage.net
No comments:
Post a Comment