list of contents

  1. What is constant in C with example?
  2. Define a Constant
  3. Different types of Constant in C
    1. Numerical Constants
      1. Integer constants
        1. Decimal Integers
        2. Octal Integers
        3. Hexadecimal Integers
      2. Real Constant
      3. Exponential Constants
    2. Character Constant
      1. Single character constants
      2. String Constants

What is constant in C with example?

In C programming language, constants are the fixed values that are provided to the system while the designing of program. The value of a constant cannot be change while the execution of the program. The values are stored in identifiers with different name and at different places. The type of the constant can be mentioned using keywords before initializing the constants.

Constants can be defined as the fixed values that are either defined by the user as an output during the execution of the program or defined at the time of declaration to a particular identifier. There are some universal constants that cannot be change by the user. In C programming language, constants are subdivided into many forms. The two specific form of constants that contains the other subdivision forms are Numeric constant and Character constant.

Define a Constant

Constant in C programming language can be defined by two ways:

  1. A variable is directly declared with the value of our constant at the start of the main function.
  1. We can also use #define directive, before the start of the main function. This statement does not end with a semicolon.

Different types of Constant in C

Constants in C

The constants in C programming language can be of 2 main types, Numerical constant and Character constant.

Numerical Constants

Numerical constants are the constant which contains all the numerical values such as real numbers, Integers, decimal, octal, hexadecimal, fractional or floating point values. The numerical constants is also sub-divided into 2 types, they are,

  1. Integer constants
  2. Real Constants

Integer constants

Integer constants are the constant that contains a series or sequence of digits. They do not contain any floating point value or fractional values. The sequence is continued and there is no other character is present between the digits of the constant. The Integer constant is further divided into 3 types, such as,

  1. Decimal integers
  2. Octal Integers
  3. Hexadecimal Integers
Decimal Integers

Decimal Integers contains all the digits from 0 to 9, which can be of positive form (0 to 9)or can be of negative form (0 to -9). The sequence of decimal integer constant does not permit the spaces, commas and any other character or symbol between the digits.

Example:

Some valid examples of Decimal integers:

  1. 1234
  2. -91
  3. 765418

Some Invalid examples of Decimal integers:

  1. 53 876
  2. 54.76
  3. 54,768
Octal Integers

Octal integer is a sequence of digits which contains digits from 0 to 7, with a prefix “0”, at the beginning. The octal integer does not permit any value without the prefix “0”. It also does not allow any digit above 7 and below 0 in the sequence.

Example:

Some valid examples of octal integers:

  1. 0345
  2. 0567
  3. 0777

Some invalid examples of octal integers:

  1. 7676
  2. O897
  3. 1234
Hexadecimal Integers

Hexadecimal integer consist digits from 0 to 15, where 10 to 15 represented by A to F respectively. The sequence is started with a 0, which is followed by x or X. the following table shows the alphabets that are used in place of digits from 10 to 15.

Digit101112131415
AlphabetABCDEF
Digits represented in Hexadecimal form

Example:

  1. 0x3c34
  2. 0X25
  3. 0XACD

Real Constant

Real constant contains the values which have a factional part or a floating point. This is also called as floating point constants. These constants are represented by values like 54.876. This constants can be of positive form (+ sign) or negative form (- sign).

Examples;

  1. 0.078
  2. -1.1456
  3. +765.089

Exponential Constants

Real constants can also be represented as a exponential constants. The exponential constant starts with notation E or e, followed by an exponent. The exponent is an integer value with an option plus (+) or minus (-) sign.

Examples:

  1. 123 e 6
  2. 54.43 E 8
  3. 354 e-5

Character Constant

The character constants are the constants that contain the character or string values. They may contain digits, alphabets, special symbols or the string of all three. They are classified into two forms such as, Single character constant and String Constants.

Single character constants

The single character constant contains a single character that can be an alphabet, digit or special symbol. These constants are mentioned in a pair of single quotation mark.

Example:

  1. ‘A’
  2. ‘7’
  3. ‘!’
  4. ‘x’

String Constants

The string constant contains a sequence of character, followed by enclosed in double inverted commas. The characters can be a digit, alphabet or special symbol.

Example:

  1. “HelloWorld!”
  2. “ABC!1234”

In C programming language, String is closed by ‘\0’, which is a null character that is automatically added to the end of the string.

Some of the other character constants are:

\0: Null character constant

\b: backspace character constant

\r: carriage-return character constant

\ t: horizontal tab character constant

\n: new line character constant

Here, we have completed with the whole concept of Constants and its types. Hope it will help you for better understanding purpose. If you have any queries, please feel free to contact us through comment section or through contact us forum.