Difference between fundamental data types and derived data types
Last Updated :
25 Mar, 2022
In computer programming, data type is a classification that specifies to compiler or interpreter which type of data user is intending to use.
There are two types of data types –
- Primitive/Fundamental data type: Each variable in C/C++ has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it.
Example of fundamental data types –
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 2;
float b = 2.0;
double c = 2.0003;
char d = 'D' ;
cout<< "Integer value is = " << a
<< "\nFloat value is = " << b
<< "\nDouble value is = " << c
<< "\nChar value is = " << d <<endl;
}
|
Java
class GFG {
public static void main(String[] args)
{
int a = 2 ;
float b = 2 .0f;
double c = 2.0003 ;
char d = 'D' ;
System.out.printf( "Integer value is = %d" , a);
System.out.printf( "\nFloat value is = %f" , b);
System.out.printf( "\nDouble value is = %f" , c);
System.out.printf( "\nChar value is = %c" , d);
}
}
|
Python
a = 2
b = 2.0
c = 2.0003
d = 'D'
print ( "Integer value is = " , a);
print ( "\nFloat value is = " , b);
print ( "\nDouble value is = " , c);
print ( "\nChar value is = " , d);
|
C#
using System;
class GFG {
public static void Main()
{
int a = 2;
float b = 2.0f;
double c = 2.0003;
char d = 'D' ;
Console.WriteLine( "Integer value is = " + a);
Console.WriteLine( "\nFloat value is = " + b);
Console.WriteLine( "\nDouble value is = " + c);
Console.WriteLine( "\nChar value is = " + d);
}
}
|
PHP
<?php
{
$a = 2;
$b = 2.0;
$c = 2.0003;
$d = 'D' ;
echo ( "Integer value is = " . $a );
echo ( "\nFloat value is = " . $b );
echo ( "\nDouble value is = " . $c );
echo ( "\nChar value is = " . $d );
}
|
Javascript
<script>
var a = 2;
var b = 2.0;
var c = 2.0003;
var d = 'D' ;
document.write( "Integer value is = " , a + "<br>" );
document.write( "\nFloat value is = " , b + "<br>" );
document.write( "\nDouble value is = " , c + "<br>" );
document.write( "\nChar value is = " , d + "<br>" );
</script>
|
Output:
Integer value is = 2
Float value is = 2.000000
Double value is = 2.000300
Char value is = D
C++
#include <iostream>
using namespace std;
int main()
{
int variable = 10;
int * pointr;
pointr = &variable;
cout << "Value of variable = " << variable;
cout << "\nValue at *pointer = " << *pointr;
return 0;
}
|
C
#include <stdio.h>
int main()
{
int variable = 10;
int * pointr;
pointr = &variable;
printf ( "Value of variable = %d" , variable);
printf ( "\nValue at *pointer = %d" , *pointr);
return 0;
}
|
Output:
Value of variable = 10
Value at *pointer = 10
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int i;
for (i = 0; i < 5; i++)
cout << ( "%d " , a[i]);
return 0;
}
|
C
#include <stdio.h>
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int i;
for (i = 0; i < 5; i++)
printf ( "%d " , a[i]);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
int a[] = { 1 , 2 , 3 , 4 , 5 };
int i;
for (i = 0 ; i < 5 ; i++)
System.out.printf( "%d " , a[i]);
}
}
|
Python3
a = [ 1 , 2 , 3 , 4 , 5 ];
for i in range ( 5 ):
print (a[i], end = " " );
|
C#
using System;
class GFG {
public static void Main(String[] args)
{
int [] a = { 1, 2, 3, 4, 5 };
int i;
for (i = 0; i < 5; i++)
Console.Write( "{0} " , a[i]);
}
}
|
PHP
<?php
$a = array (1, 2, 3, 4, 5);
for ( $i = 0; $i < 5; $i ++)
print ( $a [ $i ] . " " );
?>
|
Javascript
<script>
let a = [ 1, 2, 3, 4, 5 ];
let i;
for (i = 0; i < 5; i++)
document.write(a[i] + " " );
</script>
|
C++
#include <bits/stdc++.h>
using namespace std;
struct structure_example
{
int integer;
float decimal;
char character[20];
};
int main()
{
struct structure_example s = { 15, 98.9, "geeksforgeeks" };
cout << "Structure data -" << endl;
cout << "integer = " << s.integer << endl;
cout << fixed<<setprecision(6)<< "decimal = " << s.decimal << endl;
cout << "name = " << s.character << endl;
return 0;
}
|
C
#include <stdio.h>
struct structure_example {
int integer;
float decimal;
char character[20];
};
void main()
{
struct structure_example s = { 15, 98.9, "geeksforgeeks" };
printf ("Structure data - \n integer = %d \n decimal =
%f \n name = %s", s.integer, s.decimal, s.character);
}
|
Output:
Structure data -
integer = 15
decimal = 98.900002
name = geeksforgeeks
Fundamental Data Types |
Derived Data Types |
Fundamental data type is also called primitive data type. These are the basic data types. |
Derived data type is the aggregation of fundamental data type. |
character, integer, float, and void are fundamental data types. |
Pointers, arrays, structures and unions are derived data types. |
Character is used for characters. It can be classified as char, Signed char, Unsigned char. |
Pointers are used for storing address of variables. |
Integer is used for integers( not having decimal digits). It can be classified as signed and unsigned. Further classified as int, short int and long int. |
Array is used to contain similar type of data. |
float is used for decimal numbers. These are classified as float, double and long double. |
structure is used to group items of possibly different types into a single type. |
void is used where there is no return value required. |
It is like structure but all members in union share the same memory location |
Share your thoughts in the comments
Please Login to comment...