Constants in C Programming Language

Rumman Ansari   Software Engineer   2023-01-27   20739 Share
☰ Table of Contents

Table of Content:


In C programming, a constant is a value that cannot be modified once it is assigned. Constants are typically defined using the keyword "const" before the data type. For example, the line "const int MAX_SIZE = 100;" defines a constant integer named MAX_SIZE with a value of 100. Unlike variables, constants cannot be changed during the execution of a program. They are used to represent values that will remain the same throughout the program, such as mathematical constants or other fixed values.

C constants can be divided into two major categories:

  1. Primary Constants
  2. Secondary Constants
Datatypes in c programming

Integer Constants

Integer Constants refers to a Sequence of digits which Includes only negative or positive Values and many other things those are as follows

Example

  1. Decimal Integer Constants
  2. Ocatal Integer Constants
  3. Hexadecimal Integer Constants

Constant in C Programming Language

  •   An Integer Constant must have at Least one Digit
  •   it must not have a Decimal value
  •   it could be either positive or Negative
  •   if no sign is Specified then it should be treated as Positive
  •  No Spaces and Commas are allowed in Name 
  • Constant in C Programming Language

    Decimal Integer Constants

    Decimal constant consists of a set of digits 0 through 9, preceded by an optional –or + sign.

     1, 
    3, 7
    , 8, 65, 543676664

    Octal Integer Constants

    An octal integer constant consists of any combimation of digits from the set 0 through 7, with an leading 0.

     038,
     320, 0456,
      0552, 0432 

    Hexadecimal Integer Constants

    An octal integer constant consists of any combimation of digits from the set 0 through F, with an leading 0x or 0X.

     0x4,
     0X456, 0x552
    , 0x32, 0x43 

    Rules for Constructing Integer Constants

    (a) It must not have a decimal point.
    (b) It can be either positive or negative.
    (c) If no sign precedes an integer constant it is assumed to be positive.
    (d) No commas or blanks are allowed within an integer constant.
    (e) The allowable range for integer constants is -32768 to 32767.
    (f) An integer constant must have at least one digit.

    Real Constants

    Real constants are often called Floating Point constants. The real constants could be written in two forms—Fractional form and Exponential form.

    Integer numbers are unable to represent distance, height, temperature, price, and so on. This information is containing fractional parts or real parts like 56.890. Such numbers are called Real or Floating Point Contents.

    Example

    • A Real  Constant must have at Least one Digit
    • it must  have a Decimal value
    • it could be either positive or Negative
    • if no sign is Specified then it should be treated as Positive
    • No Spaces and Commas are allowed in Name

    Like 251, 234.890 etc are Real Constants

    In The Exponential Form of Representation, the Real Constant is Represented in the two Parts The part before appearing e is called mantissa whereas the part following e is called Exponent.

    • In Real Constant, The Mantissa and Exponent Part should be Separated by letter e
    • The Mantissa Part have may have either positive or Negative Sign
    • Default Sign is Positive
    • The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive.
    • Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.

    Example:

    +4.2e-5
    5.1e8
    -0.2e+3
    -7.2e-5
    

    Rules for Constructing Real Constants

    Following rules must be observed while constructing real constants expressed in fractional form:

    (a) Real Constants must have a decimal point.
    (b) A real constant must have at least one digit.
    (c) Real Constants could be either positive or negative.
    (d) Default sign is positive.
    (e) No commas or blanks are allowed within a real constant.

    Single Character Constants

    A Character is Single Alphabet a single digit or a Single Symbol that is enclosed within Single inverted commas.

    1. Character Constant Can hold Single character at a time.
    2. Contains Single Character Closed within a pair of Single Quote Marks
    3. Single Character is smallest Character Data Type in C.
    4. Integer Representation: Character Constant reprent by Unicode
    5. It is Possible to Perform Arithmetic Operations on Character Constants

    Examples of Character Type:

    'a'
    'A'
    '1'
    '4343'
    '#'
    '-'
    '<'
    'X'
    ETC.

    How to Declare Character Variable?

    Way 1: Declaring Single Variable

    char variable_name;

    Way 2: Declaring Multiple Variables

    char var1,var2,var3;

    Way 3: Declaring & Initializing

    char var1 = 'A',var2,var3;


    You May Like