constants in c programming
Constants: -
- A constant is a value that cannot be changed during the execution of the program.
- It is fixed, and once defined, its value cannot be modified by the program.
- A constant can be a number, a character, or a string.
- Constants are used to store values that must stay the same throughout the program.
- We can use constants for values like integers, floating-point numbers, characters, or enumeration values.
- A constant can belong to any data type, like int, float, or char.
Syntax:
const data_type variable_name; (or) const
data_type *variable_name;
Types of C constants: C constants are mainly classified into two categories:
1. Primary Constants
2. Symbolic Constants
1) Primary Constants: - The primary constants are:
a. Integer constants.
b. Real or Floating-point constants.
c. Character constants.
d. String constants.
e. Escape sequence constants.
a) Integer constants: -
- The integer constants are whole numbers without any decimal point.
- An integer constant must have at least one digit.
- It can be positive or negative.
- An integer can be a decimal, octal, or hexadecimal constant.
- A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
- An integer can also have a suffix that is a combination of 'U' for unsigned and 'L' for long.
- No commas or blanks are allowed within an integer constant.
Examples: 100, -45, 0
There are three types:
Decimal (Base 10): e.g., 123
Octal (Base 8): Starts with 0, e.g., 076
Hexadecimal (Base 16): Starts with 0x, e.g.,
0x7F
b) Real or Floating-point constants: -
- These are numbers with decimal points or in exponential form.
- A real constant must have at least one digit.
- It must have a decimal point.
- A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part.
Examples: 3.14, -0.001, 2.5e3 (which means
2.5 × 10³ = 2500)
Note: E-5 = 10-5
c) Character constants: -
- A character constant represents a single character enclosed in single quotes.
- The maximum length of a character constant is 1 character.
Examples: 'A', '1', '%'
d) String constants: -
- A string constant is a sequence of characters enclosed in double quotes.
- Unlimited length can allow any number of characters.
Examples: "Hello", "123",
"C Language"
e) Escape sequence constants: -
- There are some characters which have special meaning iin the Clanguage.
- They should be preceded by a backslash symbol to make use of their special function.
- The list of special characters and their purpose.
Backslash_character |
Meaning |
\b |
Backspace |
\f |
Form feed |
\n |
New line |
\r |
Carriage return |
\t |
Horizontal tab |
\” |
Double quote |
\’ |
Single quote |
\\ |
Backslash |
\v |
Vertical tab |
\a |
Alert or bell |
\? |
Question mark |
\N |
Octal constant (N is an octal constant) |
\XN |
Hexadecimal constant (N – hex.dcml cnst) |
2) Symbolic Constants: -
- Symbolic constants are constants that are named using #define or const keyword.
using #define :
#define PI 3.14159, Here, PI is a symbolic constant, and its value cannot be changed.
Examples
#define
LENGTH 10
#define WIDTH
5
#define NEWLINE '\n'
const int MAX = 100; Here, MAX is a constant integer that holds the
value 100.
Examples
const
int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';