Working with Variables and Data Types in C Programming
Data Types
All variables and constants in C must be declared with an associated data type that defines how the value is stored in memory. Comon built-in data types in C include:
char // Character data type
short // Short integer type
int // Standard integer type
long // Long integer type
long long // Extended-length integer type
float // Single-precision floating point
double // Double-precision floating point
// Note: C does not have a native built-in string type
Two core questions about C data types are:
- Why does C provide such a wide range of data types?
- How much memory does each type occupy on standard systems?
You can check the memory footprint (in bytes) of each type using the sizeof operator, as shown below:
#include <stdio.h>
int main() {
printf("char size: %zu byte\n", sizeof(char));
printf("short size: %zu bytes\n", sizeof(short));
printf("int size: %zu bytes\n", sizeof(int));
printf("long size: %zu bytes\n", sizeof(long));
printf("long long size: %zu bytes\n", sizeof(long long));
printf("float size: %zu bytes\n", sizeof(float));
printf("double size: %zu bytes\n", sizeof(double));
printf("long double size: %zu bytes\n", sizeof(long double));
return 0;
}
The wide range of available types allows developers to accurately model real-world values of different scales, reducing unnecessary memory usage.
Sample type declarations:
char user_initial = 'L';
int item_stock = 75;
int monthly_revenue = 32000;
Variables and Constants
Many values in real-world scenarios are fixed (e.g., mathematical pi, blood type, passport ID, universal gravity constant) while others change over time (e.g., user age, shopping cart total, monthly rainfall). In C, fixed values are represented with constants, while mutable values are represented with variables.
Defining Variables
The stendard syntax for variable declaration includes a type, variable name, and optional initial value:
int student_age = 16;
float shipment_weight = 19.7f;
char menu_option = 'Y';
Variable Naming Rules
- Variable names may only contain letters, numbers, and underscores, and cannot start with a numeric digit
- C is case-sensitive:
OrderTotalandordertotalare treated as separate identifiers - Reserved C keywords (e.g.,
int,if,while) cannot be used as variable names - While most modern compilers support long variable names, limiting names to 8 or fewer characters is recommended for readability and legacy compatibility
- CamelCase naming (capitalizing the first letter of each subsequent word in multi-word names) is a widely adopted best practice for clarity
Variable Categories
C variables are split into two primary categories:
- Local variables: declared inside a code block or function
- Global variables: declared outside all functions, at the top level of a source file
#include <stdio.h>
int global_counter = 2024; // Global variable
int main() {
int local_value = 2023; // Local variable
// Local variable with same name as global is allowed
int global_counter = 2025;
printf("global_counter value: %d\n", global_counter);
return 0;
}
When a local variable shares the same name as a global variable, the local variable takes precedence within its scope.
Working with Variables
The example below demonstrates accepting user input, performing a simple calculation, and printing the result using standard I/O functions:
#include <stdio.h>
int main() {
int first_input = 0;
int second_input = 0;
int sum_result = 0;
printf("Enter two integer values: ");
scanf("%d %d", &first_input, &second_input);
sum_result = first_input + second_input;
printf("Calculated sum: %d\n", sum_result);
return 0;
}
This example uses scanf() for input and printf() for formatted output. The & operator passed to scanf() provides the memory address of the variable to store the input value.
Variable Scope and Lifecycle
Scope
The scope of an identifier is the region of code where the identifier can be referenced without throwing a compilation error:
- Local variables have scope limited to the code block where they are declared
- Global variables have scope across the entire multi-file project
Lifecycle
A variable's lifecycle is the period between when memory is allocated for the variable and when that memory is released:
- Local variable lifecycle starts when execution enters the block where the variable is declared, and ends when execution exits that block
- Global variables remain allocated for the entire runtime of the program
Constants
C includes four distinct types of constants, each with different use cases:
- Literal constants
const-modified read-only variables#definepreprocessor identifier constants- Enum constants
#include <stdio.h>
// Enum constant definition
enum AccessLevel {
ADMIN,
EDITOR,
VIEWER
};
// ADMIN, EDITOR, VIEWER are enum constants, defaulting to 0, 1, 2 respectively
#define MAX_USER_LIMIT 120 // #define identifier constant
int main() {
// Literal constant examples
3.14159;
7500;
// const-modified read-only variable
const float PI = 3.14f;
// PI = 5.2f; // This line will throw a compilation error, as const values cannot be modified
printf("Maximum allowed users: %d\n", MAX_USER_LIMIT);
printf("Admin access level code: %d\n", ADMIN);
printf("Editor access level code: %d\n", EDITOR);
printf("Viewer access level code: %d\n", VIEWER);
return 0;
}
Note that const-modified values are technically still variables with a read-only restriction, rather than true compile-time constants.
Practice Exercise
For each given identifeir and declaration, determine if the variable name is C-compliant, and identify the data type of all valid declared variables.