C

1. What are Literals?

Literals are the actual values assigned to variables. They are data items that do not have a name but represent a specific value directly in the code.

  • Integer Literals: Plain numbers without decimals (e.g., 100, -25).

  • Floating-point Literals: Numbers with decimal points (e.g., 3.14, 10.5).

  • Character Literals: Single characters enclosed in single quotes (e.g., 'A', '$').

  • String Literals: Sequences of characters enclosed in double quotes (e.g., "Hello EMBLAB").

2. What are Constants?

Constants are variables whose values cannot be modified once they are defined. If you try to change a constant, the compiler will throw an error. There are two primary ways to define constants in C:

A. Using the const Keyword

This is the most common way to declare a constant. It follows the standard variable declaration but starts with const.

C
const float PI = 3.14;
  • Benefit: The compiler knows the data type, which helps with debugging and memory management.

B. Using the #define Preprocessor

This defines a "symbolic constant." The preprocessor replaces every instance of the name with the value before the code is even compiled.

C
#define MAX_SPEED 120
  • Difference: It does not use memory like a variable and does not require a semicolon at the end.

3. Comparison Table

Featureconst Variable#define Constant
Type CheckingPerformed by compilerNo type checking
ScopeRespects scope (local/global)Global (until #undef)
MemoryOccupies memoryNo memory (text replacement)
Syntaxconst int X = 10;#define X 10

4. Escape Sequences (Special Literals)

C uses special character literals called Escape Sequences to represent non-printable characters or characters with special meanings:

  • \n : Newline (moves cursor to the next line)

  • \t : Horizontal Tab

  • \\ : Backslash

  • \" : Double Quote

Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now