Next: Type Qualifiers, Previous: Statements, Up: Top [Contents][Index]
Every variable used in a C program needs to be made known by a
declaration. It can be used only after it has been declared.
It is an error to declare a variable name more than once in the same
scope; an exception is that extern
declarations and tentative
definitions can coexist with another declaration of the same
variable.
Variables can be declared anywhere within a block or file. (Older versions of C required that all variable declarations within a block occur before any statements.)
Variables declared within a function or block are local to it. This means that the variable name is visible only until the end of that function or block, and the memory space is allocated only while control is within it.
Variables declared at the top level in a file are called file-scope. They are assigned fixed, distinct memory locations, so they retain their values for the whole execution of the program.
• Variable Declarations | Name a variable and and reserve space for it. | |
• Initializers | Assigning inital values to variables. | |
• Designated Inits | Assigning initial values to array elements at particular array indices. | |
• Auto Type | Obtaining the type of a variable. | |
• Local Variables | Variables declared in function definitions. | |
• File-Scope Variables | Variables declared outside of function definitions. | |
• Static Local Variables | Variables declared within functions, but with permanent storage allocation. | |
• Extern Declarations | Declaring a variable which is allocated somewhere else. | |
• Allocating File-Scope | When is space allocated for file-scope variables? | |
• auto and register | Historically used storage directions. | |
• Omitting Types | The bad practice of declaring variables with implicit type. |
Next: Type Qualifiers, Previous: Statements, Up: Top [Contents][Index]