Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

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

  1. Allowed characters: letters (A‑Z, a‑z), digits (0‑9), and underscore.
  2. The first character must be a letter or underscore.
  3. 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: 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 SequenceMeaning
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tHorizontal tab
\vVertical tab
\\Backslash
\'Single quote
\"Double quote
\?Question mark
\0Null 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

  1. Master C# Variables & Primitive Data Types: A Complete Guide
  2. Mastering C++ Variables, Literals, and Constants: A Comprehensive Guide
  3. Understanding C Storage Classes: Scope, Lifetime, and Performance
  4. Python Variables, Constants, and Literals – A Comprehensive Guide
  5. Java Variables and Literals: A Comprehensive Guide
  6. C Variables, Data Types, and Constants – A Practical Guide
  7. Understanding Variables in C: Types, Naming Rules, and Memory Management
  8. Understanding C Constants and Literals: Types, Usage, and Best Practices
  9. C++ Constants & Literals Explained: Types, Syntax, and Best Practices
  10. Mastering C# Constants & Literals: Types, Rules, and Best Practices