# C Language Constants

**C Language Constants**

**What is Constant?**

Constant in C language is that item whose value remains same during the execution of the program from the beginning till the end of a program.

Constant may be a numeric constant or an unknown numeric constant. Although a constant’s value is specified only once, a constant may be referenced many times in a program. Using a constant instead of specifying a value multiple times in the program can not only simplify code maintenance, but it can also supply a meaningful name for it and consolidate such constant bindings to a standard code location (for example, at the beginning).

How to declare and use constants?

The keyword const can be added to the declaration of an object to make that object a constant rather than a variable.

The general syntax of constant declaration is:

Syntax: const data-type name = value;

Here const is known as a keyword that must be used to declare a constant. Name is the name of the constant, data-type is the type of the data which we are declaring and value is the constant value of the data-type.

For example: const int manager_id=001;

Here we declared a constant named as manager_id of type integer which is assigned a value 001.

There are two simple ways in C to define constants:

• Using #define preprocessor.

• Using const keyword.

The #define Preprocessor: Following is the form to use #defines preprocessor to define a constant:

#define identifier value

For example:

• #define FIRST_NUMBER 1

The const Keyword: You can use const prefix to declare constants with a specific type as follows:

const type variable = value;

For example:

• const float pie =3.147;

• const int radius =4;

• const char c = ‘A’;

**Difference between constants and variables:**

A constant, Variable or a keyword can be formed from any combination of Alphabets, Digits and Special Symbols. A constant is an entity whose value does not change throughout the program execution.

A variable is an entity whose value keeps on changing throughout the program execution. However, it’s not a rule that the value of the variable will change. A variable value might remain same throughout the program execution. However the main difference between variable and constant is that we can’t change the value of constant in between the program, but we can change the value of the variable during program execution.

Variable’s syntax is: data_type variable_name;

Constant’s syntax is: const data type constant_name=value;

Variables can be initialized after its declaration where as constants must be initialized at the time of their declaration otherwise they will take a garbage value.

Let us take an example:

We define a variable named x in a program. Let us say x=3.After sometime during execution we change the value of x to 6. Now, x=6.Here value of x is getting changed. But the value of 3 and 6 will never change. Hence, x is called a variable and 3 is called a constant.

**Types of constants:**

There are four basic types of constants in C. They are:

• Integer constants

• Floating-point constants

• Character constants

• String constants

Integer and floating-point constants represent numbers. They are often referred to as numeric-type constants. The following rule applies to all numeric type constants:

• Comma and blank spaces cannot be included within the constants.

• Constants can be preceded by a – or + sign, if desired. If either sign does not precede the constant it is assumed to be positive.

• The value of a constant cannot exceed specified minimum and maximum bounds. For each type of constant, these bound vary from one C compiler to another.

Integer Constants:

Integer constants are whole numbers without any fractional part. Thus integer constants consist of a sequence of digits. Integer constants can be written in three different number systems: Decimal, Octal and Hexadecimal.

Rules for constructing Integer constant:

• 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

Decimal integers: A decimal integer constant consists of any combination of digits taken from the set 0 through 9. If the decimal constant contains two or more digits, the first digit must be something other than 0. The following are valid decimal integer constants.

Examples:

0

1

1234

-786

Octal integer constants: An octal integer constant can consist any combination of digits taken from the set 0 through 7. However, the first digit must be 0, in order to identify the constant as an octal number. The following are valid octal integer constants.

Examples:

0

01

0125

0555

Hexadecimal integer constant: A hexadecimal integer constant must begin with either 0x or 0X. It can then be followed by any combination of digits taken from the set 0 through 9 and A through F (either upper-case or lower-case). The following are valid hexadecimal integer constants.

Examples:

0X0

0x1

0XAB125

-0x555

Unsigned integer constants: Exceed the ordinary integer by magnitude of 2, they are not negative. A character U or u is prefixed to number to make it unsigned.

Long Integer constants: These are used to exceed the magnitude of ordinary integers and are appended by L.

For example,

50000U decimal unsigned.

1234567889L decimal long.

0123456L octal long.

0777777U octal unsigned.

Real constants:

A real or floating-point constant is a base-10 number that contains either a decimal point or an exponent or both. A floating-point constant can be written in two forms: Factorial form or Exponential form. A floating-point constant in a fractional form must have at least one digit each to the left and right of the decimal point. A floating-point in exponent form consists of a mantissa and an exponent. The mantissa itself is represented as a decimal integer constant or a decimal floating-point constant in fractional form. The letter E or e and the exponent follow the mantissa. The exponent must be a decimal integer. The actual number of digits in the mantissa and the exponent depends on the computer being used.

Rules for constructing Real constants:

Rules for constructing Real constants (Fractional Form)

1) A real constant must have at least one digit

2) It must have a decimal point

3) It could be either positive or negative

4) If no sign precedes an integer constant, it is assumed to be positive.

5) No commas or blanks are allowed within a real constant.

E.g.: +867.9, -26.9876, 654.0

Rules for constructing Real constants (Exponential Form)

1) The mantissa part and the exponential part should be separated by the letter ‘e’

2) The mantissa may have a positive or negative sign (default sign is positive)

3) The exponent must have at least one digit

4) The exponent must be a positive or negative integer (default sign is positive)

5) The range of real constants in exponential form is -3.4e38 and -3.4e38

E.g.: +3.2e-4, 4.1e8, -0.2e+4, -3.2e-4

Single character constants:

A single character constant contains a single character enclosed within a pair of single quotation marks.

Ex: ’5’ ‘X’ ‘;’.

Characters are stored internally in computer as coded set of binary digits, which have positive decimal integer equivalents. The value of a character constant is the numeric value of the character in the machine’s character set. This means that the value of a character constant can vary from one machine to the next, depending on the character set being used on the particular machine. For example, on ASCII machine the value of ‘A’ is 65 and on EBCDIC machine it is 193.

Rules for constructing Character constant:

1) A character constant is an alphabet, a single digit or a single special symbol enclosed within inverted commas. Both the inverted commas should point to the left. For example, ’S’ is a valid character constant whereas ‘S’ is not.

2) The maximum length of a character constant can be 1 character. Allots 1 byte of memory

E.g.: ’B’, ’l’, ’#’

There are two types of character constants. They are:

• Direct

• Escape sequences.

Direct: A single character is enclosed within single quotation marks.

Ex:’B’.

Escape sequences: There are some non-printable characters that can be printed by preceding them with ‘’backslash character. Within character constants and string literals, you can write a variety of escape sequences. Each escape sequence determines the code value for a single character.

String constants:

It consists of sequence of characters enclosed within double quotes. The characters may be letters, numbers, special characters and blank space. For example,

“red”

“Blue Sea”

“41213*(I+3)”.

## Comments