C - Variable: Constants, Variables and Keywords
C - Variable: Constants, Variables and Keywords
The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. Let us see what are constants and variables in C. A constant is an entity that doesnt change whereas a variable is an entity that may change. In any program we typically do lots of calculations. The results of these calculations are stored in computers memory. Like human memory the computer memory also consists of millions of cells. The calculated values are stored in these memory cells. To make the retrieval and usage of these values easy these memory cells (also called memory locations) are given names. Since the value stored in each location may change the names given to these locations are called variable names. Consider the following example. Here 3 is stored in a memory location and a name x is given to it. Then we are assigning a new value 5 to the same memory location x. This would overwrite the earlier value 3, since a memory location can hold only one value at a time.
C Variable
C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable. The value of the C variable may get change in the program. C variable might be belonging to any of the data type like int, float, char etc.
C Constant
Prev Next
C Constant are also like normal variables except their values can not be modified by the program once they are defined. They refer to fixed values. They are also called as literals They may be belonging to any of the data type. Please see below table for constants with respect to their data type.
Types of C constant:
1. 2. 3. 4. 5. 6. Integer constants Real or Floating point constants Octal & Hexadecimal constants Character constants String constants Backslash character constants S.no Constant type data type int unsigned int 1 Integer constants long int long long int Example 53 , 762 , -478 , etc 5000u , 1000U , etc 1000L , -300L , etc 5555555LL
111.22F , 2.22e-2f 111.22 , 4.0 , 2 Real or Floating point constants float, doule -0.34565 3 4 5 6 Octal constant Hexadecimal constant character constants string constants int int char char 013 090 /* starts with 0 */ /* starts with 0x */ C
A , B,
ABCD , Hai
1. 2. 3. 4. 5. 6.
Integer Constants
An integer constant must have at least one digit. It must not have a decimal point. It can either be positive or negative. No commas or blanks are allowed within an integer constant. If no sign precedes an integer constant, it is assumed to be positive. The allowable range for integer constants is -32768 to 32767
1. 2. 3.
real Constants
A real constant must have at least one digit It must have a decimal point It could be either positive or negative
4. 5.
If no sign precedes an integer constant, it is assumed to be positive. No commas or blanks are allowed within a real constant.
1. 2. 3.
character constants
A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to left. For example, C is a valid character constant whereas C is not. The maximum length of a character constant is 1 character