C Variables

Introduction

C++ is a statically typed language, which means that types are checked at compile time. The process by which types are checked is referred to as type-checking.

lvalue: An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.
rvalue: An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.

A definition of a variable allocates storage for the variable and may also specify an initial value for the variable. There must be one and only one definition of a variable in a program.

A declaration makes known the type and name of the variable to the program. A definition is also a declaration: When we define a variable, we declare its name and type. We can declare a name without defining it by using the extern keyword. A declaration that is not also a definition consists of the object's name and its type preceded by the keyword extern.

Declaration of Storage Class

Variables in C have not only the data type but also storage class that provides information about their location and visibility. The storage class divides the portion of the program within which the variables are recognized.

  • auto : It is a local variable known only to the function in which it is declared. Auto is the default storage class
  • static : Local variable which exists and retains its value even after the control is transferred to the calling function.
  • extern : Global variable known to all functions in the file
  • register : Social variables which are stored in the register.

Tips

  • Assign the variable an initial value that is outside the range of meaningful values for that variable.
  • The proper C++ style is to declare variables when and where they are needed
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.