Description
Data types specify the different sizes and values that can be stored in the variable. It is used for declaring variables or functions of different types.
C/C++
-> C supports both signed and unsigned data type.
-> The memory size of the basic data types may change according to 32 or 64-bit
operating system.
-> There are following data type in c(32-bit).
Types | Data Types |
---|---|
Basic Data Type | int, char, float, double |
Derived Data Type | array, pointer, structure, union, Function type |
Enumeration Data Type | enum |
Void Data Type | void |
Data Type | Memory(Bytes) | Range | Format Specifier |
---|---|---|---|
char | 1 | -128 to 127 | %c |
signed char | 1 | -128 to 127 | %c |
unsigned char | 1 | 0 to 255 | %c |
short | 2 | -32,768 to 32,767 | %hd |
signed short | 2 | -32,768 to 32,767 | %hd |
unsigned short | 2 | 0 to 65,535 | %hu |
int | 2 | -32,768 to 32,767 | %d |
signed int | 2 | -32,768 to 32,767 | %d |
unsigned int | 2 | 0 to 65,535 | %u |
short int | 2 | -32,768 to 32,767 | %hd |
signed short int | 2 | -32,768 to 32,767 | %u |
unsigned short int | 2 | 0 to 65,535 | %hu |
long int | 4 | -2,147,483,648 to 2,147,483,647 | %ld |
signed long int | 4 | -2,147,483,648 to 2,147,483,647 | %ld |
unsigned long int | 4 | 0 to 4,294,967,295 | %lu |
float | 4 | 6 decimal places(Precision) | %f |
double | 8 | 15 decimal places(Precision) | %lf |
long double | 10 | %Lf |
-> Boolean data type contains two types of values 0 and 1.
0 represents false value and 1 represents true value.
To use bool data type add stdbool.h header file.
/* C Program to explain Data Type */
//Save it as DataTypeInC.c
#include<stdio.h>
#include<stdbool.h>
int main(){
bool booleanDataType=true;
printf("Boolean : %d\n",booleanDataType);
char charDataType='H';
printf("char : %c\n",charDataType);
int intDataType=100;
printf("int : %d\n",intDataType);
float floatDataType=15.85;
printf("float : %f",floatDataType);
return 0;
}
Output:
Boolean : 1
char : H
int : 100
float : 15.850000
Java
-> There are two types of data types in java.
1) Primitive data types: boolean, char, byte, short, int, long, float and double are primitive data types.
2) Non-primitive data types: Classes, Interfaces, and Arrays are non non-primitive data types.
-> There are following data type in -
Data Type | Default Value | Default size | Range |
---|---|---|---|
boolean | false | 1 bit | true,false |
char | ‘\u0000’ | 2 byte | 0 to 255 |
byte | 0 | 1 byte | -128 to 127 |
short | 0 | 2 byte | -32,768 to 32,767 |
int | 0 | 4 byte | -2,147,483,648 to 2,147,483, 647 |
long | 0L | 8 byte | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 0.0f | 4 byte | 6 to 7 significant decimal digits |
double | 0.0d | 8 byte | 15 significant decimal digits |
Note: Default value is for instance variable(Variable which are defined inside
the class but outside the method).
/* Java Program to explain Data Type */
//Save it as DataTypeInJava.java
import java.io.*;
public class DataTypeInJava {
static boolean booleanDataTypeDefault;
static boolean booleanDataTypeValue = true;
static char charDataTypeDefault;
static char charDataTypeValue = 'd';
static byte byteDataTypeDefault;
static byte byteDataTypeValue = 100;
static short shortDataTypeDefault;
static short shortDataTypeValue = 1320;
static int intDataTypeDefault;
static int intDataTypeValue = 150000020;
static long longDataTypeDefault;
static long longDataTypeValue = 15007800450000L;
static float floatDataTypeDefault;
static float floatDataTypeValue = 7.87f;
static double doubleDataTypeDefault;
static double doubleDataTypeValue = 74.800045;
static String stringDataTypeDefault;
static String stringDataTypeValue = "Assigned Value";
public static void main(String[] args) {
System.out.println("The default value of boolean : " + booleanDataTypeDefault);
System.out.println("The assigned value of boolean : " + booleanDataTypeValue);
System.out.println();
System.out.println("The default value of char : " + charDataTypeDefault);
System.out.println("The assigned value of char : " + charDataTypeValue);
System.out.println();
System.out.println("The default value of byte : " + byteDataTypeDefault);
System.out.println("The assigned value of byte : " + byteDataTypeValue);
System.out.println();
System.out.println("The default value of short : " + shortDataTypeDefault);
System.out.println("The assigned value of short : " + shortDataTypeValue);
System.out.println();
System.out.println("The default value of int : " + intDataTypeDefault);
System.out.println("The assigned value of int : " + intDataTypeValue);
System.out.println();
System.out.println("The default value of long : " + longDataTypeDefault);
System.out.println("The assigned value of long : " + longDataTypeValue);
System.out.println();
System.out.println("The default value of float : " + floatDataTypeDefault);
System.out.println("The assigned value of float : " + floatDataTypeValue);
System.out.println();
System.out.println("The default value of double : " + doubleDataTypeDefault);
System.out.println("The assigned value of double : " + doubleDataTypeValue);
System.out.println();
System.out.println("The default value of string : " + stringDataTypeDefault);
System.out.println("The assigned value of string : " + stringDataTypeValue);
}
}
Output:
The default value of boolean : false
The assigned value of boolean : true
The default value of char : \u0000
The assigned value of char : d
The default value of byte : 0
The assigned value of byte : 100
The default value of short : 0
The assigned value of short : 1320
The default value of int : 0
The assigned value of int : 150000020
The default value of long : 0
The assigned value of long : 15007800450000
The default value of float : 0.0
The assigned value of float : 7.87
The default value of double : 0.0
The assigned value of double : 74.800045
The default value of string : null
The assigned value of string : Assigned Value
Related Programs
1) Program to find modulus of two numbers2) Program to calculate power of a number
3) Program to calculate Grade of Student
4) Program to find the average of numbers in a given range
5) Program to Swap Two Numbers
6) Program to find sum of digits of a number
7) Program to reverse a number
8) Program to display multiplication table of a number
9) Program to Display Fibonacci Series using Recursion
10) Program to find factors of a Number