C++ DATA TYPES | DATA TYPES IN C++ | My CS Tutorial
All variables use data types during declaration to restrict the type of data to be stored. whenever a variable is defined in C++ the compiler allocates memory for that variable based on the data type with which it is declared. every data type requires a different amount of memory.
Data types in C++ | My Cs Tutorial |
DATA TYPES IN C++
C ++ programming, a data type is a set of values and is set to work on those values. C/C++ provides different types of data types that allow the programmer to select the appropriate type of variable so that its value can be determined.Each variable of C/C++ has an associated data type. Different data types require different amount of storage and there are specific operations that can be done on it.Data types in C++ can be classified in following categories:-
(i) USER DEFINED DATA TYPE
structure
union
class
enumerated
(ii) PRIMITIVE OR BUILT- IN TYPE
integer :- int,char
void
floating type :- float,double
(iii) DERIVED DATA TYPE
array
function
pointer
reference
PRIMITIVE BUILT- IN TYPES
C++ offers the programmer a rich assortment of built-in as well as user defined data types.
Following table lists down seven basic C++ data types:-
TYPE KEYWORD
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t
Several of the basic types can be modified using one or more of these type modifiers:-
signed
unsigned
short
long
The following table shows the variable type, how much memory it takes to store the value in memory, and what is maximum and minimum value which can be stored in such type of variables.
Type Bit Width Typical Range
char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 127
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int Range 0 to 65,535
signed short int Range -32768 to 32767
long int 4bytes -2,147,483,647 to 2,147,483,647
signed long int 4bytes same as long int
unsigned long int 4bytes 0 to 4,294,967,295
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
USER DEFINED DATA TYPE
The data type that are defined by the user are call the user defined data types.
(a) Structure :- Structure is a user defined data types in C/C++ structure creates a data type that can be used to group items of possibly different types and to a single type.
syntax:-
struct address
{
char name[50];
char street[100];
char city[50];
int pin;
};
(b) Union :- Like structure, union is a user defined data types. In union all members share the same memory location, for example in the following program syntax both X and Y share the same location. If we change x we can see the changes being reflected in Y.
example:-
union test{
int X,Y;
};
(c) Class :- It is a user defined data type which holds its own data member and member function. Which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.
syntax:-
class class_name
{
access specifier;
data members;
member functions( )
};
(d) Enumerated -: An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each enumerator is a constant whose type is the enumeration.
Creating an enumeration requires the use of the keyword enum. The general form of an enumeration type is:-
enum enum-name { list of names } var-list;
DERIVED DATA TYPE
The data types that are derived from the primary or Buitl in data types are referred to as derived data types.
(a) Array :- An array is a collection of items stored at continuous memory location. The idea of array is to represent many instances in one variable.
syntax :-
data_type array_name[size_of_array];
int arr[5];
(b) Function :- Function is a block of code or programs segment that is defined to perform a specific well-defined task. A function is generally defined to save the user from writing the same lines of code again, and again for the same input. All the lines of code are put together inside a single function and this can be called anywhere required. Main() is a default function that is defined in every program of C++.
syntax :-
function_type function_name(parameters)
(c) Pointer :- Pointers are symbolic refresentation of addresses. They enable programs to simulate call by reference as well as to create and manipulate dynamic Data Structure. It is General declaration in C++ has the format.
syntax :-
data_type * var_name;
int *ptr;
(d) Reference :- When a variable is declared as reference, It becomes an alternative name for an assisting variable. A variable can be declared as reference by putting ' & ' in the declaration.
example :-
int main()
{
int x=10;
int& ref=x;
ref=20;
_______________________________________
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment