Saturday, 3 February 2018

Data Types And Variables In C++

Variable:
Variable means whose value can be changed.
Variable contains value which is stored in memory and holds some space.
Every Variable is assigned a datatype which shows size(2 byte,4 byte etc.) and type of data type(int,float etc)
2 steps to use variables
·        Variable Declaration
·        Variable Initialization

Variable Declaration:
Syntax: 
Datatype variablename;

Eg 1:
int x;
Where int is the data type and x is the variable name.

Eg 2:
float y;    
char ch;   
char name[20];
    Where float, char,string are the  data type, y,ch and name are the variables.

Variable Initialization:
Syntax:
Datatype variablename=value;

Eg 1:
int x=89;
Where int is the data type, x is the variable name and 89 is the value which assigned to x.

Eg 2:

float y=78.90;    
    char ch=’A’;    
   char name[20]=”LearningPoint92”;

Rules for defining variables
It can have alphabets, digits and underscore.
A variable name should start with alphabet and underscore only. It can't start with digit.
No white space is allowed within variable name.
A variable name can not be any reserved word or keyword e.g. double, float etc.

Valid variable declaration:
int x;    
     int _xy;    
      int x45;    
Invalid variable declaration:
 int 7;   
 int x y;  
 int float;  

Data Types:

Data types are used to store different values in the variable. There are four types of data types in c++.


  •  Basic (int ,float,char,double etc)  
  • Derived(array,pointer etc) 
  • Enumeration(enum)  
  • User Defined(Structure)
char
Data types
Size
Range
char
1 byte
-128 to 127
signed char
1 byte
-128 to 127
unsigned char
1 byte
0 to 127

short
Data types
Size
Range
short
2 byte
-32,768 to 32,767
signed short
2 byte
-32,768 to 32,767
unsigned short
2 byte
0 to 32,767

int
Data types
Size
Range
int
2 byte
-32,768 to 32,767
Signed int
2 byte
-32,768 to 32,767
Unsigned int
2 byte
0 to 32,767

short int
Data types
Size
Range
Short int
2 byte
-32,768 to 32,767
Signed short int
2 byte
-32,768 to 32,767
Unsigned short int
2 byte
0 to 32,767

long int
Data types
Size
long int
4 byte
Signed long int
4 byte
Unsigned long int
4 byte

float and double
Data types
Size
float
4 byte
double
8 byte
long double
10 byte


 

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...