Constant in C Programming

Constant is an entity which never changes in the given problem or program.

Types of Constant

There are basically two types of constants in C.

  1. Scalar/ Primitive/ Primary
  2. Composite

The Scalar types of constant are those which can't be divided any more and the composite constant are those which can be divided into multiple scalar constants.

In this article, we only discuss the Scalar/Primitive constant.

The Composite constants are discussed later.

1. Integer Constant

  1. A number without any fractional part is called Integer constant.
  2. Example: 25, 0, -44, 456 etc

Rules of Integer Constant

  1. No decimal points are allowed. Example 25 is correct but 25.0 is incorrect.
  2. Rules of Integer Constant +ve/-ve both types of values are allowed.
  3. Special, Comma and other characters are not allowed. Example 2 3 4 and 12,500 are incorrect.
  4. Valid Range: -32768 to 32767

2. Floating point constant or real constant

  1. Can contain decimal point +ve/-ve both types of values are allowed.
  2. Special, Comma and other characters are not allowed. Example 5.3,200 or 5 .4 3 are incorrect.
  3. The real constant can be specified in two ways 
    1. Fractional Form: Example 437.86 
    2. Exponential Form: Example 4.3786E2 or 4.3786e2.
    3. 4.3786E2 is equal to 4.3786 * 102.
    4. Valid Range: 3.4E-38 to 3.4e38 Exponential format have two parts.
    5. The number before 'e' is called mantissa and after 'e' is called exponent.

3. Character Constant

  1. Example: 'm', '@', '^', '9' etc.
  2. Must be enclosed in single quotes.
  3. Character constant must be a single character.
  4. 'Jaipur' is an invalid character constant.
  5. Character constant are encoded by ASCII encoding.

4. String Constant

A string is generally called a group of characters.
  1. Example: "abc","Jaipur","abc@example.com","ACX-124-EG-098"
  2. String constant must be enclosed in double quotes.
Previous Back to C Programming index Next

Comments

Popular posts from this blog

String in golang

Inline V/S Block Level Element

Floating point Data types in Go lang

Escape Sequence | Formatted Printing | Interview Questions

Sum of two numbers

Operators in C Language| Part-4

Printing in C programming

Arrays in C Language

Program to check a year is leap year or not.

Data types in Java