Constants in c programming

Constants are quite similar to variables as they are also data storage locations used by C program. But the values stored in constants are fixed and the program cannot alter it during the execution of the progam itself. Such fixed values are also called literals.

There are two types of constants in C programming.

1. Literal Constants

Literal constants are actual values that are assigned to symbolic constants or variables. They can be any data type like integer constant, floating constant, character constant or a string literal.

Example :

int a = 5;
char name= ‘W’;

Here, ‘20’ and ‘W’ are literal constants.

2. Symbolic Constants

Symbolic constants are constants which are represented by name or symbol in a C program. To define a symbolic constant, #define is used.

Example : 

#define LENGTH 5
#define WIDTH 5
#define HEIGHT 2

VOLUME = LENGTH * WIDTH * HEIGHT;

Alternatively, const keyword can also be used to define a symbolic constant.

const int LENGTH = 5;
const int WIDTH = 5;
const int HEIGHT = 2;
VOLUME = LENGTH * WIDTH * HEIGHT;

#include <stdio.h>
#define LENGTH 5
#define WIDTH 5
#define HEIGHT 2
int main() {
  int VOLUME;

  VOLUME = LENGTH * WIDTH * HEIGHT;
  printf("Volume of the rectangle is : %d\n", VOLUME);
}

i. Integer Literals

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base of the integer liter like 0x for hexadecimal, 0 for octal, and nothing for decimal. It can also have a suffix like u or l for unsigned or long integer literal, respectively. The prefix and suffix can be uppercase or lowercase and can be in any order. Such prefix and suffix are not allowed to be repeated in a single literal though.

Decimal Constant

Decimal constants are the constants where any digit from 0 to 9 is allowed. The constant must not start with a 0 though.

Example : 

81, 723 /* Valid decimal constant */
081 /* Invalid decimal constant */

Octal Constant

Octal constants are the constants where any digit from 0 to 7 is allowed. The constant must start with a 0 though.

Example :

061, 0103 /* Valid octal constant */
018, 103 /* Invalid octal constant */

Hexadecimal Constant

Hexadecimal constants are the constants where any digit from 0 to 9 along with characters A, B, C, D E and F for 10, 11, 12, 13, 14 and 15 is allowed. The constant must start with prefix 0x though.

Example :

0x61, 0x1EF /* Valid hexadecimal constant */
0xx18, ABC /* Invalid hexadecimal constant */

Some more examples of integer literals are given below.

71         /* decimal */
0517       /* octal */
0x3F       /* hexadecimal */
3          /* int */
14u        /* unsigned int */
193l       /* long */
391ul       /* unsigned long */

ii. Floating-point Literals

A floating-point literal has an integer part, a decimal point, a fractional part and an exponent part. Floating point literals can be represented in decimal as well as exponential form.

Floating point literals in decimal form must include the decimal point or the exponent or both while the exponential form must include the integer part or the fractional part or both. The signed exponent is introduced by e.

Here are some examples of floating-point literals.

3.14159       /* Valid */
314159E-5L    /* Valid */
510E          /* Invalid, incomplete exponent */
210F          /* Invalid, no decimal or exponent */
.E55          /* Invalid, missing integer or fraction */

iii. Character Literals

Character literals are characters enclosed in single quotes which can be stored in a simple variable of char type. It can be a plain character or an escape sequence or a universal character. 

Example :
char a = 'w';
char b = '/h';
char c = '/u02C0';

Some characters are reserved in C and have some special meaning when preceded by a back slash. 

Reserved Escape Sequence Characters Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh... Hexadecimal number of one or more digits

Example : 

#include <stdio.h>

int main() {
  printf("Let\'s learn C programming. \n");
}

iv. String Literals

String literals are characters enclosed in double quotes. A string constant can be plain characters, escape sequences or universal characters. Long texts can be broken down into multiple lines using string literals and separating them using backslashes. String literals are appended with the null character ‘\0’ which indicates the end of the string.

Example :

#include <stdio.h>

int main() {
    char a[] = "Hello \
    World";
    printf("%s\n",a);
    printf("Total number of characters in a is %d\n",strlen(a));
}

0 Like 0 Dislike 0 Comment Share

Leave a comment