CBasics
C Variables and Data Types
Learn C variables and data types, understand how data is stored in memory, and choose the correct data type for efficient and reliable C programs.
What are Variables in C?Link to this section
Variables are named memory locations used to store data during program execution. Each variable holds a value that can change while the program runs.
In C, every variable must be declared with a specific data type before it is used.
Rules for Naming Variables :Link to this section
Variable names in C:
- Must start with a letter or underscore (_)
- Can contain letters, digits, and underscores
- Cannot start with a digit
- Cannot use C keywords
- Are case-sensitive
tip
Use meaningful variable names to improve readability.
Declaring Variables in C :Link to this section
A variable declaration specifies the data type and variable name.
Initializing Variables :Link to this section
Initialization assigns an initial value to a variable.
note
Uninitialized variables may contain garbage values.
Basic Data Types in C :Link to this section
C provides several built-in data types to store different kinds of data.
int
Used to store integer values.
float
Used to store decimal (floating-point) numbers.
double
Used to store double-precision floating-point values.
char
Used to store a single character.
note
Characters are enclosed in single quotes.
Size of Data Types :Link to this section
The size of data types depends on the system and compiler.
tip
Use sizeof to write portable programs.
Derived Data Types :Link to this section
Derived data types are formed using basic data types.
Examples include:
- Arrays
- Pointers
- Structures
- Unions
note
These will be covered in later topics.
Why Data Types Matter :Link to this section
Choosing the correct data type:
- Saves memory
- Improves performance
- Prevents unexpected behavior
warning
Using the wrong data type can cause overflow and data loss.
Check Your Understanding
Question 1 of 5What is a variable in C?
Practice Challenges
- Declare variables to store your age, height, and grade.
- Initialize variables with different data types and print them.
- Write a program to display the size of all basic data types.
- Identify and fix errors in variable declarations.