In C, data types can be broadly categorized into three main types:
1. Predefined Data Types
These are the fundamental data types built into the C language.
- Integer Types:
int
: Represents integer values. Example:int age = 25;
short
: Represents short integer values. Example:short temp = -5;
long
: Represents long integer values. Example:long population = 100000L;
long long
: Represents very large integers. Example:long long bigNum = 9876543210LL;
- Floating-Point Types:
float
: Represents single-precision floating-point numbers. Example:float height = 5.9f;
double
: Represents double-precision floating-point numbers. Example:double pi = 3.14159;
long double
: Represents extended precision floating-point numbers. Example:long double bigNum = 3.14159265358979323846L;
- Character Type:
char
: Represents a single character. Example:char initial = 'A';
2. Derived Data Types
- array, pointers, function
3. Derived Data Types
- Structures (
struct
)
Structures allow you to group variables of different types together. This is useful for representing a record or an entity
struct Person {
char name[50];
int age;
float height;
};
// Usage
struct Person person1;
strcpy(person1.name, “Alice”);
person1.age = 30;
person1.height = 5.5;
-
Unions (
union
)Unions are similar to structures but allow you to store different data types in the same memory location. Only one member can hold a value at any given time.
union Data {
int intValue;
float floatValue;
char charValue;
};
// Usage
union Data data;
data.intValue = 10; // Only intValue holds a valid value at this point
-
Enumerations (
enum
)Enumerations define a variable that can take a set of named integer constants. This improves code readability and maintainability.
enum Day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
// Usage
enum Day today;
today = Wednesday;
-
Typedef
typedef
is a keyword that allows you to create an alias for existing data types. This can simplify code and improve clarity.
typedef struct {
char name[50];
int age;
} Person;
// Usage
Person person1; // Now you can use Person instead of struct Person