Difference Between Constant And Literals In C

In the realm of C programming, a constant denotes a fixed value that persists unchanged throughout the program's runtime. C encompasses various categories of constants, such as integers, floating-point values, characters, and strings. These constants act as stand-ins for unchanging values, allowing developers to leverage them without the need for repetitive manual declaration.

Types of Constants in C

  • Integer constants:

Integer literals denote actual numbers and have the ability to be either positive or negative. Illustrations include 10, -10, 0, and so forth.

  • Real number constants:

Character constants are used to represent individual characters and are enclosed in single quotation marks. Examples include 'a', '5', etc.

Character constants denote individual characters enclosed within single quotation marks. Instances include 'a', 'b', and so on.

In the C programming language, there are no dedicated string constants. Nevertheless, strings in C are commonly denoted using string literals, which are a series of characters enclosed within double quotation marks.

Features of Constants

There are several features of Constants in C. Some main features of the Constants are as follows:

  • Once a constant is defined and assigned a value, its value cannot be changed during the program execution. These are immutable.
  • Constants are often declared using the "const" keyword to indicate that their values should not be modified.
  • Constants are type-specific. They are associated with a particular data type, such as int, float, or char. It helps in maintaining data integrity.
  • Constants may have internal linkage or external linkage.
  • Constants must be initialized at the time of declaration. Once declared, their value cannot be changed.
  • Constants are typically given meaningful names using uppercase letters and underscores to enhance code readability. It helps convey the purpose or significance of the constant.
  • Constants are evaluated at compile time. It leads to optimized code execution.
  • Constants are often declared in header files, allowing them to be shared across multiple source files and ensuring consistency in their usage.
  • Example:

Let's consider a C program to demonstrate the various categories of constants:

Example

#include <stdio.h>

int main()

{

 // Integer Literals

 int integerLiteral1 = 5;

 int integerLiteral2 = -10;

 // Floating-point Literals

 float floatLiteral1 = 3.14;

 float floatLiteral2 = -0.5;

 // Character Literals

 char charLiteral1 = 'A';

 char charLiteral2 = '1';

 // String Literal

 const char *stringLiteral = "Hello, World!";

 // Displaying Integer Literals

 printf("Integer Literals:\n");

 printf("Literal 1: %d\n", integerLiteral1);

 printf("Literal 2: %d\n\n", integerLiteral2);

 // Displaying Floating-point Literals

 printf("Floating-point Literals:\n");

 printf("Literal 1: %f\n", floatLiteral1);

 printf("Literal 2: %f\n\n", floatLiteral2);

 // Displaying Character Literals

 printf("Character Literals:\n");

 printf("Literal 1: %c\n", charLiteral1);

 printf("Literal 2: %c\n\n", charLiteral2);

 // Displaying String Literal

 printf("String Literal:\n");

 printf("%s\n", stringLiteral);

 return 0;

}

Output:

Output

Integer Literals:
Literal 1: [value]
Literal 2: [value]

Floating-point Literals:
Literal 1: [value]
Literal 2: [value]

Character Literals:
Literal 1: %c
Literal 2: %c

String Literal:
[value]

Literals in C

Literals serve as the concrete representations of constants within the source code. They function as the direct symbols employed to specify fixed values. Essentially, literals are the means by which developers articulate constants in their programming scripts.

Types of Literals in C:

There exist various categories of literals in the C programming language. One of the primary kinds of literals is:

  • Integer literals:

Integer constants depict complete numbers and may denote positivity or negativity. Instances encompass 7, -12, 0, 072, 0x2F, and so forth. They also cover octal integers with an 'o' prefix and hexadecimal whole number constants.

  • Real number literals:

Floating point literals are used to denote real numbers, containing decimal points to represent fractions or decimal numbers. Instances include values like 3.14, 0.5, 0.25, and so forth.

  • Character literals are:

The symbol 'A' denotes the character value 'A', while the digit '1' signifies the character constant '1'.

  • String Constants:

Strings are commonly denoted using string literals, which consist of a series of characters enclosed within double quotes. Instances of this include "France", "Python Programming", and more.

Features of Literals:

There are several features of Literals in C. Some main features of the Literals are as follows:

  • Unlike constants, literals are not inherently immutable. They can be assigned to variables and modified if not declared as constants explicitly.
  • Literals can be used directly in expressions and calculations, similar to constants.
  • Literals often initialize variables or as direct values in expressions without separating declarations.
  • String literals in C are treated as arrays of characters, and their representation includes a null-terminating character ('\0') at the end.
  • Like constants, literals can also be used in preprocessor directives, but they are not explicitly marked with the "const" keyword
  • Literals do not have linkage as they are part of the source code and do not exist as separate entities in memory.
  • Literals are evaluated at compile time, and their values are often substituted directly into the compiled code.
  • Example:

Let us take a program to illustrate the literals in C:

Example

#include <stdio.h>

int main()

{

 // Integer Literals

 int integerLiteral1 = 5;

 int integerLiteral2 = -10;

 // Floating-point Literals

 float floatLiteral1 = 3.14;

 float floatLiteral2 = -0.5;

 // Character Literals

 char charLiteral1 = 'A';

 char charLiteral2 = '1';

 // String Literal

 const char *stringLiteral = "Hello, World!";

 // Displaying Integer Literals

 printf("Integer Literals:\n");

 printf("Literal 1: %d\n", integerLiteral1);

 printf("Literal 2: %d\n\n", integerLiteral2);

 // Displaying Floating-point Literals

 printf("Floating-point Literals:\n");

 printf("Literal 1: %f\n", floatLiteral1);

 printf("Literal 2: %f\n\n", floatLiteral2);

 // Displaying Character Literals

 printf("Character Literals:\n");

 printf("Literal 1: %c\n", charLiteral1);

 printf("Literal 2: %c\n\n", charLiteral2);

 // Displaying String Literal

 printf("String Literal:\n");

 printf("%s\n", stringLiteral);

 return 0;

}

Output:

Output

Integer Literals:
Literal 1: [value]
Literal 2: [value]

Floating-point Literals:
Literal 1: [value]
Literal 2: [value]

Character Literals:
Literal 1: %c
Literal 2: %c

String Literal:
[value]

Differences between constants and literals in C:

Example

Integer Literals:
Literal 1: [value]
Literal 2: [value]

Floating-point Literals:
Literal 1: [value]
Literal 2: [value]

Character Literals:
Literal 1: %c
Literal 2: %c

String Literal:
[value]

There exist various distinctions between the constants and literals in C programming. Some key disparities between the constants and literals in C include:

Feature Constants Literals
Definition Values that remain fixed during execution. Explicit notations representing constant values.
Mutability It is immutable and cannot be changed during runtime. The actual representation of constants in the code.
Syntax Named entities, defined with const keyword. Actual values are specified directly in the source code.
Memory Allocation It may require memory allocation depending on the type. There is no memory allocation; literals are part of the source code.
Scope It can have various scopes (local or global). The scope is usually limited to the block or expression.
Storage Class It can be associated with storage classes. Not associated with any storage class.
Initialization It must be initialized at the time of declaration. Initialization is part of the declaration in literals.
Immutability Once assigned, it cannot be changed. Dependent on the context; literals themselves are immutable.
Linkage It may have internal or external linkage. No linkage, literals are local to the scope.
Use in Expressions It can be used in expressions as variables. Literals can also be used directly in expressions.

Input Required

This code uses input(). Please provide values below: