Variables

Variables

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,

void function1 (void)

{

int x;

x =10 ;

}

 

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.

int is_in(char *s, char c)

{

while(*s)

if (*s==c) return1;

else s++;

return 0;

}

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.

void function1 (void);
int main(void)

{

count =100;

function1 ();

return 0;

}

Related Post

How to use Flutter most usable Widgets | Flutter t... We need some basic widgets for creating our first flutter app. Widgets describe what their view should look like given their current configuration and...
UIPopOverPresentationController In Swift UIPopOverPresentationController UIPopOverViewController in swift is writen in swift 3.1 and Use the xcode Version 8.3.2. For use this import the Pop...
Dart Data type | Dart Tutorial Dart Data type Variables can store data of different types, and different data types can do different things. Dart supports the following data typ...
Emailing Phrases Most useful emailing phrases There are some standard phrases that are used in business or formal letters. These are essential to give a professional ...
New Tools for 64-bit Support in Xcode 9.3   New Tools for 64-bit Support in Xcode 9.3 The last macOS release to support 32-bit apps without compromise is macOS High Sierra. Make sure fu...
Custom View by use of xib in swift Custom View by use of xib in swift We can create the custom and common view by the use xib in swift 3.1 and xcode 8.3.2 . Custom views are common and...

Leave a Reply

Your email address will not be published. Required fields are marked *