What is Constant in C Programming Language

What is Constant in C Programming Language

The Constant in C Programming Language 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.

You must test the C Language code provided in this lesson in your code editor to validate it. However, if you prefer to execute this code online, we also provide a free C Online Compiler for testing your C Language code.

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 Constants in C

Acceleration Formula with Velocity ...
Acceleration Formula with Velocity and Time

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. Primary constants include things like character constants, real constants, integer constants, etc. Secondary constant types include structures, arrays, pointers, unions, etc.

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 “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 Constant in C

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.

An unsigned type or long type can also be utilized for the integer constant in a program. The long integer constant value is prefixed with “l”, whereas the unsigned constant value is prefixed with “u”. Additionally, we add the suffix “ul” to the unsigned long integer constant value.

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 in 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.

An integer portion, a decimal point, a fractional part, and an exponent part make up a floating-point literal. Either decimal form or exponential form can be used to express floating point literals.

The decimal point, the exponent, or both must be included when representing numbers in decimal form, and the integer portion, the fractional part, or both must be included when representing numbers in exponential form. Either e or E introduces the signed exponent.

Rules of Building Real Constant in 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.

The example to demonstrate a few escape sequence characters is provided below.

#include <stdio.h>
int main() {
   printf("Welcome\tItsourcecode.com\n\n");
   return 0;
}

The result of compiling and running the aforementioned code is as follows.

Welcome Itsourcecode.com

You can try to test the code here!

Other Example of Character Constant

  • ‘B’
  • ‘5’
  • ‘+’

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, and each of these sequences is prefixed with the symbol “/“. These escape sequences are used in output methods called “printf().”

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

Creation and Use of Constants in C

Two of the following notions can be used to construct constants in the C programming language:

  • By using the ‘#define’ preprocessor
  • By using the ‘const’ keyword.

Use of the ‘const’ Keyword

Any given datatype can be made into a constant in a program by using the “const” keyword. The keyword “const” must come before the variable declaration in order to create a constant. The general syntax for utilizing the keyword “const” is as follows:

const datatype constantName = value ;
Of
const datatype constantName ;

Let’s examine an example to understand this concept better.

In this instance, x is a constant integer with the fixed value of 20.

const int  LENGTH = 45;
const int  WIDTH = 25;

The program will show as follows:

#include <stdio.h>
int main() {
   const int  LENGTH = 45;
   const int  WIDTH = 25;
   const char NEWLINE = '\n';
   int area;  
   
   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);
   return 0;
}

The following is the outcome of compiling and running the above code:

value of area : 1125

You can try to test the code here!

Use of the ‘#define’ preprocessor

The constants can also be made using the ‘#define’ preprocessor command. Additionally, the preprocessor directive must be defined at the very beginning of the program when creating constants using it. This is due to the requirement that we write all preprocessor directives prior to the global declaration.

The syntax required to define a constant using the ‘#definepreprocessor directive is as follows:

#define CONSTANTNAME value
#include <stdio.h>
#define LENGTH 100
#define WIDTH  25
#define NEWLINE '\n'
int main() {
   int area;  
  
   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);
   return 0;
}

The following is the outcome of compiling and running the above code:

value of area : 2500

You can try to test the code here!

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.

Try to run the C Programming code for free through online

Click the link below!
C Online Compiler

Conclusion

  • Constant variables are those whose values remain constant during the course of the program.
  • 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.

Leave a Comment