C - Constants and Literals

In C programming, a constant is a value that cannot be changed during the execution of a program. A literal is a notation used to represent a constant value in code. Constants and literals play an important role in programming by allowing you to define and use fixed values in your code.

There are several types of constants and literals in C programming, including:

Integer Constants and Literals:

Integer constants and literals are used to represent integer values. An integer literal can be written in decimal, octal, or hexadecimal notation. For example:

int a = 10;       // decimal integer literal
int b = 012;      // octal integer literal
int c = 0xA;      // hexadecimal integer literal

Floating-Point Constants and Literals:

Floating-point constants and literals are used to represent floating-point values, such as decimal numbers. Floating-point literals are written with a decimal point or an exponent. For example:

float a = 3.14;   // floating-point literal
float b = 1e-5;   // floating-point literal with exponent

Character Constants and Literals:

Character constants and literals are used to represent character values. Character literals are written within single quotes. For example

char a = 'A';  

String Constants and Literals:

String constants and literals are used to represent strings of characters. String literals are written within double quotes. For example:

char str[] = "Hello, World!";   // string literal

Enumeration Constants:

Enumeration constants are used to represent a set of named integer values. Enumeration constants are defined using the enum keyword. For example:

enum Days {Monday, Tuesday, Wednesday, Thursday, Friday};

Macro Constants:

Macro constants are defined using the #define preprocessor directive. They are used to define symbolic constants that can be replaced with their values during compilation. For example:

#define PI 3.14159

Boolean Constants:

Boolean constants are used to represent true or false values. In C programming, the values 0 and 1 are used to represent false and true, respectively.

_Bool a = 1;      // true
_Bool b = 0;      // false

Constants and literals are useful in programming because they allow you to define fixed values that can be used throughout your program. By using constants and literals, you can make your code more readable and easier to maintain. Constants and literals also help to prevent errors by ensuring that values are consistent throughout your program.

In conclusion, constants and literals are an essential part of C programming. They allow you to define fixed values that can be used throughout your program and make your code more readable and easier to maintain. By understanding the different types of constants and literals in C programming, you can write better code and avoid common programming errors.