In C programming, constants and literals refer to values that cannot be changed during the execution of a program. Constants are declared using specific keywords or preprocessor directives, while literals are fixed values assigned directly to variables. These concepts help improve code readability and prevent accidental changes to critical values.

1. Constants in C

A constant is a variable whose value remains the same throughout the program. Constants can be defined using:

  1. The const keyword.
  2. The #define preprocessor directive.

Types of Constants:

  • Integer Constants
  • Floating-Point Constants
  • Character Constants
  • String Constants
  • Enumeration Constants

1.1 Defining Constants with the const Keyword

The const keyword is used to declare a constant variable whose value cannot change after initialization.

Syntax:

const data_type variable_name = value;

Example:

const int max_students = 50;  // Constant integer (SamagraCS Educational Technology)

const char founder_initial = ‘P’;  // Character constant (Pawan Jaiswal)

1.2 Defining Constants with the #define Preprocessor Directive

The #define directive allows you to define constant values before compilation. It creates a macro that replaces occurrences of the constant name with its value throughout the program.

Syntax:

#define constant_name value

Example:

#define PI 3.14159

#define COMPANY_NAME “SamagraCS Educational Technology”  // String constant

  • Note: The #define directive does not enforce type checking, unlike const.

2. Literals in C

A literal is a fixed value directly written into the program code. Literals can represent various data types such as integers, floating-point numbers, characters, and strings.

Types of Literals:

  1. Integer Literals
  2. Floating-Point Literals
  3. Character Literals
  4. String Literals

2.1 Integer Literals

Integer literals represent whole numbers. They can be written in:

  • Decimal: Base 10 (e.g., 100).
  • Octal: Base 8, prefixed with 0 (e.g., 010).
  • Hexadecimal: Base 16, prefixed with 0x or 0X (e.g., 0x64).

Example:

int num_students = 25;  // Decimal literal

int octal_num = 012;    // Octal literal (10 in decimal)

int hex_num = 0x19;     // Hexadecimal literal (25 in decimal)

2.2 Floating-Point Literals

Floating-point literals represent real numbers with decimal points or in exponential form.

Example:

float pi_value = 3.14159;   // Floating-point literal

double big_number = 1.2e5;  // Exponential notation (1.2 * 10^5 = 120000)

2.3 Character Literals

A character literal represents a single character enclosed in single quotes. It is stored as an integer value corresponding to its ASCII code.

Example:

char founder_initial = ‘P’;  // Character literal (Pawan Jaiswal)

char new_line = ‘\n’;        // Escape sequence for a new line

2.4 String Literals

A string literal is a sequence of characters enclosed in double quotes. In C, a string literal is automatically terminated by a null character (\0).

Example:

char company_name[] = “SamagraCS Educational Technology”;  // String literal

char founder_name[] = “Pawan Jaiswal”;  // String literal (Founder)

3. Difference Between Constants and Literals

  • Constants: These are declared variables that cannot be modified during program execution. They can be defined using const or #define.
    • Example: const int max_capacity = 100;
  • Literals: These are fixed values directly written into the source code.
    • Example: int num_students = 50; (Here, 50 is a literal.)

Example:

#include <stdio.h>

#define MAX_SIZE 100  // Macro constant

#define COMPANY “SamagraCS Educational Technology”  // String constant

const float PI = 3.14159;  // Constant using const

int main() {

    const int max_students = 50;  // Integer constant (SamagraCS Educational Technology)

    const char founder_initial = ‘P’;  // Character constant (Pawan Jaiswal)

    printf(“Company: %s\n”, COMPANY);  // Using string constant

    printf(“Max Size: %d\n”, MAX_SIZE);  // Using macro constant

    printf(“PI Value: %.5f\n”, PI);  // Using float constant

    printf(“Max Students: %d\n”, max_students);  // Integer constant

    printf(“Founder Initial: %c\n”, founder_initial);  // Character constant

    return 0;

}

Output:

Company: SamagraCS Educational Technology

Max Size: 100

PI Value: 3.14159

Max Students: 50

Founder Initial: P

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?