In C, variables are used to store data, and data types define what kind of data can
be stored in these variables. Understanding variables and data types is
fundamental for creating well-structured and efficient programs.
1. Variables in C
A variable is a named storage location in memory that holds a value. The value
can be modified during program execution.
Declaration and Initialization
To use a variable, you must first declare it with its data type. A variable can also
be initialized at the time of declaration.
int number; // Declaration
number = 10; // Initialization
// Declaration and Initialization in one line
int age = 25;
The name of a variable must start with a letter (either lowercase or
uppercase) or an underscore (_ and must only contain alphanumeric
characters and underscores).
C is case-sensitive, so age and Age are considered different variables.
2. Data Types in C
C has a variety of data types to store different kinds of values. The main data
types can be classified into the following categories:
Primary Data Types:
1. int:
o Used to store integers (whole numbers).
, o Size: Typically 4 bytes (32 bits) on most systems.
o Example: int age = 25;
2. float:
o Used to store floating-point numbers (numbers with decimal points).
o Size: Typically 4 bytes.
o Example: float temperature = 36.6;
3. double:
o Used to store double-precision floating-point numbers (larger
decimal values).
o Size: Typically 8 bytes.
o Example: double pi = 3.14159265359;
4. char:
o Used to store a single character (like a, b, or 1).
o Size: Typically 1 byte.
o Example: char grade = 'A';
Derived Data Types:
1. Arrays:
o An array is a collection of variables of the same type, stored in
contiguous memory locations.
o Example: int numbers[5] = {1, 2, 3, 4, 5};
2. Pointers:
o A pointer is a variable that stores the memory address of another
variable.
o Example: int *ptr; (pointer to an integer)
3. Structures:
o A structure is a collection of variables of different data types grouped
together.
o Example:
struct Person {
char name[50];
int age;
};