Wiki


C — Variables

Naming

There are some restrictions on variable names. The variable name should start with a letter or ‘_’. After the first letter it can also include numbers. Variable names are case sensitive so, x and X are two different variables in C. Some of keywords like if, else, for, et cetera are reserved and therefore can’t be used as variable names.

Types of Variable (Scope)

Note: For this topic I’m going to use int data type but this information can be used with other data types as well.

Based on scope there can be two types of variables in C.

Local Variables

Local variables, as the name says are local to the Function or Block in which they are defined and are independent of other local variables of the same name. Generally, Local variables are automatic type by default but can be explicitly defined as static.

Global Variables

Global variables are global in the whole program and can be used by any function. But in order for compiler to know about the variable it has to be defined or declared before the actual use. The norm is to define the Global variables on top of the file and then use them in functions, but if multiple files are involved or Global variable is defined later in the file, it can be declared in the function just like are normal variable by placing extern keyword in front of the declaration. Global variables are static type by default.

Examples

Normal Usage
int g = 19;

int main (void)
{
	int h = 31;

	...

}
Global Variable Defined Later / In Different File
int main (void)
{
	extern int g;
	int h = 31;

	...
}

...

int g = 19;

Types of Variable (Nature)

Based on nature there can be two types of variables in C.

Static Variables

Static variables are persistent in memory for the time of execution of the program. This means that, values stored in static variable can be accessed on anytime under the scope. So, let’s say if a function has a static variable, then even after the function handles control back to caller, static variable stays in memory. If the function is then called again, it can access that value again. In C, Static integers are assigned ‘0’ on declaration.

static int g = 19;

Automatic Variables

All variables in function are by default Automatic Variables. These variables are not persistent in memory and as soon as function handles control back to caller, they are wiped away from memory. In C, automatic variables are not assigned any value upon declaration and hence may contain garbage value. It is recommended to assign value and then access them.

...

int function (void)
{
	auto int g = 19;

	...
}

Or simply

...

int function (void)
{
	int g = 19;

	...
}

Passing Variables

In C, variables of regular data types are passed by value. This means that, whenever a variable is passed as an argument to a function, its value gets copied into local variable of the function. It sort of get copied to the function. So, if the value of that copy is changed inside of a function, the actual value remains unaffected.

When the function is called with variables as arguments, there data types need not be specified. However, either prototype or function should be defined before calling it or compiler will throw error.

{
	...

	function(g, h);

	...
}

See Also