Variables

A variable is a named location in memory that is used to hold a value that can be modified by the program. All variable 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. Here are some declarations:

int i, j, k;

short int si;

unsigned int ui;

double balance, profit, loss;

Note: In C the name of a variable has nothing to do with its type.

Where Variable Are Declared

Variable can be declared in three places: inside functions, in the definition of function parameters, and outside of all functions. These positions correspond to a local variable, formal parameters, and global variable respectively.

Local Variables

Variables that are declared inside a function are called local variables. In C literature, these variables are referred to as automatic variables. Local variables can be used only by statements that are inside the block in which the variables are declared. Local variables are not known outside their own code block. A block of code begins with an opening curly brace and terminates with a closing curly brace.

The local variable exists only while the block of code in which they are declared is executing. The most common code block in which local variables are declared is the function.

For example,
[cc lang=”bash” escaped=”true” width=”auto” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]
void function1 (void)

{

int x;

x =10 ;

}[/cc]

 

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 variables inside the function. As shown in the following program fragment, their declarations occur after the function name and inside parentheses.

[cc lang=”bash” escaped=”true” width=”370″ nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

int is_in(char *s, char c)

{

while(*s)

if (*s==c) return1;

else s++;

return 0;

}
[/cc]

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. We 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.
[cc lang=”bash” escaped=”true” width=”370″ nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

void function1 (void);
int main(void)

{

count =100;

function1 ();

return 0;

}
[/cc]

Related Post

Loop Control Statement Loop control statements are used when a section of code may either be executed a fixed number of times. C gives you a choice of three types of loop st...
Variables in Java Variables in Java A variable is something which is associated with a value can be changed. For example: int i=10; Here variable name “i” wh...
Invitation Letter for Visa Invitation Letter Samples These letter are necessary for taking visa of another country. so these samples are the most important document. Sample 1 ...
New Features XCode 12 – IDE Supported Configurations Xcode 12 beta includes SDKs for iOS 14 iPadOS 14 tvOS 14 watchOS 7 macOS 11 The Xcode 12 beta release supports o...
Use of CollectionViewDataSource method moveItemAt ... Use of CollectionViewDataSource method moveItemAt for Reordering CollectionView cell In IOS (Swift 4) In this tutorial we are study about how we can r...
Decision Control Statement C has three major decision making instructions— the if statement, the if-else statement, and the switch statement. The if Statement It is used to ex...
Author

Write A Comment