C Variables

A variable is a named memory location that can hold a single value (numeric or character)or a name given and associated to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

The C language demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it.

The name of a C variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C is case-sensitive. Based on the basic types explained in previous chapter, there will be the following basic variable types:

Type	Description char	Typically a single octet(one byte). This is an integer type. int	The most natural size of integer for the machine. float	A single-precision floating point value. double	A double-precision floating point value. void	Represents the absence of type.

C programming language also allows to define various other types of variables, which we will cover in subsequent chapters like Enumeration, Pointer, Array, Structure, Union, etc. For this chapter, let us study only basic variable types.

The Programming language C has two main variable types

Local Variables

Global Variables

Local Variables


 * 1) Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block.
 * 2) When a local variable is defined - it is not initalised by the system, you must initalise it yourself.
 * 3) When execution of the block starts the variable is available, and when the block ends the variable 'dies'.

Global Variables

Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it.

Global variables are initalised automatically by the system when you define them! Data Type 	Initialser int 	         0 char 	        '\0' float 	         0 pointer   	  NULL

If same variable name is being used for global and local variable then local variable takes preference in its scope. But it is not a good practice to use global variables and local variables with the same name.