Variables in C have the same meaning as variables in algebra. A variable in C is a storage unit, which sets a space in memory to hold a value and can take different values at different times during program execution.
1. A variable name may consists of letters, digits and the underscore ( _ ) characters.
2. A variable name must begin with a letter. Some system allows to starts the variable name with an underscore as the first character.
3. ANSI standard recognizes a length of 31 characters for a variable name. However, the length should not be normally more than any combination of eight alphabets, digits, and underscores.
4. Uppercase and lowercase are significant. That is the variable Totamt is not the same as totamt and TOTAMT.
5. The variable name may not be a C reserved word (keyword).
Generally, C programmers maintain the following conventions for naming variables.
Every C word is classified as either a keyword or an identifier. Each keyword has a specific meaning and these meanings cannot be changed. Keywords serve as basic building blocks for program statements. There are only 32 keywords in C. The list of all keywords in ANSI C is listed in the following table. All keywords must be written in lowercase.
Constants in C refer to a specific quantity that doesn't change during the execution of a program.
|241||Decimal Integer||047||Octal Integer|
|-973||Decimal Integer||053||Octal Integer|
|0||Decimal Integer||0X59||Hexadecimal Integer|
|+4177||Decimal Integer||0x47F||Hexadecimal Integer|
Each program needs a certain kind of data for displaying a meaningful result. This certain kind of data are known as a data type.
ANSI C supports four classes of data types :
All C compilers support four fundamental data types
|Type||Range of values||Description|
|-128 to 127||a single byte(8 bits) and can store one character type data|
Integers (whole numbers)
|-32768 to 32767||an integer type is used to represent whole numbers within a specified range of values.|
|3.4e-38 to 3.4e+38||single-precision floating point|
|1.7e-308 to 1.7e+308||double-precision floating point|
Character Types :
A single character can be defined as a character type data. Characters are usually stored in 8 bits of internal storage. The qualifier signed or unsigned may be explicitly applied to char. While unsigned chars have values between 0 and 255, signed chars have values from -128 to 127.
Integer Types :
C has three classes of integer storage, namely short int, int, and long int, in both signed and unsigned forms.
The keywords signed and unsigned are the two sign qualifiers which specify whether a variable can store positive or negative or both numbers.
The keyword signed uses one bit for a sign and 15 bits for the magnitude of the number in a 16-bit machine.
The keyword unsigned uses to store all the bits for the magnitude of the number and always positive.
Floating Point Types :
Floating point numbers are stored in 32 bits with 6 digits of precision. Floating point numbers are defined in C by the keyword float. When the accuracy provided by a float number is not sufficient, the type double can be used to define the number.
Double Point Types :
A double data type number uses 64 bits giving a precision of 14 digits. These are known as double precision numbers. Remember that double type represents the same data type that float represents but with a greater precision. To extend the precision further, we may use long double which uses 80 bits.
The following table shows the size and range of the type-specifiers on most common implementations :
|char or signed char||8||-128 to 127|
|unsigned char||8||0 to 255|
|int or signed int||16||-32768 to 32767|
|unsigned int||16||0 to 65535|
|short int or signed short int||8||-128 to 127|
|unsigned short int||8||0 to 255|
|long int or signed long int||32||-2147483648 to 2147483647|
|unsigned long int||32||0 to 4294967295|
|float||32||3.4E-38 TO 3.4E+38|
|double||64||1.7E-308 TO 1.7E+308|
|long double||80||3.4E-4932 TO 1.1E+4932|
Variables are given initial values, or initialized, when declared. See the following examples :
char abc = 'X';
int marks =77;
float amount = 45.23;
Write initialized variables on a separate line and a comment beside the variable name is a good idea. See the following examples :
int qty; /* quantity of an item */
float value = 12.10; /* purchase value of an item */
int marks; /* marks of a subject */
Solve these problems: