C variable types and declarations

From Wikipedia, the free encyclopedia
Jump to: navigation, search

The C computer programming language has two variable types that must be declared before they are used.

Local Variables[edit]

The scope of a local variable is confined within the block or function where it is defined. Local variables must always be defined at the top of a block. When a local variable is defined it is not initialized by the system and must be initialized by the program. When execution of the block starts the variable is available, and when the block ends the variable 'dies'.

Global Variables[edit]

A 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 initialized automatically by the system when you define them:

Data Type Initial value
int 0
char '\0'
float 0
pointer NULL

If the same variable name is being used for both a global and a 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.