Constant in C Language with Program Example

The Constants in C Programming Language is a variable whose values cannot be updated or altered.

A constant can only hold one variable at a time while a program is running, unlike variables in the C programming language.

This means that once a constant has a value assigned to it, it cannot be changed during the course of a program’s execution; instead, it remains fixed.

What is Constant in C

Constants in C are fixed values that cannot be changed by the program while it is running. Literals are another name for these fixed values.

Any of the fundamental data types, such as an integer constant, a floating-point constant, a character constant, or a string literal, can be used as constants.

Enumeration constants are also present. The only difference between constants and ordinary variables is that once a constant’s value has been defined, it cannot be changed.

Use of the constant C

A constant in a program is essentially a named memory region that maintains a single value throughout the program’s execution.

Any data type, including character, floating-point, string, double, integer, etc., may be used. Constants come in many different varieties in C.

Primary and secondary constants are its two main categories.

What are Literals in C?

Literals are the values we provide to variables that hold true throughout the course of a program’s execution.

In general, the phrases “literal” and “C constants” can be used interchangeably.

As an illustration, the statement “const int = 7;” is an example of a constant expression, whereas the number 7 is referred to as a constant integer literal.

Different Types of C constant

Type of ConstantsData typeExample of Data type
Integer constantsint23, 738, -1278, etc.
unsigned int2000u, 5000U, etc.
long int, long long int325,647 1,245,473,940
Floating-point or Real constantsdoule500.987654321
float20.987654
Octal constantintExample: 013 /*starts with 0 */
Hexadecimal constantintExample: 0x90 /*starts with 0x*/
character constantscharExample: ‘X’, ‘Y’, ‘Z’
string constantscharExample: “PQRS”, “ABCD”

More Information about C Constant Types:

Integer Constants

Integer Constants It could be an integer in octal, decimal, or even hexadecimal form.

While we prefix the octal integer values with “o”, we specify a decimal integer value as a direct integer value.

We also use the prefix “0x” before the hexadecimal integer values.

Example of Integer Constants

66 —> Decimal Integer Constant
0x3B —> Hexa Decimal Integer Constant
O27 —> Octal Integer Constant
69ul —> Unsigned Long Integer Constant
90l —> Long Integer Constant
20u —> Unsigned Integer Constant

Rules of Building Integer Constant C

  • It must contain a minimum of one digit.

  • No decimal point is allowed.

  • No spaces or commas are permitted.

  • Both negative and positive integer constants are possible.

  • If there is no sign in front of an integer constant, we take it to be positive.

  • This kind of constant can have values between -32768 and 32767.

Floating Point Constants or Real Constants

Floating Point Constants or Real Constants – Both decimal and integer components must be included in this kind of constant.

The exponential component may occasionally be included in the floating-point constant.

When a floating-point constant is represented in an exponential form, the suffix ‘E’ or ‘e’ must be added to its value.

Rules of Building Real Constants C

  • This kind of constant needs to have at least one digit.

  • No decimal point is permitted.

  • Both negative and positive constants fall under this category.

  • There may not be any spaces or commas inside.

  • If there is no sign in front of an integer constant, we take it to be positive.

Following are a few examples of floating-point literals.

floating-point constantValue
5.3876e453,876
4e-110.00000000004
1e+5100000
7.321E-30.007321
3.2E+432000
0.5e-60.0000005
0.450.45
6.e1060000000000
3E-143.14

Character Constants

Character constants. is a Symbols that are encased in a single quotation mark. A character quotation can only be one character long at most.

Character literals are denoted by single quotes, such as “x“, which can be stored in a straightforward char-type variable.

An escape sequence (such as “t”), a universal character (such as “u02C0“), or a simple character (such as “x“) can all be used as character literals.

Some C characters, like newline (n) and tab, have specific meanings when they are preceded by backslashes (\t).

Rules of Building String and Character Constants

  • A single digit, letter, or even a single unique symbol that remains encased in single quotations can serve as this kind of constant.

  • Double quotes are used to encapsulate the string constants.

  • This kind of constant can only be one character long at most.

In the C programming language, there are a few predefined character constants called escape sequences. Each escape sequence has a unique set of distinctive features.

Each of these sequences is prefixed with the symbol “/“. These escape sequences are used in output methods called “print().”

String Constants

The special symbols, numerals, letters, and escape sequences that make up the string constants are encapsulated in double quotation marks.

One line contains the definition of a string constant:

“This is Itsourcecode”

Constant many lines can also be used to define this:

” This\

is\

Itsourcecode”

White spaces may also be used in the definition of the same string constant:

“This” is “Itsourcecode”

The same string constant is defined by all three of the aforementioned definitions.

Rules of Building Backslash Character Constants

  • In the C language, these are some character types that have unique meanings.

  • For the program to be able to use the special function in these constants, a backslash sign must come before them.

  • The purpose of each special character in the C language is listed below:
Meaning of CharacterBackslash Character
Backspace\b
New line\n
Form feed\f
Horizontal tab\t
Carriage return\r
Single quote\’
Double quote\”
Vertical tab\v
Backslash\\
Question mark\?
Alert or bell\a
Hexadecimal constant (Here, N – hex.dcml cnst)\XN
Octal constant (Here, N is an octal constant)\N

Conclusion

  • When programs employing constants are required, constants are employed.

  • Primary constants and Secondary constants are terms used to describe constants for primary and secondary variables, respectively.

  • Using the const keyword or the #define preprocessor, a variable can be made a constant.

In the next following post, I’ll create a storage classes in C and try to explain its many components in full details.

I hope you enjoy this post on Constant in C, in which I attempt to illustrate the language’s core grammar.


Leave a Comment