C Constants and Literals

Introduction

In the previous chapter, we learned about C data types. In this chapter, we will learn about constants and literals in C programming. Constants are fixed values that cannot be changed during the execution of a program. Literals are the actual values assigned to variables or constants.

What are Constants?

Constants are variables whose value cannot be changed once assigned. They are used to store data that remains the same throughout the program. Constants improve the readability and maintainability of the code by providing meaningful names for fixed values.

How to Create Constants

In C, you can create constants in two main ways:

  1. Using the const keyword
  2. Using the #define preprocessor directive

1. Using the const Keyword

The const keyword is used to declare a constant variable. Once declared, its value cannot be modified.

Example:

#include <stdio.h>

int main() {
    const int DAYS_IN_WEEK = 7; // Declaring an integer constant using const keyword
    printf("Days in a week: %d\n", DAYS_IN_WEEK); // Printing the constant value

    // DAYS_IN_WEEK = 8; // Uncommenting this line will cause a compilation error

    return 0; // Returning 0 to indicate successful execution
}

Output:

Days in a week: 7

2. Using the #define Preprocessor Directive

The #define directive is used to create symbolic constants. This method does not use memory but replaces the constant value directly in the code during preprocessing.

Example:

#include <stdio.h>

#define PI 3.14159 // Defining a symbolic constant using #define directive

int main() {
    printf("Value of PI: %f\n", PI); // Printing the constant value

    return 0; // Returning 0 to indicate successful execution
}

Output:

Value of PI: 3.141590

Naming Conventions for Constants

To make your code more readable and maintainable, follow these naming conventions for constants:

  • Use uppercase letters for constant names.
  • Separate words with underscores (_).

Examples:

  • MAX_SIZE
  • MIN_VALUE
  • DAYS_IN_WEEK

Types of Constants

  1. Integer Constants
  2. Floating-Point Constants
  3. Character Constants
  4. String Constants
  5. Enumeration Constants

1. Integer Constants

Integer constants are whole numbers without a fractional part. They can be decimal (base 10), octal (base 8), or hexadecimal (base 16).

Example:

#include <stdio.h>

int main() {
    const int DECIMAL_CONSTANT = 100; // Decimal integer constant
    const int OCTAL_CONSTANT = 0144; // Octal integer constant (prefixed with 0)
    const int HEX_CONSTANT = 0x64; // Hexadecimal integer constant (prefixed with 0x)

    printf("Decimal Constant: %d\n", DECIMAL_CONSTANT); // Printing the decimal constant
    printf("Octal Constant: %d\n", OCTAL_CONSTANT); // Printing the octal constant
    printf("Hexadecimal Constant: %d\n", HEX_CONSTANT); // Printing the hexadecimal constant

    return 0; // Returning 0 to indicate successful execution
}

Output:

Decimal Constant: 100
Octal Constant: 100
Hexadecimal Constant: 100

2. Floating-Point Constants

Floating-point constants are numbers with a fractional part. They can be represented in decimal form or exponential form.

Example:

#include <stdio.h>

int main() {
    const float PI = 3.14f; // Floating-point constant
    const double E = 2.718281828459045; // Double precision floating-point constant

    printf("Pi: %.2f\n", PI); // Printing the floating-point constant
    printf("Euler's Number: %.15lf\n", E); // Printing the double precision floating-point constant

    return 0; // Returning 0 to indicate successful execution
}

Output:

Pi: 3.14
Euler's Number: 2.718281828459045

3. Character Constants

Character constants are single characters enclosed in single quotes.

Example:

#include <stdio.h>

int main() {
    const char GRADE = 'A'; // Character constant

    printf("Grade: %c\n", GRADE); // Printing the character constant

    return 0; // Returning 0 to indicate successful execution
}

Output:

Grade: A

4. String Constants

String constants are sequences of characters enclosed in double quotes.

Example:

#include <stdio.h>

int main() {
    const char GREETING[] = "Hello, World!"; // String constant

    printf("Greeting: %s\n", GREETING); // Printing the string constant

    return 0; // Returning 0 to indicate successful execution
}

Output:

Greeting: Hello, World!

5. Enumeration Constants

Enumeration constants are user-defined constants represented by names.

Example:

#include <stdio.h>

enum Weekday { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };

int main() {
    enum Weekday today = WEDNESDAY; // Enumeration constant

    printf("Today is day number %d of the week.\n", today); // Printing the enumeration constant

    return 0; // Returning 0 to indicate successful execution
}

Output:

Today is day number 3 of the week.

What are Literals?

Literals are fixed values that appear directly in the source code. They represent constant values that are assigned to variables or constants. Literals can be of various types: integer literals, floating-point literals, character literals, and string literals.

Examples of Literals

  • Integer Literal:
int age = 30; // Integer literal
  • Floating-Point Literal:
float pi = 3.14; // Floating-point literal
  • Character Literal:
char grade = 'A'; // Character literal
  • String Literal:
char greeting[] = "Hello"; // String literal

Conclusion

Understanding constants and literals is essential for writing clear and maintainable C programs. Constants provide meaningful names for fixed values, enhancing code readability. Literals represent constant values directly in the source code. By using constants and literals effectively, you can create more robust and understandable programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top