Summary: How to Declare a Simple Variable

I l @ ve RuBoard

Summary: How to Declare a Simple Variable

  1. Choose the type you need.

  2. Choose a name for the variable.

  3. Use this format for a declaration statement: type- specifier variable-name; . The type-specifier is formed from one or more of the type keywords. Here are some examples:

     int erest; unsigned short cash; 
  4. To declare more than one variable of the same type, separate the variable names with commas:

     char ch, init, ans; 
  5. You can initialize a variable in a declaration statement:

     float mass = 6.0E24; 

Summary: Storage Classes

Keywords:

 auto, extern, static, register 

General Comments:

The storage class of a variable determines its scope, its linkage, and its storage duration. Storage class is determined both by where the variable is defined and by its associated keyword. Variables defined outside all functions are external, have file scope, external linkage, and static storage duration. Variables declared inside a function are automatic unless one of the other keywords is used. They have block scope, no linkage, and automatic storage duration. Variables defined with the keyword static inside a function have block scope, no linkage, and static storage duration. Variables defined with the keyword static outside a function have file scope, internal linkage, and static storage duration.

Properties:

In the following list, those variables in storage classes above the dotted line are declared inside a function; those below the line are defined outside a function.

Storage Class Keyword Duration Scope and Linkage
automatic auto temporary local
register register temporary local
static static persistent local
external extern [*] persistent global (all files)
external static persistent global (one file)
static      

[*] The keyword extern is used only to redeclare variables that have been defined externally elsewhere. The act of defining the variable outside a function makes it external.

[*] The keyword extern is used only to redeclare variables that have been defined externally elsewhere. The act of defining the variable outside a function makes it external.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net