Pages

Monday 27 June 2011

Constants in C



The quantities involved in programming are not all of variable types alone. Some of the quantities do not change their values as variables do. They are called constants.
A constant is an entity with fixed value that does not change. It can be stored at a location in the memory of the computer and can be referenced through that memory address. There are four basic types of constants in C, viz. integer constants, floating- point constants, character constants and string constants. Composite types may also have constants.
Integer and floating-point constants represent numbers. They are often referred to collectively as numeric-type constants.
C imposes the following rules while creating a numeric constant type data item:
Commas and blank spaces cannot be included within the constants
• The constant can be preceded by a minus(-) sign if desired.
• Value of a constant cannot exceed specified maximum and minimum bounds.
For each type of constant, these bounds will vary from one C-compiler to
another.
Constants are the fixed values that remain, unchanged during the execution of a program and are used in assignment statements Constants can also be stored in variables.
The declaration for a constant in C language takes the following form:
const datatype var_name = value;

Exapmle: const float pi = 22/7;

This declaration defines a constant named pi whose value remains 22/7 throughout the program in which it is defined.
C language facilitates five different types of constants.
1. Character
2. Integer
3. Real
4. String
5. Logical
Character Constants: A character constant consists of a single character, single digit, or a single special symbol enclosed within a pair of single inverted commas. The maximum length of a character constant is one character.
Ex.:'a' is a character constant
Ex.: 'd' is a character constant
Ex.: 'P' is a character constant
Ex.: '7' is a character constant
Ex.: '*' is a character constant
Integer Constants: An integer constant refers to a sequence of digits and has a numeric value. There are three types of integers in C language: decimal, octal, hexadecimal.
Decimal integers : 1,56, 7657,-34 etc.
Octal integers : 076, -076,05 etc. (preceded by zero, 0)
Hexadecimal integers : 0x56, -0x5D etc. (preceded by zero, Ox)
No commas or blanks are allowed in integer constants.
Real or floating point Constants: A number with a decimal point and an optional preceding sign represents a real constant.
Exapmle: 34.8, -655.33, .2, -.56, 7.
Note that 7 is an integer while 7. or 7.0 is real.
Another notation (called scientific notation) for real constants consists of three parts:
i) A sign (+ or 0) preceding the number portion (optional).
ii) A number portion
iii) An exponent portion following the number portion (optional). It
starts with E or e followed by an integer. Which may or may not
be preceded by a sign.
Example:




Valid Representations


Invalid Representations


+ . 72


12 (no decimal)


+ . 72


12 (no decimal)


+ 72.


7.6 E + 2.2(non integer exponent)


+ 7.6 E + 2


1.2 E9229892(very large exponent)


24.4e-5

String Constants: A string constant is a sequence of one or more characters enclosed with in a pair of double quotes (" "). If a single character is enclosed within a pair of double quotes, it will also be interpreted as a string constant and not a character constant.
Exapmle:
1. "Welcome To C Programming \ n"
2. "a"
Actually, a string is an array of characters terminated by a NULL character. Thus, "a" is a string consisting of two characters, viz. 'a' and NULL('\0')
Logical Constants:A logical constant can have either true value or false value. In C, a non zero value is treated as true while 0 is treated as false.

c programmming online,basic of c language,structure of c programming,c language statements,Compiling a C Program,Pseudo Codes in c,basis data type in c ,variables in C Program,constants in C Program,c operators in C Program,special operators in C ,Decision Making,Branching & Looping in C Program,if statement in C Program,for loop in C Program

No comments:

Post a Comment