Constant

Constant

Published by: Nuru

Published date: 21 Jun 2021

Constants and Variables Photo

Constant

 Constants in C refer to fixed values that do not change during the execution of a program. Constants and Variables differ from each other as variables change their value whereas constants do not change their value during execution.

C constants can be divided into different categories:

Numeric Constants

  1. Integer Constants
  2. Real Constants

 Character Constants

  1. Single Character Constants
  2. String Constants

pic

Numeric Constants

1)Integer Constants

Integer Constant refers to a sequence of digits (at least one digit) with no decimal point and either positive or negative. If no sign precedes an integer constant, it is assumed to be positive. No commas or blank spaces are allowed within the integer constant.

E.g. 0, 123, +365, -555, etc.

$1000, 20,000, 22 329, are not allowed.

Three types of integer constant are:

  1. Decimal Integer Constants
  2. Octal Integer Constants
  3. Hexadecimal Integer Constants

 

a) Decimal Integer Constants:

Decimal integers consist of a set of digits, 0 through 9, preceded by an optional – or + sign. E.g. +78, 0, 123, etc.

b) Octal Integer Constants:

An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0.

E.g. 056, 0, 0123, etc

c) Hexadecimal Integer Constants:

A hexadecimal integer constant consists of any combination of digits from the set 0 to 9, and alphabets A through F or a through f with a leading 0X or 0x.The letters A through F represent the numbers 10 through 15.

  • E.g. 0X2, 0x9F, 0Xabc, 0x0, etc.

NOTE: Octal and Hexadecimal numbers are rarely used in programming.

 

2) Real Constants

Integer numbers are inadequate to represent quantities that vary continuously, such as distances, heights, temperatures, prices, and so on. These quantities are represented by numbers having fractional parts like 3.14.

Such numbers are called real (or floating point) constants.

E.g. 0.00123, -0.25, +125.0, 23.35, etc

Real constants can be written into two forms:

  1. fractional form
  2. exponential form

1) fractional form

Fractional form constants must have at least one digit and a decimal point. It can either be positive or negative but the default sign is positive. Commas or blank spaces are not allowed within a real constant.

E.g. +23.45, 456.0, -23.35, -5544.312, etc. are in fractional form.

2) Exponential form

In the exponential form of representation, the real constant is represented in two parts as mantissa e exponent The digits before ‘e’ is called mantissa and after is called the exponent. The mantissa part may have a positive or negative sign, but the default is positive. The exponent must have at least one digit (must be an integer), which can be either positive or negative.

E.g. -3.2e-4        implies  [-3.2*10-4]

-0.2e+3 implies  [-0.2*103]

Character Constants

a) Single Character Constants

A single character constant (or simply character constant) contains a single character alphabet, a digit, or a special symbol enclosed within a pair of single quote marks.

E.g. ‘5’, ‘X’, ‘;’ ‘’, etc.

Character constants have integer values known as ASCII values.

NOTE: The character constant ‘5’ is not the same as the number 5.

NOTE: ‘A’ is a valid character constant but ‘AA’ is not.

b) String Character Constants

A string constant is a sequence of characters enclosed in double quotes.

The characters may be letters, numbers, special characters, and blank spaces. However, it does not have an equivalent ASCII value.

E.g. “Hi!”, “2011”, “WELL DONE”, “?...!”, “5+3”, “X”, etc.

NOTE:

A character constant (e.g. ‘X’) is not equivalent to the single-character string constant (e.g. “X”).

Also, “5+3” is a string rather than an

arithmetic operation

 

Variables

A variable is a data name that is used to store a data value. Constants and Variables are opposite to each other. Unlike constants that remain unchanged during the execution of a program, a variable may take different values at different times during execution. Since a variable is an identifier, the rules for naming variables are similar to those of identifiers. A variable name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in the program.

E.g. Average, sum, counter, first_name, etc.

123, (area), %, 25th, Price$, blood group, etc. are not allowed.

 

Declaration of variable

Any variable should be defined before using it in a program.

The variables are defined or declared using the following syntax:

datatype variable_name;       where variable_name is the name of the variable.

E.g.

int a;

float radius;

char gender;

int x1,x2,x3;