Previous | Home | Next |
Constants and Variables
A constant is an entity that doesn't change whereas a variable is an entity that may change. In any program we typically do many calculations and the result of these calculation are stored in computers memory. To make the retrieval and usage of the value easy these memory locations are given names. Since the value stored in each location may change the names given to these locations are called variable names.
Types of variables
As we know that an entity that may vary during the program execution is called variable .variable names are names given to location in memory. The rules of constructing different type of constants is different.
Scope of variables
The scope of a variable is often one of the things programmers don’t understand at first. Depending on where they are declared, variables can be either visible or not visible.
Life span of variable
Determining how long a variable will be kept is another problem that perplexes aspiring programmers. Let's look at the keyword modifier static. This modifier has several purposes that, unfortunately, are related. When static is used on a variable found within a function or block, it tells the compiler never to discard or reallocate the variable. The variable is created at compile time and is initialized to zero. The opposite of static in this situation is auto (the default). That variable, found inside a function or block, is reallocated every time the function or block is entered. When static is used on a variable that is defined outside any functions or blocks, its meaning is that the variable is known to only those functions contained in the specified source file, and are not known outside the source file. When a variable is known outside the source file, it is called an external variable. (Don't confuse this with the keyword extern.) The extern keyword tells the compiler that the variable is being defined (and not declared). Because extern and static conflict, they cannot be used together.
RULES FOR CONSTRUCTING VARIABLE NAMES
- A variable name is any combination of 1 to 31 alphabets, digits or underscores.
- The first character in the variable name must be an alphabet or an underscore.
- No commas or blanks are allowed with in variable name.
- No special symbol other than underscore can be used in a variable name.
Ex -- gross salary
Types of C constants
Primary constants
- Integer constants
- Real constants
- Character constants
Secondary constants
- Array
- Pointer
- Structure
- Union
Rules of constructing integer constants
- An integer constant must have at least one digit.
- It must not have a decimal point.
- It can be either positive or negative.
- The allowable range for constants is -32768 to 32767
In fact the range of integer constants depends upon compiler.
For ex. 435
+786
-7000
Rules for constructing real constants
- A real constants must have at least one digit.
- It must have a decimal point.
- It could be either positive or negative.
- Default sign is positive.
For ex. +325.34
426.0
In exponential form of representation, the real constants is represented in two parts. The part appearing before 'e' is called mantissa where as the part following 'e' is called exponent.
Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Ex. +3.2e-5
Rules for constructing character constants
- a character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.
- The maximum length of character constant can be one character.
Ex `A`
What is an identifier?
An identifier is used for any variable, function, data definition. If we talk about C then an identifier is a combination of alphanumeric characters. There are some rules which are used in naming identifiers.
Difference between definition and declaration
- Both objects and functions can be defined and declared. When we declare a variable or object then its attribute is known to the compiler. When we define a object then the attribute is known to the compiler and the object is also be created.
- Whenever a variable is defined within the function then it has local scope(local scope means that variable can only access locally). While when a variable is defined outside the function then it has global scope(global scope means we can access the variable globally or anywhere).
The simplest declaration of a variable is shown in the following code fragment:
void OurFunction(int nType) { int nTest; nTest = nType; }
In the fragment, an integer variable is defined. That is, both its attributes (the variable is an integer) were made known to the compiler, and storage was allocated.
Previous | Home | Next |