ADVERTISEMENT
ADVERTISEMENT

Constants in C with Examples

In C programming, a constant is a value that cannot be modified once it is set. Constants are also known as literals. Constants can be of any data type, such as an integer, floating point, character, or string.

There are two types of constants in C:

Primary constants

Primary constants are constants that are represented directly by their value. They are further divided into four categories:

  • Integer constants
  • Real or Float constants
  • Character constants
  • String constants
  1. Integer Constants: Integer constants are whole numbers that can be positive, negative, or zero. They can be written in decimal (base 10), octal (base 8), or hexadecimal (base 16). Decimal integer constants have no prefix. Octal integer constants are prefixed with a leading zero (0), and hexadecimal integer constants are prefixed with "0x" or "0X". For Example -57,458,-658 .

  2. Real or Float Constants: Real constants, also known as floating point constants, represent real numbers. They can be written in two ways: in decimal form, or in exponential form. In decimal form, a real constant consists of an integer part, a decimal point, a fractional part, and an exponent part. In exponential form, a real constant consists of a mantissa and an exponent. The mantissa can be an integer or a real number, and the exponent is an integer preceded by the letter "e" or "E". For Example 25.86,2.36

  3. Character Constants: Character constants are single characters enclosed in single quotes. They can be letters, digits, special characters, or escape sequences. Escape sequences are special characters that are represented by a combination of two or more characters and begin with a backslash (). For example, the newline character is represented by the escape sequence " ". For Example 'A','6' 

  4. String Constants: String constants are sequences of characters enclosed in double quotes. They can contain letters, digits, special characters, and escape sequences. String constants are used to represent text. For Example "Tech","Programming"

Secondary Constants

In addition to constants, C also has several composite data types, which allow you to store multiple values of different types in a single data structure. These data types include arrays, structures, unions, and pointers.

  1. Arrays: An array is a collection of variables of the same data type, stored in contiguous memory locations and accessed by a common name. Arrays are indexed by a set of integers, and the elements of an array can be accessed by their index. For example:
    int numbers[5];
    

    This declares an array of 5 integers called "numbers". The elements of the array can be accessed using the syntax "numbers[index]", where "index" is the zero-based index of the element.

  2. Structures: A structure is a user-defined data type that consists of a collection of variables of different data types. Structures are used to represent a record, such as a student record or an employee record. Structures are defined using the "struct" keyword, and the variables inside a structure are called members. For example:
    struct student {
      char name[50];
      int roll_number;
      float marks;
    };
    

    This declares a structure called "student" with three members: a character array called "name", an integer called "roll_number", and a float called "marks".

  3. Unions: A union is a data type that allows you to store different data types in the same memory location. A union takes up as much memory as its largest member. Unions are defined using the "union" keyword, and the variables inside a union are called members. For example:
    union data {
      int i;
      float f;
      char str[20];
    };
    

    This declares a union called "data" with three members: an integer called "i", a float called "f", and a character array called "str".

  4. Pointers: A pointer is a variable that stores the memory address of another variable. Pointers are used to manipulate variables indirectly, and they are an important feature of C. Pointers are defined using the "*" operator, and the memory address of a variable can be obtained using the "&" operator. For example:
    int x = 10;
    int *p;
    p = &x;
    

     

Rules of Constructing Constants in C

Here is how we construct these constants in a given program:

Rules for Declaring Integer Constants

  • It must have at least one digit.
  • There must be no decimal point.
  • It does not allow any blanks or commas.
  • An integer constant can be both negative or positive.
  • We assume an integer constant to be positive if there is no sign in front of that constant.
  • The allowable range for this type of constant is from -32768 to 32767.
  • Valid Integer Constants : 254,56,468,-33
  • Invalid Integer Constant: 2.5 (Decimal Not Allowed)

Rules for Declaring Real/Float Constants

  • This type of constant must consist of one digit at least.
  • There must not be any decimal point.
  • This type of constant can be both negative or positive.
  • It does not allow any blanks or commas within.
  • We assume an integer constant to be positive if there is no sign in front of that constant.
  • Valid Real Constants : 2.54,5.6

Rules for Declaring String and Character Constants

  • This type of constant can be a single digit, a single alphabet, or even a single special symbol that stays enclosed within single quotes.
  • The string constants get enclosed within double quotes.
  • The maximum length of this type of constant is a single character.
  • Valid Character Constant: 'A','5'

ADVERTISEMENT

ADVERTISEMENT