Data types in Data Structures

 

Data types

Data types are the type of data stored in a C program. Data types are used while defining a variable or functions in C. The compiler needs to understand the type of predefined data it will encounter in the program. A data type is an attribute that tells a computer how to interpret the value.

C provides several built-in data types, such as integer (int), character (char), floating-point (float), and double-precision floating-point (double), among others. Each data type has its own set of possible values and operations that can be performed on it.

Types Of Data Types In C

There are majorly five main categories of Data Type in C:

Data TypeExample of Data Type
Primary Data TypeInteger, Floating-point, double, string.
Derived Data TypeUnion, structure, array, etc.
Enumerated Data TypeEnums
Void Data TypeEmpty Value
Bool TypeTrue or False

Primary Data Types In C

The C programming language has five primitive or primary data types. 

1. Integer (int): Refers to positive and negative whole numbers (without decimal), such as 10, 12, 65, 3400, etc.

Example:


#include <stdio.h>
void main()
{
int i = 5;
printf("The integer value is: %d \n", i);
}
Copy code

2. Character (char): Refers to all the ASCII character sets within single quotes such as ‘a’, ‘A’, etc.

Example:

#include <stdio.h>
void main()
{
char c = 'b';
printf("The character value is: %c \n", c);
}
Copy code

3. Floating-point (float): Refers to all the real number values or decimal points, such as 3.14, 10.09, 5.34, etc.

Example:

#include <stdio.h>
void main()
{
float f = 7.2357;
printf("The float value is: %f \n", f);
}
Copy code

4. Double (double): Used when the range exceeds the numeric values that do not come under either floating-point or integer data type. 

Example:

#include <stdio.h>
void main()
{
double d = 71.2357455;
printf("The double value is: %lf \n", d);
}

Data Type Modifiers In C 

Modifiers are C keywords that modify the meaning of fundamental data types. It indicates how much memory will be allocated to a variable. Modifiers are prefixed with fundamental data types to adjust the memory allocated for a variable. C Programming Language has four data type modifiers:

  • long
  • short
  • signed
  • Unsigned

These modifiers make the memory required for primary data types more precise. 

Size Of Data Types In C

The size of each data type is defined in bits or bytes (8 bits). Each data type in C is associated with a specific range of values defined as below: 

Data TypeFormat SpecifierMinimal RangeSize in bit
unsigned char%c0 to 2558
char%c-127 to 1278
signed char%c-127 to 1278
int%d, %i-32,767 to 32,76716 or 32
unsigned int%u0 to 65,53516 or 32
signed int%d, %i-32,767 to 32,767 (same as int)16 or 32
short int%hd-32,767 to 32,76716
unsigned short int%hu0 to 65,53516
signed short int%hdSame as short int16
+ More 8 Rows

Fun Fact: Double is called double because it can hold double the float values.

NOTE: Format specifiers are used while printing the value of a variable within the printf() statement.

Must Read: Top 10 Programming Languages to Learn in 2023

Understanding Data Type In C In Terms Of Memory

A data type reserves a chunk of memory to store and represent a value. A single byte consists of 8 bits of memory. Consider the below representation of byte, where each bit is represented by an underscore (_):

byte: _ _ _ _ _ _ _ _ < – 8 bits

Since we have 8 positions, we can input either a 0 or 1. So we can have a combination of 2^8 or 256 distinct values, which can be represented from the 8 bits, which is the overall range of a byte. 

byte: 0 0 0 0 0 0 0 0  <- Represents  “0”

byte: 0 0 0 0 0 0 0 1  <- Represents  “1”

        .. so on ..

byte: 1 1 1 1 1 1 1 0  <- Represents  “254”

byte: 1 1 1 1 1 1 1 1  <- Represents  “255”

Similarly, we have, 

int:  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _   < – 16 bits

long: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  < – 32 bits

Data Type Value Out Of Range

Whenever you try to add value that is outside the range of the data type. The C compiler would throw an error. 

Example:

#include <stdio.h>
int main()
{
// the maximum value allowed in the int is 32,767
int x = 40000;
printf(x);
return 0;
}
Copy code

