C Variables, Constants, and Literals: A Complete Guide
C Variables, Constants, and Literals
This tutorial covers the fundamentals of C variables, naming conventions, literal types, and how to declare constants for reliable code.
Variables in C
A variable is a named storage location that holds data. Each variable must have a unique identifier that represents a memory address.
int playerScore = 95;
The identifier playerScore stores an int value of 95. Variables are mutable, meaning their value can change during execution.
char ch = 'a';
// later in the code
ch = 'l';
Variable Naming Rules
- Allowed characters: letters (A‑Z, a‑z), digits (0‑9), and underscore.
- The first character must be a letter or underscore.
- Length is unlimited by the language, but some compilers truncate names longer than 31 characters.
Tip: Use descriptive names like firstName instead of fn for readability and maintainability.
C is a strongly typed language. Once declared, the data type of a variable cannot change.
int number = 5; // integer
number = 5.5; // error
double number; // error – redeclaration with a different type
To store decimal values, declare the variable as float or double.
For more on data types, see our Data Types guide.
Literals
Literals are fixed values that appear directly in the source code. They cannot change at runtime.
Integer Literals
C supports three integer literal bases:
- Decimal (base 10)
- Octal (base 8) – prefixed with 0
- Hexadecimal (base 16) – prefixed with 0x
Decimal: 0, -9, 22
Octal: 021, 077, 033
Hex: 0x7f, 0x2a, 0x521
Floating‑Point Literals
These contain a fractional part or an exponent:
-2.0
0.0000234
-0.22E-5
Note: E-5 equals 10-5.
Character Literals
Single characters enclosed in single quotes:
'a', 'm', 'F', '2', '}'
Escape Sequences
Use a backslash to represent characters that cannot be typed directly:
| Escape Sequence | Meaning |
|---|---|
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\? | Question mark |
\0 | Null character |
Example: \n inserts a line break.
String Literals
Sequences of characters enclosed in double quotes:
"good" // string constant
"" // empty string
" " // string of six spaces
"x" // single character string
"Earth is round\n" // prints with a newline
Constants
Use the const keyword to declare immutable values:
const double PI = 3.14;
PI = 2.9; // compilation error
Alternatively, define constants with the preprocessor directive #define. Learn about macros in our C Macros tutorial.
C Language
- Master C# Variables & Primitive Data Types: A Complete Guide
- Mastering C++ Variables, Literals, and Constants: A Comprehensive Guide
- Understanding C Storage Classes: Scope, Lifetime, and Performance
- Python Variables, Constants, and Literals – A Comprehensive Guide
- Java Variables and Literals: A Comprehensive Guide
- C Variables, Data Types, and Constants – A Practical Guide
- Understanding Variables in C: Types, Naming Rules, and Memory Management
- Understanding C Constants and Literals: Types, Usage, and Best Practices
- C++ Constants & Literals Explained: Types, Syntax, and Best Practices
- Mastering C# Constants & Literals: Types, Rules, and Best Practices