Types of constant declaration in programming languages.

Kaushal mandal
2 min readNov 14, 2021

There are mainly 3 ways how a constant can be declared in programming languages.

1. Using const keyword

Using const keyword before declaring a variable is a way of declaring a constant.

ex : const int a = 5 ;

2. Using #define macro

Using macro is another way to declare constant.

#define KIRO_ PI 3.14

Rules for defining constant with macro

  • Name of macro conventionally should be written in capital letters.
  • So while declaring macro no need of = operator.
  • While declaring macro no need to specify data type.
  • Macro is written always above main in global scope.
  • While declaring macro or preprocessor directive semi colon is not used.
  • Macro is just a place holder when we use it in our code it just gets substituted over there.

3. Using enum

Enum is another way to declare constant.

Enum gives back to back some numbers or takes some number, its full form is enumerations.

Rules for declaring enum

  1. Conventionally enum is written above main
  2. Enum by default gives values to the identifier from first to last and by default it starts from 0.
  3. So if we don’t want the default value to be zero than while declaring enum assign the value to identifier.
  4. If we change the value in between than the initial value would strat from zero but when it comes at the identifier where we changed the value it goes to the next identifier by +1.

HOPE I WAS ABLE TO HELP THANK YOU FOR READING AND FOLLOW FOR MORE

--

--