Fundamentals 7 min read

Master C Constants and Variables: Types, Definitions, and Common Pitfalls

This article explains C/C++ constants—including integer, floating‑point, character, string, and symbolic forms—their syntax, examples, and common errors, and then covers variables, their definition, assignment, initialization, and related best‑practice notes.

AI Cyberspace
AI Cyberspace
AI Cyberspace
Master C Constants and Variables: Types, Definitions, and Common Pitfalls

Constants

In C/C++, a constant is a value that cannot be changed during program execution.

Integer constants

Decimal integer : e.g., 7, 20, 1000.

Octal integer : starts with 0, digits 0‑7, e.g., 07, 024, 01750.

Hexadecimal integer : starts with 0x or 0X, digits 0‑9 and a‑f/A‑F, e.g., 0x07, 0x14, 0x3e8.

Long integer constant : ends with l or L, occupies twice the storage of a basic integer, e.g., 7L, 20L, 1000L.

Example: Convert decimal 7, 20, 1000 to octal, hexadecimal and long integer constants.

Integer constants illustration
Integer constants illustration

Floating‑point constants

Two forms:

Decimal form : a number with a decimal point, e.g., 21.290000.

Exponential form (scientific notation) : mantissa, exponent indicator E/e, and integer exponent, e.g., 2.129000e+001.

NOTE:

Floating‑point constants default to double.

Suffix F/f makes it a float constant.

Suffix L/l makes it a long double constant.

12.3f   // float
12.3    // double
12.3e4f // float
12.3e4  // double
12.3e4l // long double
2.1E+5  // double (2.1×10^5)
2.1E+5f // float
123E-3  // double (123×10^-3)
123E-3f // float

Character constants

Ordinary character constant : a single character enclosed in single quotes, e.g., 'a'.

Escape character constant : a backslash followed by a special character, e.g., '\n'.

NOTE: Character constants occupy 1 Byte and are stored as ASCII codes, so they can be interchanged with integer values.

ASCII table
ASCII table

String constants

A sequence of characters enclosed in double quotes. In memory the characters are stored consecutively and terminated by the null character '\0', which also occupies 1 Byte.

#define CharA 'a'   // 'a' is a character constant, occupies 1 Byte
#define StringA "a"  // "a" is a string constant, occupies 2 Bytes
#define StringB "Hello World!" // occupies 13 Bytes

Symbolic constants

Defined using #define or const. #define creates a macro, while const defines a typed constant that must be initialized.

#define MAX 100
const double PI = 3.1415926;

NOTE: Symbolic constants cannot be modified after definition, are usually written in uppercase, and their defining expressions cannot contain variables or functions.

Variables

A variable is a named storage location whose value can change during program execution.

Three elements of a variable: name, data type, and value.

Definition syntax: data_type variable_name_list; Assignment uses the = operator; initialization assigns a value at definition.

int x;        // declaration
x = 10;       // assignment

char c = 'x'; // initialization
long w = 8L, l = 10L; // multiple initialization
double area, radius = 20; // radius initialized, area only declared

NOTE: Declaring a variable without initializing leaves its memory containing indeterminate data.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CC programmingVariablesConstants
AI Cyberspace
Written by

AI Cyberspace

AI, big data, cloud computing, and networking.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.