C Data Types
What are Data Types in C?
Data types define the type of data that a variable can store.
Common C data types are int, float, char, and double.
Data types help the compiler allocate memory efficiently and manage memory usage.
Why Data Types are Important?
Data types are important because they:
• Define the type of value a variable can store.
• Help the compiler allocate memory efficiently.
• Improve program accuracy and performance.
• Make programs easier to understand and maintain.
Types of Data Types in C
C provides different types of data types for storing different kinds of values.
The most commonly used basic data types are:
• int → Stores integer values.
• float → Stores decimal values.
• char → Stores a single character.
• double → Stores large decimal values with higher precision.
Basic Data Types
• int → Stores integer values
• float → Stores decimal values
• char → Stores a single character
• double → Stores large decimal values with higher precision
Example
int age = 20;
float salary = 25000.50;
char grade = ‘A’;
Explanation
• int age = 20; → Stores an integer value 20 in the variable age.
• float salary = 25000.50; → Stores a decimal value in the variable salary.
• char grade = ‘A’; → Stores the character A in the variable grade.
These variables are initialized at the time of declaration.
Memory Size of Data Types
• char → 1 byte
• int → 4 bytes
• float → 4 bytes
• double → 8 bytes
Example
int age = 20;
float salary = 25000.50;
char grade = ‘A’;
Explanation
• int age = 20; → Stores an integer value 20 in the variable age.
• float salary = 25000.50; → Stores a decimal value in the variable salary.
• char grade = ‘A’; → Stores the character A in the variable grade.
These variables are initialized at the time of declaration.
Example Program
Program
#include <stdio.h>
int main()
{
int age = 20;
float salary = 25000.50;
char grade = ‘A’;
printf(“Age = %d\n”, age);
printf(“Salary = %.2f\n”, salary);
printf(“Grade = %c”, grade);
return 0;
}
Output
Age = 20
Salary = 25000.50
Grade = A
Explanation
• #include <stdio.h> → Includes the standard input/output library.
• int main() → Starting point of the program.
• int age = 20; → Declares and initializes an integer variable.
• float salary = 25000.50; → Declares and initializes a float variable.
• char grade = ‘A’; → Declares and initializes a character variable.
• printf() → Displays values on the screen.
• return 0; → Ends the program successfully.
Key Points
• Data types define what kind of value a variable can store.
• int, float, char, and double are the most commonly used data types in C.
• Different data types use different amounts of memory.
• Choosing the correct data type improves program efficiency.
• Data types help the compiler allocate memory properly.
• Understanding data types is essential for writing accurate C programs.
