Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating, character,
etc.
There are the following data types in C language.
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
Basic Data Types:
The basic data types are integer-based and floating-point based. C language supports both signed
and unsigned literals.
The memory size of the basic data types may change according to 32 or 64-bit operating system.
Let's see the basic data types. Its size is given according to 32-bit architecture.
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.
Data Types Memory Size Range
char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
short 2 bytes −32,768 to 32,767
signed short 2 bytes −32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
int 2 bytes −32,768 to 32,767
signed int 2 bytes −32,768 to 32,767
unsigned int 2 bytes 0 to 65,535
short int 2 bytes −32,768 to 32,767
signed short int 2 bytes −32,768 to 32,767
unsigned short int 2 bytes 0 to 65,535
long int 4 bytes -2,147,483,648 to 2,147,483,647
signed long int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long int 4 bytes 0 to 4,294,967,295
float 4 bytes
double 8 bytes
long double 10 bytes
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.
1: Int:
Integers are entire numbers without any fractional or decimal parts, and the int data type is used
to represent them.
It is frequently applied to variables that include values, such as counts, indices, or other numerical
numbers.
The int data type may represent both positive and negative numbers because it is signed by default.
2: Char:
Individual characters are represented by the char data type. Typically used to hold ASCII.
Here are 256 characters that can be represented by a single char, which takes up one byte of
memory.
Characters such as 'A', 'b', '5', or '$' are enclosed in single quotes.
3: Float:
Floating numbers can be used to represent fractional units or numbers with decimal places.
It can store values with an accuracy of about 6 decimal places and a range of about 3.4 x 1038 in
4 bytes of memory.
4: Double:
Use two data types to represent two floating integers. When additional precision is needed, such
as in scientific calculations or financial applications, it provides greater accuracy compared to
float.
Double type, which uses 8 bytes of memory and has an accuracy of about 15 decimal places, yields
larger values.
Example: double pi = 3.14159265359;
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.
Derived Data Type:
Beyond the fundamental data types, C also supports derived data types.
Including arrays, pointers, structures, and unions.
These data types give programmers the ability to handle heterogeneous (combination of diff_types
or forms) data, directly modify memory, and build complicated data structures.
1: Array
An array, a derived data type, lets you store a sequence of fixed-size elements of the same type.
It provides a mechanism for joining multiple targets of the same data under the same name.
The index is used to access the elements of the array, with a 0 index for the first entry. The size of
the array is fixed at declaration time and cannot be changed during program execution. The array
components are placed in adjacent memory regions.
Here is an example of declaring and utilizing an array:
1. #include <stdio.h>
2.
3. int main() {
4. int numbers[5]; // Declares an integer array with a size of 5 elements
5.
6. // Assign values to the array elements
7. numbers[0] = 10;
8. numbers[1] = 20;
9. numbers[2] = 30;
10. numbers[3] = 40;
11. numbers[4] = 50;
12.
13. // Display the values stored in the array
14. printf("Values in the array: ");
15. for (int i = 0; i < 5; i++) {
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.
16. printf("%d ", numbers[i]);
17. }
18. printf("\n");
19.
20. return 0;
21. }
Output:
Values in the array: 10 20 30 40 50
2: Pointer:
A pointer is a derived data type that keeps track of another data type's memory address.
When a pointer is declared, the data type it refers to is stated first, and then the variable
name is preceded by an asterisk (*).
Pointers are commonly used in tasks such as function pointers, data structures, and
dynamic memory allocation.
Here is an example of declaring and employing a pointer:
1. #include <stdio.h>
2.
3. int main() {
4. int num = 42; // An integer variable
5. int *ptr; // Declares a pointer to an integer
6.
7. ptr = # // Assigns the address of 'num' to the pointer
8.
9. // Accessing the value of 'num' using the pointer
10. printf("Value of num: %d\n", *ptr);
11.
12. return 0;
13. }
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.
Output:
Value of num: 42
3: Structure:
A structure is a derived data type that enables the creation of composite data types by allowing the
grouping of many data types under a single name.
It provides the ability to create your own unique data structures by fusing together variables of
various sorts (various_data_types).
A structure's members or fields are used to refer to each variable within it.
Any data type, including different structures, can be a member of a structure.
A structure's members can be accessed by using the dot (.) operator.
A declaration and use of a structure is demonstrated here:
1. #include <stdio.h>
2. #include <string.h>
3. // Define a structure representing a person
4. struct Person {
5. char name[50];
6. int age;
7. float height;
8. };
9.
10. int main() {
11. // Declare a variable of type struct Person
12. struct Person person1;
13.
14. // Assign values to the structure members
15. strcpy(person1.name, "John Doe");
16. person1.age = 30;
17. person1.height = 1.8;
18.
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.
19. // Accessing the structure members
20. printf("Name: %s\n", person1.name);
21. printf("Age: %d\n", person1.age);
22. printf("Height: %.2f\n", person1.height);
23.
24. return 0;
25. }
Output:
Name: John Doe
Age: 30
Height: 1.80
4: Union:
A derived data type called a union enables you to store various data types in the same
memory address.
In contrast to structures, where each member has a separate memory space, members of a
union all share a single memory space.
A value can only be held by one member of a union at any given moment. When you need
to represent many data types interchangeably, unions come in handy.
Like structures, you can access the members of a union by using the dot (.) operator.
Here is an example of a union being declared and used:
1. #include <stdio.h>
2. // Define a union representing a numeric value
3. union NumericValue {
4. int intValue;
5. float floatValue;
6. char stringValue[20];
7. };
8. int main() {
9. // Declare a variable of type union NumericValue
10. union NumericValue value;
11. // Assign a value to the union
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.
12. value.intValue = 42;
13. // Accessing the union members
14. printf("Integer Value: %d\n", value.intValue);
15. // Assigning a different value to the union
16. value.floatValue = 3.14;
17. // Accessing the union members
18. printf("Float Value: %.2f\n", value.floatValue);
19.
20. return 0;
21. }
Output:
Integer Value: 42
Float Value: 3.14
Enumeration Data Type
A set of named constants or enumerators that represent a collection of connected values can be
defined in C using the enumeration data type (enum).
Enumerations give you the means to give names that make sense to a group of integral values,
which makes your code easier to read and maintain.
Here is an example of how to define and use an enumeration in C:
1. #include <stdio.h>
2.
3. // Define an enumeration for days of the week
4. enum DaysOfWeek {
5. Monday,
6. Tuesday,
7. Wednesday,
8. Thursday,
9. Friday,
10. Saturday,
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.
11. Sunday
12. };
13.
14. int main() {
15. // Declare a variable of type enum DaysOfWeek
16. enum DaysOfWeek today;
17.
18. // Assign a value from the enumeration
19. today = Wednesday;
20.
21. // Accessing the enumeration value
22. printf("Today is %d\n", today);
23.
24. return 0;
25. }
Output:
Today is 2
Void Data Type
The void data type in the C language is used to denote the lack of a particular type. Function
return types, function parameters, and pointers are three situations where it is frequently utilized.
1: Function Return Type:
A void return type function does not produce a value. A void function executes a task or action
and ends rather than returning a value.
Example:
void printHello() { printf("Hello, world!\n"); }
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.
2: Function Parameters:
The parameter void can be used to indicate that a function accepts no arguments.
Example:
void processInput(void) { /* Function logic */ }
3: Pointers:
Any address can be stored in a pointer of type void*, making it a universal pointer. It offers a
method for working with pointers to ambiguous or atypical types.
Example:
void* dataPtr;
The void data type is helpful for defining functions that don't accept any arguments when working
with generic pointers or when you wish to signal that a function doesn't return a value. It is
significant to note that while void* can be used to build generic pointers, void itself cannot be
declared as a variable type.
Here is a sample of code that shows how to utilize void in various situations:
1. #include <stdio.h>
2. // Function with void return type
3. void printHello() {
4. printf("Hello, world!\n");
5. }
6. // Function with void parameter
7. void processInput(void) {
8. printf("Processing input...\n");
9. }
10.
11. int main() {
12. // Calling a void function
13. printHello();
14.
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.
15. // Calling a function with void parameter
16. processInput();
17.
18. // Using a void pointer
19. int number = 10;
20. void* dataPtr = &number;
21. printf("Value of number: %d\n", *(int*)dataPtr);
22.
23. return 0;
24. }
Output:
Hello, world!
Processing input...
Value of number: 10
Bhushan Sarde---------------------------Ping: 7741967818---------------------Email id: bushansarde@gmail.com
Infosys Computer, Navanit Nagar, Nagpur, 440023.