C/Variables and Expressions
In C, a variable is a named area of storage in memory. Memory is some form of physical hardware on a computer that either temporarily or permanently stores data. To that extent, variables are declared, defined and used to manipulate a stored value.
To use a variable it must first be declared, preceding the alphanumeric representation with a type. The type used to represent a variable is a constraint placed on the representation of the data’s format and length in memory. Primitive data types in C, for instance, are restricted to storage in memory that is no greater than 8, 16, 32, or 64 bits. One example of a data type in C is
int, which has a size suggested by the host machine architecture, for example, it may be restricted to 32 bits. In addition to the length, types are constrained by format. In this case, the
int is limited to integer data. The declaration of a variable, then, starts with the data type, and is followed by the identifier (the alphanumeric name). To indicate the end of the declaration, use a semicolon. Like this:
In this example, ‘int a;’, has a data type of ‘int’ and an identifier, ‘a’. The variable ‘a’, can store a value no greater than the value specified in
INT_MAX - a value defined in the header file <limits.h>. If the width of an
int happens to be 32 bits, then
INT_MAX will be 2147483647. The value currently assigned to the variable ‘a’, is undefined, but is constrained to an integer format.
In C, you can assign a value to a variable when defining it. To do so, use the assignment operator, ‘=’. The assignment operator should not be confused with the mathematical sign of equality. In mathematics it is used to indicate equality; however, in C, it is used to assign a value to a variable. So, the definition of a variable, in C, begins with the variable identifier, followed by the assignment operator, and then the initializer (e.g. an integer when assigning to an integer type). Like the declaration, to indicate the end of the definition, use the semicolon. Here is an example:
a = 16;
In this example, the value 16 is assigned to the variable ‘a’.
To manipulate the variable, ‘a’, declared and defined in the previous section, an expression is needed. By definition, an expression, in C, is an interpreted combination of values, variables, operators or functions. There are a number of operators available including addition, ‘+’, subtraction, ‘-‘, division ‘/’, and multiplication ‘*’. In an expression, the variable name on the left side of the assignment operator represents the area of memory that stores interpreted results. Variable and constants on the right side of the assignment operator are interpreted to determine a result prior to assignment. Note these definitions and declarations:
int a; int b; a = 0; b = 8;
What follows is a statement which manipulates storage in memory (an expression becomes a statement when it is followed by a semicolon):
a = b + 24;
In this statement, the constant ‘24’, is added to the value stored in the variable ‘b’. The result of that calculation, then, is assigned to a memory location, symbolically represented by the variable ‘a’. After the interpretation of the statement, the variable 'a' is assigned the value 32.
|Previous: Typecasting — C/Variables and Expressions — Next: C/Flow Control|