Monday, April 18, 2016

Constants in C

What is constant in C?

Constant in C refer to fixed values that do not change during the execution of a program. C supports several types of Constants those are listed below.

Types of Constants

The figure shows the types of constants in c

Integer Constants

An Integer constants refers to a sequence of digits. there are Three types of integers, namely, decimal octal and hexadecimal.
Decimal integers consist of a set of digits, 0 through 9, preceded by an optional - or + sign. Valid examples of decimal integer constants are: 123, -321, 0, 62543, +788...
Embedded spaces, commas, and non-digit characters are not permitted between digits. For example : 15 750 20,000 $1000..
An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0. Example: 037, 0. 0435, 0551
A sequence of digits preceded by 0x and 0X is considered as hexadecimalinteger. They may also include alphabets. examples; 0X2, 0X9F,0Xbcd..

Important tips

  1. The range of the integer constants depends on the complier.
  2. Integr constant value is assigned to integer variable
  3. Integer values does not have decimal point

Real Constants

Integer numbers are inadequate to represnt quantities that vary continuously, such as distance, heights, teperatures,prices,and so on. These quantities are represented by numbers containg fractional parts like 17.458. Such numbers are called real or(Flaot point) constants. Example: 0.0083, -0.75 .

Single character constants

A single character constant contains a single character enclosed with in a pair of single quote marks. example : '5','X','a'..
Note that the character constant '5' is not the same as the number 5. the last constant is a blank space.

String Characters

A string constant is a sequence of character enclosed in double quotes. the characters may be letters, numbers, special characters and blank space. example : "Hai", "543","?..!".

Backslash Character

C Supports some special backslash character constants that are used in output functions. For example the symbol '\n' stands for new line character. A list of such backslash character constants is given in Table.
Constant Meaning
'\a' audible alert(bell)
'\b' back space
'\f' form feed
'\n' new line
'\r' carriage retur
'\t' horizontal tab
'\v' vertical tab
'\" single quote
'\ " ' doubble quote
'\?' Question mark
'\0' null

1 comment: