Data Types and Variables in C/C++3 min read

 

DATA TYPES

Data types are the means to identify the type of data and associated operations.

Types of data types in C++

FUNDAMENTAL DATA TYPE

Different types of data types in c++

Apart from the considered data types there is one more most important type i.e, void.

Void

It is used for an empty set of values and as a return type for the non-returning function. It is also a fundamental data type.

String

It is used as the data type for characters which include space.

As , string name = “James Bond” ;

DERIVED DATA TYPE

We will discuss these types further in detail in coming lessons.

Bade Kaam ki baat (BKKB 1):-

Floating point numbers have 2 advantages over integers

  1. They can represent values between integers.
  2. They can represent a much greater range of values.

But floating point operation are usually slower than integer operation.

Bade Kaam ki baat (BKKB 2):

Double data type has 2 advantages over floating point

  1. They can represent values between integers.
  2. They can represent a much greater range of values.

But double occupies double memory than float. The type double is larger and slower than type float.

VARIABLES

Variables are the named storage locations, whose values can be manipulated during program run.
For example- To store names, roll no, marks of students we need storage locations that to named in order to distinguish them easily.

DECLARATION OF VARIABLES

Creating a variable of a particular data type is known as declaration of variable.

Syntax

type name; //where type represents data type and name is the variable name.

INITIALIZATION OF VARIABLES

A Variable with declared first value is said to be intialized variable.

Following are the forms of initialization:

  int num =12; 
  int num(12);  
  int num= int(12);
  In all cases num got it’s initial value as 12. 

DYNAMIC INITIALIZATION:-

It is defined as initialization of variables at run time using expressions at place of value.

 For example- 
float avg;      
avg=sum/count;         

Let’s see a program with use of data types and variables.

//program to accept a number and print its cube.
#include<iostream.h>
#include<conio.h> 
Void main()                          //data type for main function is void i.e, main is a non-returning function.
{  float b,c ;                         //creating two variable of float type
cout<<”enter the number\n”;
cin>>b;
c= b*b*b;                           //dynamic initialization
cout<<”\nThe cube of”<<b<<”is”<<c;
getch();                            //program doesn’t return any value
}

OUTPUT

enter the number
6
The cube of 6 is 216

FLOW OF STATEMENTS

In a program statements can be executed sequentially, selectively or iteratively.

SEQUENTIALLY

It means statements are executing in sequence. This represent default flow of control.

SELECTIVELY

It means statements are executing based on a condition test. If the condition evaluates to be true a set of a statement is followed, otherwise, another set of a statement is followed.

There are two types of selection statements:

  1. If statement
  2. Switch statement
Selectivity in C++

ITERATIVELY

According to it repetitive execution of statements take place depending upon a condition test. Execution is continued till the time condition is matched. It is also known as looping.

C++ provide three types of loops:

  1. For loop
  2. While loop
  3. Do-while loop

We will study them in next articles.

If you are loving my articles please folloew me on @priya123.

Bookmark(0)

Add a Comment

Your email address will not be published. Required fields are marked *