C Data Types

Learn C data types, format specifier, etc.

Data types

Data types define the type of data that a variable can hold. Data types specify the size and format of values that can be stored in variables, which helps the compiler allocate memory and perform operations on those values correctly. C provides several built-in data types, which can be categorized into the following major groups:

Basic Data Types:

In the C programming language, there are several basic or fundamental data types that are used to represent different kinds of values. These basic data types include:

int: Used for integer values. It can be signed (can hold both positive and negative values) or unsigned (only holds non-negative values).

char: Used for characters. It can store a single character, which is enclosed in single quotes ('a', '1', etc.).

float: Used for floating-point numbers, typically with single precision.

double: Used for double-precision floating-point numbers, providing more precision than float.

_Bool: Used for boolean values, where 0 represents false and non-zero represents true.

Data Types Occupied Size Range Format Specifier
char 1 byte -128 to 127 %c
_Bool 1 byte Only two possible values: true (1) and false (0). You can use the %d format specifier, which treats true as 1 and false as 0.
int 2 bytes -2,147,483,648 to 2,147,483,647 %d
float 4 bytes Approximately: ±1.2 x 10^-38 to ±3.4 x 10^38 %f
double 8 bytes Approximately ±2.2 x 10^-308 to ±1.8 x 10^308 %lf
Additional data type information:
unsigned char 1 byte 0 to 255 %hhu
short int 1 byte -32,768 to 32,767 %hd
unsigned short int 1 byte 0 to 65,535 (2^16 - 1) %hu
unsigned int 2 bytes 0 to 4,294,967,295 (2^32 - 1) %u
long int 4 bytes -2,147,483,648 to 2,147,483,647 (2^31 - 1 to -2^31 + 1) %ld
unsigned long int 4 bytes 0 to 4,294,967,295 (2^32 - 1) %lu
long double 10 to 16 bytes 3.4E-4932 to 1.1E+4932 %Lf

Note: An 'unsigned int' typically ranges from 0 to 4,294,967,295 (2^32 - 1) on systems with 32-bit 'unsigned int', and from 0 to 18,446,744,073,709,551,615 (2^64 - 1) on systems with 64-bit 'unsigned int'.

On most 32-bit systems, A 'long int' typically ranges from -2,147,483,648 to 2,147,483,647 (2^31 - 1 to -2^31 + 1), and on most 64-bit systems, typically ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (2^63 - 1 to -2^63 + 1).

An 'unsigned long int' typically ranges from 0 to 4,294,967,295 (2^32 - 1) on most 32-bit systems, and On most 64-bit systems, typically ranges from 0 to 18,446,744,073,709,551,615 (2^64 - 1).

In practice, 'long double' typically provides a range of roughly 1.1E-4932 to 1.1E+4932, but the exact range may vary depending on the specific implementation and hardware.

Derived Data Types:

Derived data types in C are data types that are created by combining or modifying the fundamental or basic data types in some way. They are derived from the primary data types and include pointers, arrays, structures, and unions. Here's an overview of these derived data types:

Pointers: Pointers are variables that store memory addresses. They allow you to work with the memory locations of other data types. Pointer types are derived from the basic data types they point to. For example, an int* is a pointer to an integer, and a char* is a pointer to a character.

Arrays: Arrays are collections of elements of the same data type stored in contiguous memory locations. The array itself is considered a derived data type. For example, an 'int[]' is an array of integers.

User-Defined Data Types:

In C, you can define custom data types using various mechanisms. Here are some common ways to define custom data types:

typedef: The 'typedef' keyword allows you to create a new name for an existing data type. It's often used to improve code readability or to create custom data type names. For example:

typedef int Age;
Age personAge = 30;

In this example, 'Age' is defined as a new name for the 'int' data type.

struct: You can use the struct keyword to create a composite data type that groups together variables of different data types. For example:

struct Point {
    int x;
    int y;
};

struct Point p1;
p1.x = 10;
p1.y = 20;

Here, a custom data type 'struct Point' is defined to represent a point with 'x' and 'y' coordinates.

Union: Similar to 'struct', you can use 'union' to define a composite data type, but in a union, all members share the same memory location. This can be useful in situations where you want to represent data that can be one of several types. For example:

union Variant {
    int intValue;
    float floatValue;
    char stringValue[20];
};

union Variant data;
data.intValue = 42;

Here, 'union Variant' can hold an integer, a float, or a string, but only one at a time.

enum: The enum keyword allows you to create an enumerated data type with a set of named values. For example:

enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
enum Days today = Wednesday;

In this case, 'enum Days' defines a custom data type with named constants for days of the week.

Format Specifiers

C format specifiers are placeholders used in format strings to control how data is formatted and displayed when using functions like printf and scanf. They are used to specify the type and formatting of data that is to be input or output. Here are three commonly used C format specifiers with their meanings:

Data Types Formats Meanings
int %d Respects a decimal integer value.
%u Respects an unsigned integer value.
%o Respect an unsigned octal value.
%x Respect an unsigned hexadecimal value.
float %f Represents a floating point value.
%e Floating value in decimal or exponential form.
char %c Represents a single character value.
%s Represents a strings of value of characters.

What is New?

We have just updated this website & still working on adding new content & updating existing features.