CONSTANTS IN C

Maha


Constants In C

Constants in C are basically defined as a named memory. In constants are values that are fixed and cannot be altered during program execution. 

Constants are used to represent fixed values in a program, such as numerical values, character values, or boolean values. The constants in C programming can be of any data type: a character type, floating type, string, integer, and double type.  

Constants in C using two methods:

  • By using ‘”#define”
  • By using the keyword “const”

Example: #define program

  
  #include<stdio.h>
  //define constant variable
  #define PI 3.15
  int main()
  {
        float area;
        int radius =5;
        area= PI * radius*radius;
        printf("Area = %f\n",area);
  }


Output:


  Area=78.750000


Example:const keyword Program


  #include<stdio.h>

  //using const keyword to PI value
  const float PI=3.15;
  int main()
  {
        float area;
        int radius =5;
        area= PI * radius*radius;
        printf("Area = %f\n",area);
  }


Output:


  Area=78.750000


Syntax:

const data_type variable_name= value;
#define variable_name value

Types of C Constants:

   1. Integer constants
   2. Real or Floating point constants
   3. Octal & Hexadecimal constants
   4. Character constants
   5. String constants
  

 Integer Constants in C:

  • An integer constant must have at least one digit.
  • It must not have a decimal point.
  • It can either be positive or negative.

Example: The common ways to represent integer constants in C Program


  #include <stdio.h>
  int main() {
    // Decimal constant using const keyword
    const int decimalConstant = 23;
    printf("Decimal constant: %d\n", decimalConstant);

    // Octal constant using const keyword
    const int octalConstant = 052;  // 52 in decimal
    printf("Octal constant: %d\n", octalConstant);

    // Hexadecimal constant using const keyword
    const int hexConstant = 0x2A;   // 42 in decimal
    printf("Hexadecimal constant: %d\n", hexConstant);

    // Binary constant using const keyword (C99 and later)
    const int binaryConstant = 0b101010;  // 42 in decimal
    printf("Binary constant: %d\n", binaryConstant);

    // Long integer constant using const keyword
    const long int longConstant = 123456789L;
    printf("Long integer constant: %ld\n", longConstant);

    // Unsigned integer constant using const keyword
    const unsigned int unsignedConstant = 42U;
    printf("Unsigned integer constant: %u\n", unsignedConstant);

    return 0;
  }


Output:


  Decimal constant: 23
  Octal constant: 42
  Hexadecimal constant: 42
  Binary constant: 42
  Long integer constant: 123456789
  Unsigned integer constant: 42

    Real Constants in C:

    1.Floating-Point Values:
    • Real constants in C represent floating-point values, which include float and double types.
    2.Notation:
    • Real constants can be written in decimal, like 3.14 or 2.71828.
    • Exponential notation is also valid, e.g., 2.5e-3 (2.5 times 10 to the power of -3) or 1.23e4 (1.23 times 10 to the power of 4)..

    Example: Decimal Notation & Exponential Notation

        
        #include <stdio.h>
       
        int main() {
          // Decimal Notation with const keyword
          const float constFloatVar = 3.14f;
          const double constDoubleVar = 2.71828;

          printf("Decimal Notation with const keyword:\n");
          printf("Float: %f\n", constFloatVar);
          printf("Double: %lf\n", constDoubleVar);

          // Exponential Notation with const keyword
          const float constScientificFloat = 2.5e-3f;
          const double constScientificDouble = 1.23e4;

          printf("\nExponential Notation with const keyword:\n");
          printf("Scientific Float: %e\n", constScientificFloat);
          printf("Scientific Double: %e\n", constScientificDouble);

          return 0;
        }


      Output:


        Decimal Notation with const keyword:
        Float: 3.140000
        Double: 2.718280

        Exponential Notation with const keyword:
        Scientific Float: 2.500000e-03
        Scientific Double: 1.230000e+04

      Character Constants in C:

      • A character constant is a single character, a single digit or a single special symbol enclosed within single quotes.
      • The maximum length of a character constant is 1 character.

      Example: char constant


        #include <stdio.h>

        int main() {
          const char charVar = 'A';      // Constant character
          const char escapeVar = '\n';   // Constant character using escape sequence
        
          printf("Character Constant: %c\n", charVar);
          printf("Escape Sequence: %cHello\n", escapeVar);

          return 0;
        }


      Output:


        Character Constant: A
        Escape Sequence: 
        Hello

        String Constants in C: 

        These are constants that represent a sequence of characters enclosed within double quotes.

        Example:

        #include <stdio.h>

        int main() {
            // String Constants
            const char greeting[] = "Hello";
            const char sentence[] = "This is a string.";

            // Printing String Constants
            printf("Greeting: %s\n", greeting);
            printf("Sentence: %s\n", sentence);

            return 0;
        }


        Output:


          Greeting: Hello
          Sentence: This is a string.






        More topic in C

        Tags
        Our website uses cookies to enhance your experience. Learn More
        Accept !

        GocourseAI

        close
        send