In C programming, data types specify the type of data that a variable can hold. Each data type requires a certain amount of memory and defines what kind of operations can be performed on the data. C provides a variety of data types for different kinds of values, from integers to floating-point numbers to characters and more.
There are two main categories of data types in C:
Let’s break them down:
These are the most fundamental data types and are directly supported by C.
Integer types are used to store whole numbers (both positive and negative, without any decimal points).
int: Represents a standard integer. The size of an int can vary depending on the machine but is typically 4 bytes on most systems.
int x = 100;
short: Represents a short integer. It usually requires less memory than an int (typically 2 bytes).
short y = 10;
long: Represents a long integer. It usually requires more memory than an int (typically 8 bytes or 4 bytes on 32-bit systems).
long z = 100000L;
long long: Represents a longer integer type (typically 8 bytes).
long long a = 123456789123456789LL;
unsigned variants: These types represent non-negative integers. The unsigned keyword can be added to any of the integer types (e.g., unsigned int, unsigned short, etc.).
unsigned int u = 100;
Note: The size of these integer types may depend on the system architecture (e.g., 32-bit vs. 64-bit).
Floating-point types are used to represent real numbers (numbers that have fractional parts, like 3.14 or -0.001).
float: Represents single-precision floating-point numbers. It typically takes 4 bytes of memory.
float pi = 3.14f;
double: Represents double-precision floating-point numbers. It usually takes 8 bytes of memory and provides more precision than float.
double pi = 3.14159265358979;
long double: Represents extended-precision floating-point numbers. The size can vary but is typically 10, 12, or 16 bytes, depending on the compiler and system architecture.
long double pi = 3.141592653589793238L;
Character types are used to store individual characters (letters, digits, symbols).
char: Represents a single character. It typically takes 1 byte of memory and can store characters based on the ASCII character set.
char letter = 'A';
unsigned char: Represents a character, but can only store positive values (0 to 255). It still takes 1 byte of memory.
unsigned char u_letter = 'B';
signed char: Represents a character that can store both positive and negative values (from -128 to 127).
signed char s_letter = -65;
C doesn’t have a built-in Boolean data type, but stdbool.h library defines bool as a macro for int (0 for false, non-zero for true).
bool: Represents a boolean value, which can be true (1) or false (0).
#include <stdbool.h>
bool isValid = true;
Derived data types are built from the primary data types. These are more complex data types used to store collections of data or references to data.
An array is a collection of elements of the same data type stored in contiguous memory locations.
data_type array_name[size];
int arr[5] = {1, 2, 3, 4, 5};
A pointer is a variable that holds the memory address of another variable.
data_type *pointer_name;
int x = 10;
int *ptr = &x; // Pointer to x
A structure (or struct) is a user-defined data type that allows grouping different types of variables under one name.
struct structure_name {
data_type member1;
data_type member2;
// other members
};
struct Person {
char name[50];
int age;
};
struct Person person1 = {"John Doe", 30};
A union is similar to a structure, but all members share the same memory location. A union allows storing different data types in the same memory location, but only one member can hold a value at a time.
union union_name {
data_type member1;
data_type member2;
// other members
};
union Data {
int i;
float f;
char str[20];
};
union Data data;
data.i = 10; // Only one member can hold a value at a time
An enumeration (or enum) is a user-defined data type consisting of a set of named integer constants. It helps make code more readable by giving names to integral values.
enum enum_name { constant1, constant2, constant3, ... };
enum Day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
enum Day today = Wednesday;
C also provides type modifiers that modify the size and range of the basic data types:
signed: Used to indicate that the data type can store both positive and negative values (default for int).unsigned: Used to indicate that the data type can only store non-negative values.long: Increases the size of the data type.short: Reduces the size of the data type.Example (with modifiers):
unsigned int u = 10; // Non-negative integers only
short int si = 5; // Smaller range of integers
long double ld = 3.14159L; // More precision for floating-point numbers
Basic Data Types:
int, short, long, long long, float, double, charDerived Data Types:
Type Modifiers:
signed, unsigned, short, longEach data type in C has a specific use case and is optimized for particular operations. Choosing the right data type helps in managing memory effectively and ensuring that the program runs efficiently.
Open this section to load past papers