Home > Electronics Tutorials > C Language Tutorial > Identifiers - Variables

C Language Programming Library Reference Guide

Identifiers - Variables

A variable may be defined using any uppercase or lowercase character, a numerical digit (0 through 9), and the underscore character (_). The first character of the variable may not be a numerical digit or underscore. Variable names are case sensitive.

The scope of the variable (where it can be used), is determined by where it is defined. If it is defined outside any block or list of parameters, then it has file scope. This means it may be accessed anywhere in the current source code file. This is normally called a global variable and is normally defined at the top of the source code. All other types of variables are local variables. If a variable is defined in a block (encapsulated with { and }), then its scope begins when the variable is defined and ends when it hits the terminating }. This is called block scope. If the variable is defined in a function prototype, then the variable may only be accessed in that function. This is called function prototype scope.

Access to variables outside of their file scope can be made by using linkage. Linkage is done by placing the keyword extern prior to a variable declaration. This allows a variable that is defined in another source code file to be accessed.

Variables defined within a function scope have automatic storage duration. The life of the variable is determined by the life of the function. Space is allocated at the beginning of the function and terminated at the end of the function. Static storage duration can be obtained by placing the keyword static in front of the variable declaration. This causes the variable's space to be allocated when the program starts up and is kept during the life of the program. The value of the variable is preserved during subsequent calls to the function that defines it. Variables with file scope are automatically static variables.

A variable is defined by the following:

storage-class-specifier type-specifier variable-names,...
  The storage-class-specifier can be one of the following:
 
typedef The symbol name "variable-name" becomes a type-specifier of type "type-specifier". No variable is actually created, this is merely for convenience.
extern Indicates that the variable is defined outside of the current file. This brings the variables scope into the current scope. No variable is actually created by this.
static Causes a variable that is defined within a function to be preserved in subsequent calls to the function.
auto Causes a local variable to have a local lifetime (default).
register Requests that the variable be accessed as quickly as possible. This request is not guaranteed. Normally, the variable's value is kept within a CPU register for maximum speed.
The type-specifier can be one of the following:
 
void Defines an empty or NULL value whose type is incomplete.
char, signed char Variable is large enough to store a basic character in the character set. The value is either signed or nonnegative.
unsigned char Same as char, but unsigned values only.
short, signed short, short int, signed short int Defines a short signed integer. May be the same range as a normal int, or half the bits of a normal int.
unsigned short, unsigned short int Defines an unsigned short integer.
int, signed, signed int, or no type specifier Defines a signed integer. If no type specifier is given, then this is the default.
unsigned int, unsigned Same as int, but unsigned values only.
long, signed long, long int, signed long int Defines a long signed integer. May be twice the bit size as a normal int, or the same as a normal int.
unsigned long, unsigned long int Same as long, but unsigned values only.
float A floating-point number. Consists of a sign, a mantissa (number greater than or equal to 1), and an exponent. The mantissa is taken to the power of the exponent then given the sign. The exponent is also signed allowing extremely small fractions. The mantissa gives it a finite precision.
double A more accurate floating-point number than float. Normally twice as many bits in size.
long double Increases the size of double.

Here are the maximum and minimum sizes of the type-specifiers on most common implementations. Note: some implementations may be different.
 

Type Size Range
unsigned char 8 bits 0 to 255
char 8 bits -128 to 127
unsigned int 16 bits 0 to 65,535
short int 16 bits -32,768 to 32,767
int 16 bits -32,768 to 32,767
unsigned long 32 bits 0 to 4,294,967,295
long 32 bits -2,147,483,648 to 2,147,483,647
float 32 bits 1.17549435 * (10^-38) to 3.40282347 * (10^+38)
double 64 bits 2.2250738585072014 * (10^-308) to 1.7976931348623157 * (10^+308)
long double 80 bits 3.4 * (10^-4932) to 1.1 * (10^4932)
Examples:
  int bob=32;
Creates variable "bob" and initializes it to the value 32.

char loop1,loop2,loop3='\x41';
Creates three variables. The value of "loop1" and "loop2" is undefined. The value of loop3 is the letter "A".

typedef char boolean;
Causes the keyword "boolean" to represent variable-type "char".

boolean yes=1;
Creates variable "yes" as type "char" and sets its value to 1.
Note: To report broken links or to submit your projects, tutorials please email to Webmaster

Discover

     more......