Data Types

Data Types Nov. 28, 2020

Variables

Variables are the named memory locations.

For example, If we store a number 5342 in computer memory. The location of the memory, where 5342 is stored, is referred by a name a. Here a is a variable. 

Rules for variable names

  • Variable names in Visual C++ can range from 1 to 255 characters. To make variable names portable to other environments stay within a 1 to 31 character range.
  •  All variable names must begin with a letter of the alphabet or an
    underscore( _ ).  For beginning programmers, it may be easier to begin all variable names with a letter of the alphabet.
  •  After the first initial letter, variable names can also contain letters and numbers.  No spaces or special characters, however, are allowed.
  •  Uppercase characters are distinct from lowercase characters.  Using all uppercase letters is used primarily to identify constant variables. 
  •  You cannot use a  keyword (reserved word) as a variable name.

Data Types

Data Types are the classification of the data which is stored in the Variable eg. number and string. The classification can be based on there nature, functionalities or their memory capacity.


Data Types in C

C provides 4 basic data types: 

Data Type Memory (in Bytes) Range Format Specifier Type of Data Example
char 1 -128 to 127 %c Characters a,b,c,A,B,C,$,-,#,!,1,5,2,4,0
int 4 -215 to 215 -1 %d Integers  123, -4546,34245, 65453, -5764, 0
float 4 -215 to 215 -1 %f Floating Point Numbers (6 to 7 decimal digits) 465.546, -544.6525
double 8 -231 to 231 -1 %lf Floating Point Numbers (15 decimal digits) 465.54665464842, -544.652556451521
  • Memory specifies the total number of memory locations reserved by the variable to store the data. Each memory location is of 1 Byte and store 8-bit data i.e. 28 combinations can be made. Similarly, In 2 bytes 216 combinations can be made.
  • Range is the values of the combinations which can be stored in the variable. Char occupies 1 Byte of memory so 28  = 256 combinations are possible. Range is divided into equal number of negatives and positives. So there are (-128 to -1) 128 negative numbers and (0 to 127) 128 positive numbers. Similarly other ranges are also defined.
  • Format Specifier are used to tell the compiler datatype on input or output. 

Syntax

<datatype> <variable_name>;

<datatype> <variable_name>=<value>;

Example: 

int a;

int b=5;



Data Types in C++

C++ provides 7 basic data types (4 new datatypes from C): 

Data Type Memory (in Bytes) Range Format Specifier Type of Data Example
char 1 -128 to 127 %c Characters a, b, c, A, B, C, $, -, #, !, 1, 5, 2, 4, 0
int 4 -215 to 215 -1 %d Integers  123, -4546,34245, 65453, -5764, 0
float 4 -215 to 215 -1 %f Floating Point Numbers (6 to 7 decimal digits) 465.546, -544.6525
double 8 -231 to 231 -1 %lf Floating Point Numbers (15 decimal digits) 465.54665464842, -544.652556451521
wchar_t 2 -215 to 215 -1 %lc Characters Hebrew Characters
bool 1 0 to 1 %t Boolean Values true, false
void 1 0 %p Nothing / Empty  

Syntax

<datatype> <variable_name>;

<datatype> <variable_name>=<value>;

Example: 

int a;

int b=5;


Data Types in JAVA

C provides 8 basic data types: 

Data Type Memory (in Bytes) Range Format Specifier Type of Data Example
byte 1 -128 to 127 %c Integers 124, 0, -36
short 2 -215 to 215 -1 %d Integers  123, -4546, 34245, 65453, -5764, 0
int 4 -231 to 231 -1 %d Integers 45354345, -75454837
long 8 -263 to 263 -1 %lf Integers -556165616554, 654612184548
float 4 -231 to 231 -1 %lc Floating Point Numbers (6 to 7 decimal digits) 123165.5544, -435343.43534
double 8 -263 to 263 -1 %t Floating Point Numbers (15 decimal digits) 4651.6516266545465
boolean 1 0 %p Boolean Values  true, false
char 2 -215 to 215 -1 %c Characters a, b, c, A, B, C, $, -, #, !, 1, 5, 2, 4, 0

Syntax

<datatype> <variable_name>;

<datatype> <variable_name>=<value>;

Example: 

int a;

int b=5;


Data Types in Python

Python provides 14 basic data types: 

Data Type Type of Data Example Declaration
str String 'hello' 'value'
int Integers  123, -4546,34245, 65453, -5764, 0 value
float Floating Point Numbers 465.546, -544.6525 value
complex Complex Numbers 12 + 2j, 45 - 8j value
list List of other variables [45, 'abc', 45 - 2j, True, 45.2] [value , value , value , ...]
tuple Pair of variables (45,'abc') (value, value)
range Range of the Integers range(5)  
dict Dictionary (key-value pairs) {1:'one',2:'two'} {value:value, value:value, ...}
set Set (non indexed and unique values list) {5, 78, 'abc'} {value, value, ...}
frozenset non mutable set {3, 5433, 'djflkf'}  
bool Boolean Values True, False True, False
bytes Bytes Object b'hello', b'12da' b'...'
bytearray Mutable Bytes bytearray(4)  
memoryview Memory View Object of bytes / bytearray data memoryview(b'454')  

Syntax

<variable_name> <datatype>(<values>)

Variables can also be declared by the functions of the datatype. 

eg. str(), int(), frozenset(), bytearray(), memoryview()

a = 5
b = int(5)
print(type(a))
print(type(b))

Immutable DataTypes: (int, float, bool, str, tuple, bytes, frozenset, complex, range, memoryview)

The values of the immutable variables can't be modified. The variable can be reinitialized at another location. 

eg. int is immutable so if we try to change the value of int variable, the variable would be reinitialized at another location. 

n = 5
print(id(n))
n = n+4
print((id(n))

In above example the id of the variable is changed i.e. the variable is reinitialized but not modified.

Mutable DataTypes: (list, set, dict, bytearray)

The values of the mutable variables can be modified. 

eg. list is immutable so if we try to change the value of list variable, the variable would be updated. 

n = [5]
print(id(n))
n = n+[3]
print((id(n))

In above example the id of the variable is not changed i.e. the variable is modified. 


Data Types in JavaScript

JavaScript provides 2 type of data types: 

  1. Primitive Data Types (5)
  2. Non-primitive (Reference Data Types) (3)
  Primitive Data Types  
Data Type Type of Data Example
String String "Hello World!"
Number Numeric Values 123, -4546.34245, 65453, -5764, 0
Boolean Boolean Values true, false
Undefined undefined value undefined
Null Null i.e. No value at all Null
  Non - Primitive Data Types (Mutable Data Types)  
Object instance through which we can access members {1:'one'}
Array Group of Similar Variables [1,2,3,4,5]
RegExp Regular Expression codever.*/i

Syntax

var/let/const <variable_name>;

var/let/const <variable_name>=<value>;

Example: 

var a;

a= 10;

let b='hello';

const PI='3.14';


 


Data Types in PHP

PHP provides 8 basic data types: 

Data Type Type of Data Example
String String 'Hello'
Integer Integers  123, -4546,34245, 65453, -5764, 0
Float Floating Point Numbers 465.546, -544.6525
Boolean Boolean Values True, False
Array Group of similar datatype variables [1,2,4]
Object Instance of a class ('1','student','cse')
Null Null  Null
Resource Resource of external PHP  
  • The variable name must start with $.

Syntax

<variable_name> = <value>;

Example:

a=5;


 


Rate this post

Comments