Constants in C

The constants are fixed values which cannot be altered during the execution of a program. It provides a way to define a variable which cannot be modified by any other part in the code.

Constants can be defined by placing the keyword const in front of any variable declaration. If the keyword volatile is placed after const, then this allows external routines to modify the variable (such as hardware devices). This also forces the compiler to retrieve the value of the variable each time it is referenced rather than possibly optimizing it in a register.

Syntax
const type constant name;
Types of constant

There are various types of constants in C.

  • Integer Constants
  • Real Constants
  • Character Constants
  • String Constants
  • Backslash Character Constants
Integer Constants

These constants refer to a sequence of digits. Any representation can be used to specify an integer constant. Below table shows ways to specify an integer constant, whose value is 1234 in decimal number system, in different number systems.

RepresentationValue
Decimal1234
Hexadecimal0x4D2
Octal02322
Real Constants

These constants refer to those numbers which contain fractional parts like 12.34. Such numbers are called real or floating point numbers. A real constant can also be expressed in exponential or scientific notation. As an example, 12.34 can also be expressed as 1234E-2. In this form, the real number is expressed in terms of mantissa and exponent. The mantissa is either a real number or an integer and exponent is an integer number. Both can have an optional plus or minus sign. The letter E separating the mantissa and the exponent can be written in lowercase also.

Character Constants

These constants refer to those tokens which contain a single character enclosed within a pair of single quotes like ‘W’. The character may be a letter, number, special character or blank space. As an example, ‘5’ is a character constant while as 5 is an integer constant. Similarly, 5.0 can be treated as real constant.

Character constants are printable characters which are stored in memory as their ASCII (American Standard Code for Information Interchange) code.

String Constants

These constants refer to those tokens which contain a string of characters enclosed within a pair of double quotes. The characters may be letters, numbers, special characters and blank space. Examples include “Hello World!”, “123”, and so on. It should be noted “123” is a string constant while 123 is integer constant.

Backslash Character Constants

These constants refer to those special character constants which contain a backslash and some character enclosed within a pair of single quotes. These constants are used in output functions and are known as escape sequences. Below shows the list of backslash character constants along with their interpretation.

Backslash CharacterMeaning
\aalert (bell) character
\bbackspace
\fform feed
\nnew line
\rcarriage return
\thorizontal tab
\vvertical vab
\\backslash
\?question mark
\'single quote
\"double quote

Below are the examples of various constants.

ConstantExample
Integer Constant10, 20, 30, 450 etc.
Real or Floating-point Constant10.3, 20.2, 450.6 etc.
Octal Constant021, 033, 046 etc.
Hexadecimal Constant0x2a, 0x7b, 0xaa etc.
Character Constant'a', 'b', 'x' etc.
String Constant"c", "c program", "c in rookienerd" etc.
Backslash character constants\a, \n etc.

Two ways to define constant in C

There are two ways to define constant in C programming.

  1. const keyword
  2. #define preprocessor directive
const keyword

The const keyword is used to define constant in C programming.

snippet
const float PI=3.14;

Now, the value of PI variable can't be changed.

snippet
#include  
int main(){  
	const float PI=3.14;  
	printf("The value of PI is: %f",PI);  
	return 0;
}
Output
The value of PI is: 3.140000

If you try to change the the value of PI, it will render compile time error.

snippet
#include  
int main(){  
const float PI=3.14;   
PI=4.5;  
printf("The value of PI is: %f",PI);  
	return 0;
}
Output
Compile Time Error: Cannot modify a const object
#define preprocessor directive

The #define preprocessor is also used to define constant. We will learn about #define preprocessor directive later.

Visit here for: #define preprocessor directive.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +