C Variables, Data Types, and Constants – A Practical Guide
What is a Variable?
A variable is an identifier that holds a value. Unlike constants, a variable’s value can change during program execution.
Variables can be referenced from multiple places in a program. A descriptive name helps readers understand the variable’s purpose.
int height = 170; // height in centimeters int age = 29; // age in years
Before using a variable, it must be declared. Variable names may contain letters, digits, and underscores, but must start with a letter or underscore.
Variable Naming Rules
- Only letters (a–z, A–Z), digits (0–9), and underscore (_).
- Cannot start with a digit.
- No whitespace.
- Cannot be a reserved keyword.
- C is case‑sensitive:
ageandAGEare distinct identifiers.
Valid and Invalid Names
Valid:
height _HEIGHT _height1 My_name
Invalid:
1height Hei$ght My name
Declaring and Initializing Variables
Declare first, then assign:
int my_variable; my_variable = 48;
Or combine declaration and initialization:
int my_variable = 48;
Data Types Overview
C offers three categories of data types:
- Primitive (int, char, float, double, void)
- Derived (arrays, functions, pointers, structures)
- User‑defined (typedef, structs, enums, unions)
Primitive Data Types
Five fundamental types:
- int – integer values
- char – single characters
- float – single‑precision floating point
- double – double‑precision floating point
- void – no value (used in function signatures)
Standard Data Type Sizes and Ranges
| Data type | Size (bytes) | Range |
|---|---|---|
| char / signed char | 1 | -128 to 127 |
| unsigned char | 1 | 0 to 255 |
| int / signed int | 2 | -32768 to 32767 |
| unsigned int | 2 | 0 to 65535 |
| short int / unsigned short int | 2 | 0 to 65535 |
| signed short int | 2 | -32768 to 32767 |
| long int / signed long int | 4 | -2147483648 to 2147483647 |
| unsigned long int | 4 | 0 to 4294967295 |
| float | 4 | ±3.4E-38 to ±3.4E+38 |
| double | 8 | ±1.7E-308 to ±1.7E+308 |
| long double | 10 | ±3.4E-4932 to ±1.1E+4932 |
Note: C does not have a built‑in Boolean type; use int with 0 (false) or 1 (true) or the stdbool.h header for bool.
Integer Data Types
Integers are whole numbers. The standard 16‑bit int ranges from –32768 to 32767. Shorter and longer variants are available to suit the required range.
Typical declarations:
int age; short int smallCount; long int largeCount;
Floating‑Point Data Types
Use float for values with fractional parts up to about 6‑digit precision. double offers about 14‑digit precision, and long double provides the most precision.
Examples:
float ratio = 0.75f; double balance = 12345.6789; long double pi = 3.141592653589793238462643383279502884L;
Character Data Type
Stores a single character in a one‑byte value. Characters are specified in single quotes.
Example:
char letter = 'K';
Void Data Type
Indicates that a function does not return a value.
Example:
void displayData(void) {
// ...
}
Declaring Multiple Variables
Variables of the same type can be declared on a single line, separated by commas:
int x, y, z; float salary = 13.48f; char letter = 'K';
Print examples use format specifiers:
printf("%d\n", z);
printf("%f\n", salary);
printf("%c\n", letter);
Constants
A constant value never changes during program execution. C provides several forms:
Integer Constants
Decimal, octal (prefixed with 0), and hexadecimal (prefixed with 0x) literals.
Examples:
111 // decimal 012 // octal (10 decimal) 0x2BCD // hexadecimal (19293 decimal)
Character Constants
Single character surrounded by single quotes or by its ASCII code.
Examples:
'A' '9'
String Constants
Sequence of characters enclosed in double quotes.
Examples:
"Hello" "Programming"
Real (Floating‑Point) Constants
Numbers with a decimal point or in scientific notation.
Examples:
202.15 300.00 1.23e-4 // 0.000123
Defining Constants with const and #define
Using const reserves storage and enforces immutability:
#include <stdio.h>
int main(void) {
const double PI = 3.141592653589793;
printf("%f\n", PI);
return 0;
}
Using #define is a pre‑processor macro that does not consume storage:
#include <stdio.h>
#define PI 3.141592653589793
int main(void) {
printf("%f\n", PI);
return 0;
}
Summary
- Variables are mutable identifiers that store data.
- Constants are immutable values fixed at compile time.
- C’s core types:
int,float,char, andvoid. - Each type has a defined size and range, critical for memory‑constrained applications.
C Language
- Master C# Variables & Primitive Data Types: A Complete Guide
- Understanding Variable Scope in C#: Class, Method, and Block Levels
- Mastering C++ Variables, Literals, and Constants: A Comprehensive Guide
- C Variables, Constants, and Literals: A Complete Guide
- Java Primitive Data Types: A Clear Guide
- Understanding C Data Types: A Comprehensive Guide
- Understanding Variables in C: Types, Naming Rules, and Memory Management
- Understanding C Constants and Literals: Types, Usage, and Best Practices
- Mastering C Pointers: Practical Steps to Advanced Programming
- Unlocking Sinumerik 840D: Master $TC_DP Tool Data Variables for Precise CNC Programming