Explain Data Type in c Programming
Explain Data Type in c Programming
(Tuesday, April 15, 2025, 11:36 AM IST, Bhubaneswar, Odisha, India). Data types are
fundamental to any programming language, as they define the kind of data a variable can hold
and the operations that can be performed on that data.
In C, data types can be broadly categorized into:
1. Basic (Primitive) Data Types:
These are the fundamental building blocks provided by the C language itself.
● int (Integer):
○ Used to store whole numbers (both positive and negative) without any fractional
part (e.g., -10, 0, 100).
○ The exact size (in bytes) of an int can vary depending on the compiler and the
underlying architecture, but it's typically 2 or 4 bytes.
○ Modifiers:
■ short int (or just short): Usually occupies less memory than int and has a
smaller range.
■ long int (or just long): Usually occupies more memory than int and has a
larger range.
■ unsigned int (or just unsigned): Stores only non-negative integer values (0
and positive). It can represent a larger range of positive numbers compared
to a signed int of the same size.
■ signed int (or just signed): Stores both positive and negative integer values
(this is the default for int).
int age = 30;
short count = 100;
long population = 1000000000L; // 'L' suffix indicates a long
integer literal
unsigned int positive_value = 500;
signed int negative_value = -200;
● float (Floating-Point):
○ Used to store single-precision floating-point numbers (numbers with a decimal
point).
○ Typically occupies 4 bytes.
○ Has a limited precision compared to double.
float pi = 3.14159f; // 'f' suffix indicates a float literal
float temperature = 25.5f;
● char (Character):
○ Used to store single characters (e.g., 'a', 'X', '7', '$').
○ Typically occupies 1 byte.
○ Internally, characters are stored as their ASCII (or other encoding) values.
○ Modifiers:
■ signed char: Can store both positive and negative small integer values
(representing character codes).
■ unsigned char: Can store only non-negative small integer values
(representing character codes).
char initial = 'J';
char digit = '9';
signed char ascii_value = 65; // Represents 'A' in ASCII
unsigned char byte_value = 200;
● void:
○ Represents the absence of a type or a generic type.
○ Has several uses:
■ As the return type of a function that doesn't return a value (e.g., void
printMessage()).
■ As a pointer to a value of unknown type (e.g., void *ptr). You need to cast a
void pointer to a specific type before dereferencing it.
■ In function parameter lists to indicate that the function takes no arguments
(e.g., int main(void)).
void display(); // Function that doesn't return a value
void *generic_pointer;
● Pointers:
○ Variables that store the memory address of other variables.
○ Essential for dynamic memory allocation, passing arguments by reference, and
working with data structures.
int x = 10;
int *ptr_x = &x; // 'ptr_x' stores the memory address of 'x'
● Structures (struct):
○ A composite data type that groups together variables of different data types under a
single name.
○ Useful for representing records or entities with multiple attributes.
struct Student {
char name[50];
int roll_no;
float marks;
};
struct Student s1;
● Unions (union):
○ Similar to structures, but all members share the same memory location.
○ The size of a union is the size of its largest member.
○ Used when you need to store different types of data in the same memory location at
different times.
union Data {
int i;
float f;
char str[20];
};
union Data d;
● Enumerations (enum):
○ A user-defined data type that consists of a set of named integer constants.
○ Improves code readability by using meaningful names instead of raw integer
values.
enum Days { SUN, MON, TUE, WED, THU, FRI, SAT };
enum Days today = WED;