Programs need memory to store values such as age, marks, temperature, characters, and decimal numbers. In C, these values are stored using variables.
What is a Variable?
A variable is a named memory location used to store data. Think of it as a labeled container that holds a specific value while your program is running.
Why Data Types Are Needed
Different types of data use different amounts of memory. The compiler needs to know exactly what kind of data you are storing to allocate the correct space.
In embedded systems, this concept becomes critical because memory is limited and closely tied to hardware. Choosing the correct data type directly affects memory usage, execution speed, and how the program interacts with hardware registers. Inefficient use of memory or incorrect data types can lead to performance issues or unexpected behavior, so understanding how variables map to memory is essential for writing reliable and efficient programs.
Common Data Types in C
| Data Type | Used For | Example |
|---|---|---|
int |
Integer numbers | 10 |
char |
Single character | 'A' |
float |
Decimal values | 3.14 |
double |
Large decimal values | 12.34567 |
Example Program
#include <stdio.h>
int main()
{
int age = 25; // integer variable
float salary = 25000.50; // decimal (floating-point) variable
char grade = 'A'; // character variable
printf("%d\n", age); // prints integer value
printf("%.2f\n", salary); // prints float with 2 decimal places
printf("%c\n", grade); // prints character
return 0;
}
Console Output
25
25000.50
A
Understanding Format Specifiers
Used with printf() to tell the compiler what type of data to print.
| Specifier | Meaning |
|---|---|
%d |
Integer |
%c |
Character |
%f |
Float |
%lf |
Double |
Rules for Variable Names
When naming variables in C, you must follow these direct compiler rules:
-
•
Characters Allowed: Can only contain letters (A-Z, a-z), digits (0-9), and underscores (
_). Examples:age,total_marks,num1are valid. -
•
First Character: Must begin with a letter or an underscore. It cannot start with a number (e.g.,
1ageis invalid). -
•
No Spaces or Symbols: Spaces and special symbols (like
-,$,@) are strictly forbidden (e.g.,my salaryortotal-marksare invalid). -
•
No Reserved Keywords: Cannot use C language keywords (like
int,float,char) as names. -
•
Case Sensitivity: Names are case-sensitive;
ageandAgeare treated as two entirely separate variables.
Memory Concept
When a program runs, variables are stored in RAM (Random Access Memory) as contiguous bytes. Each variable is associated with a memory address, and its data type determines how many bytes are allocated and how those bytes are interpreted (e.g., integer, floating-point, character). The compiler translates variable names into memory addresses and generates instructions to read and write these locations. Data is stored in binary form, and accessing a variable essentially means accessing its corresponding memory address.
Why Important for Embedded Engineers
In embedded systems, this mapping between variables and memory is tightly coupled with hardware. Many peripherals are accessed through memory-mapped registers, where specific addresses control hardware behavior. Choosing appropriate data types ensures correct alignment, efficient memory usage, and proper interaction with these registers. Since embedded systems have limited RAM and strict performance requirements, inefficient data usage or incorrect type selection can lead to memory overflow, slower execution, or hardware misbehavior.
char sensor_status; // Uses only 1 byte (ideal for memory-mapped registers)
int adc_value; // Uses 2 or 4 bytes (standard integer size)
Interview Question
Q
What is the difference between float and double?
What is the difference between float and double?
Both store decimal numbers, but double has higher precision (more decimal places) and occupies twice the memory size (8 bytes) of a float (4 bytes).