Output:

Segmentation Fault

What is a Segmentation Fault?

A segmentation fault occurs when your program tries to access an area of memory that is not allowed. In other words, this error is thrown when the program tries to access memory beyond the allocated space for the specific data type.

Derived Data Types

Derived data types are primary data types that are grouped together. You can group many elements of similar data types. These data types are defined by the user. The following are the derived data types in C:

  • Array
  • Pointers
  • Structure
  • Union

Array

An array in C is a collection of multiple values of a similar data type and is stored in a contiguous memory location. An array can consist of chars, integers, doubles, etc.

Declaration of Array in C

data_type array_name[array_size];

C Array Example:

#include<stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
clrscr();
marks[0]=50;//initialization of array
marks[1]=60;
marks[2]=75;
marks[3]=40;
marks[4]=85; //traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]); }
return 0;
}
Copy code

Output

50

60

75
40
85

Pointer Data Type

The pointer data type is used to store the address of another variable. A pointer can store the address of variables of any data type. Pointers allow users to perform dynamic memory allocation. They also help to pass variables by reference.

A pointer with no address is called a null pointer. A pointer with no data type is a void Pointer. It is defined by using a ‘*’ operator.

Example – Program to illustrate Pointer

int main(void) {
int *ptr1;
int *ptr2;
int a = 5;
int b = 10;
//address of a is assigned to ptr1
ptr1 = &a;
//address of b is assigned to ptr2
ptr2 = &b;
//display value of a and b
printf("%d", *ptr1); //prints 5
printf("\n%d", *ptr2); //prints 10
//print address of a and b
printf("\n%d", ptr1); // prints address
printf("\n%d", ptr2); // prints address
//pointer subtraction
int minus = ptr2 - ptr1;
printf("\n%d", minus); //prints the difference
return 0; }
Copy code

Also Read: Introduction to Python Data Types with Examples

Structure

It is a data type that can store variables of similar or different data types. For example, we can use structures to store information about an employee, such as the employee’s name, employee ID, salary, and more. Each employee’s record will be represented by an object of the structure. The size of the structure is the sum of the storage size required by each variable. The ‘struct’ keyword defines a structure.

Example – Program to illustrate Structure

#include <stdio.h>
#include <string.h>
struct Employee { char name[50];
int emp_id;
float salary; } employee1;
int main() {
strcpy(employee1.name, "John");
employee1.emp_id = 1779;
employee1. salary = 3900;
printf("Name: %s\n", employee1.name);
printf("Employee ID: %d\n", employee1.emp_id);
printf("Salary: %.2f", employee1.salary);
return 0; }
Copy code

Output

Name: John  

Employee ID: 1779

Salary: 3900.00

Also Read: Top C Programming Interview Questions and Answers

Union

A union is a group of elements with similar or different data types. In a union, the memory location is the same for all the elements. Its size will be equal to the memory required for the largest data type defined. We use the keyword ‘union’ to define a union. You can declare many variables. However, just one variable can store the value at a time.

Example – Defining a Union

union Student{
int id;
char name[20];
float marks[5];
}
st1, st2;
Copy code

Enumerated Data Types

Enumerated data types are user-defined data types that consist of integer values. They are used to define variables that can only assign certain discrete integer values in the program. They are used to make a program more readable, flexible, and maintainable. We use the keyword ‘enum’ to declare new enumeration types in the C programming language.

Enum syntax:

enum flag {const1, const2, const3………};

A popular example of enumerated data types is the days of the week.

Example – Program to illustrate Enumeration

#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Fri;
printf("%d",day);
return 0;
}
Copy code

Output

4

Check out the top Programming Courses

Void

The void is just an empty data type that depicts that no value is available. Typically, the void is used for functions. When we declare a function as void, it doesn’t have to return anything.

Void is used in three situations:

  • Function returns as void – A function with no return value will have the return type as void.
  • Function arguments as void – A function with no parameter can accept the void.
  • Pointers to void – It represents the address of an object, but not its type.

Example – The below function will not return any value to the calling function.

void sum (int a, int b);









No comments:

Post a Comment