What is Variables in Programming Language
VARIABLES
Variable is a named location in memory that is used to hold a value that may be modified by the program. All variables must be declared before they can be used. The general form of a declaration is:
type variable_list;
Here, type must be a valid data type plus any modifiers, and variable_list may consist of one or more identifier names separated by commas.
Where Variables Are Declared
Variables will be basically declared in three basic places: inside functions, in the definition of function parameters, and outside of all functions. These are local variables, formal parameters, and global variables.
Local Variables
Local variables are those Variables that are declared inside a function. local variables are not known outside their own code block. Remember, a block of code begins with an opening curly brace and terminates with a closing curly brace.
Local variables exist only while the block of code in which they are declared is executing. That is, a local variable is created upon entry into its block and destroyed upon exit. The most common code block in which local variables are declared is the function.
For example, consider the following two functions:
void func1(void)
{
int x;
x = 10;
}
void func2(void)
{
int x;
x = -199;
}
In this example the integer variable x is declared twice, once in func1() and once in func2(). The x in func1() has no bearing on or relationship to the x in func2(). This is because each x is only known to the code within the same block as the variable declaration.
Local Variables are created and destroyed with each entry and exit from the block in which they are declared, their content is lost once the block is left. This is especially important to remember when calling a function. When a function is called, its local variables are created, and upon its return they are destroyed. This means that local variables cannot retain their values between calls. (However, we can direct the compiler to retain their values by using the static modifier.) local variables are stored on the stack. The fact that the stack is a dynamic and changing region of memory explains why local variables cannot, in general, hold their values between function calls.
We can initialize a local variable to some known value. This value will be assigned to the variable each time the block of code in which it is declared is entered. For example:
#include
void f(void);
int main(void)
{
int i;
for(i=0; i<20; i++) f();
return 0;
}
void f(void)
{
int j = 20;
printf(”%d “, j);
j++; /* this line has no lasting effect */
}
This Program prints the number 10 ten times.
Formal Parameters
If a function is to use arguments, it must declare variables that will accept the values of the arguments. These variables are called the formal parameters of the function. They behave like any other local variables inside the function. As shown in the following
program fragment, their declarations occur after the function name and inside parentheses:
/* Return 1 if c is part of string s; 0 otherwise */
int is_in(char *s, char c)
{
while(*s)
if(*s==c) return 1;
else s++;
return 0;
}
The function
is_in()
has two parameters:
s
and
c
. This function returns 1 if the character
specified in
c
is contained within the string
s
; 0 if it is not.
Formal variable is also dynamic and are destroyed upon exit from the function. As with local variables, we may make assignments to a function’s formal parameters or use them in any allowable expression. Even though these variables receive the value of the arguments passed to the function, you can use them like any other local variable.
Global Variables
Global variables are known throughout the program and may be used by any piece of code. Also, they will hold their value throughout the program’s execution. You create global variables by declaring them outside of any function. Any expression may access them, regardless of what block of code that expression is in.
#include
int count; /* count is global */
void func1(void);
void func2(void);
int main(void)
{
count = 100;
func1();
return 0;
}
void func1(void)
{
int temp;
temp = count;
func2();
printf(”count is %d”, count); /* will print 100 */
}
void func2(void)
{
int count;
for(count=1; count<10; count++)
putchar(’.');
}
In this program the variable count has been declared outside of all functions. Although its declaration occurs before the main() function, you could have placed it anywhere before its first use as long as it was not in a function. However, it is usually best to declare global variables at the top of the program.
Storage for global variables is in a fixed region of memory set aside for this purpose by the compiler. Global variables are helpful when many functions in your program use the same data.
we have to avoid using unnecessary global variables, because They take up memory the entire time your program is executing, not just when they are needed. We can also using a global where a local variable would do makes a function less general because it relies on something that must be defined outside itself. Finally, using a large number of global variables can lead to program errors because of unknown and unwanted side effects. A major problem in developing large programs is the accidental changing of a variable’s value because it was used elsewhere in the program. It only happens in C/C++ if we can use too many global variables in your programs.
Popularity: 1% [?]
Posted on October 17, 2009 at 5:32 am
Your undertaking colleagues may demand changed, or your overseer may have been replaced with another.
Examples of which are computer aided lay out applications, delineated arts editor, video editors and programming applications.
Posted on October 17, 2009 at 10:24 am
The same solution that has mature very all the rage is to stock your hand-me-down laptop instead of cash.
There were once 50 million Nazis that hated our souls, and if gave the occur would take killed every one of us.
Posted on October 17, 2009 at 10:52 pm
It can make in at one’s fingertips for relief purposes, enabling whole to watch movies while traveling.
Again, for simple tasks such as these, anything more than 512MB will be more than sufficient.
Posted on October 17, 2009 at 11:58 pm
Make sure the projector or box, DVD Entertainer or Laptop, and be activated is all working.
You don’t covet to devote your unsolvable earned moolah on something that won’t work and that you can’t return to your vendor if you are unhappy.
Posted on October 18, 2009 at 1:06 am
Dupe is slight be that as it may sturdy freeware software that provides a suitable feeling of information that usable appropriate for ASP (Connection of Shareware Professionals) and webmasters for obtaining of analytical advice about his competitors that labourers looking for upshot elevation and website promotion.
At the dialect right least you purpose have in the offing to look at your action, so it should look good.
Posted on October 18, 2009 at 2:11 am
These types of cd or dvds can merely be recorded once.
It may position for the sake of comprehensible protection, like keeping your cover from getting scratched, but benefit of more serious shelter you lack to get another sturdier case.
Posted on October 18, 2009 at 6:41 am
Ecommerce store owners do comprise the advantage when using Paypal’s shopping cart that the whole shebang is included as definitely as their payment options are distressed, but many other options totally are not handy representing different ecommerce stow away owners.
The against is to advance to as many levels as you can and pocket as many points as you can.
Posted on October 19, 2009 at 3:07 am
The most fundamental software may include media players and codecs that desire break up you sentiment TV size seamlessly.
In front we fall heir to into the details of registry cleaners, ease up’s layer principled a minuscule report regarding how your computer runs and why it slows down all through time.
Posted on October 19, 2009 at 10:17 am
Most people on the spider’s web wish skim the article looking for chief points.
Many are still against to hanger-on telly watching with a dish primarily for those who are located in the tramontane areas.
Posted on October 20, 2009 at 6:53 am
Exclusively fools rush in. You be subjected to to positive your purchaser just as a huntress knows their prey.
natural viagra substitutes cialis
Scanning my recipes, I rest a fast,healthy meal during families, wrote up how I developed the recipe andsent it in.
Posted on October 17, 2009 at 4:24 am
Dream close to how cosy it is to write today using computers stuffed with word processor programs, amount to checkers, grammar programs, and inscribe by number programs.
When possible, fix up your systems such that wonted tasks are as automated as possible.