DEV Community

Mehfila A Parkkulthil
Mehfila A Parkkulthil

Posted on

Day 3: C++ language | Variables | Datatypes | Part-1

Please refer this Day 2: C++ language - output before you proceed .


Topics to be covered

  1. Variables or identifiers
  2. Datatype
  3. Declaration of variables
  4. General rules of variables

Variables

Variables or identifiers are containers for storing data values or a variable is a named storage location in memory that can hold a value. The value of a variable can be changed during the execution of a program.


Datatype

Data types specify the kind of data a variable can hold.The most common are integer(int) , float or double , character(char), string , boolean(bool) etc .

Integer - Stores whole numbers without decimal points.

Will be explained in detail later.
Imagine if 

int age = 13;
here int is the datatype and age is the variable and 13 is value.

Enter fullscreen mode Exit fullscreen mode

Float - Stores numbers with fractional parts, i.e., numbers with decimals.

float price = 22.34;
here float is the datatype and price is the variable and 22.34is value.

Enter fullscreen mode Exit fullscreen mode

Double -Similar to float but with double the precision. It can store larger and more precise numbers.

double distance = 12345.6789;
here double is the datatype and distance is the variable .

Enter fullscreen mode Exit fullscreen mode

Character-Stores a single character, usually within single quotes.

char initial = 'A';
here character is the datatype and initial is the variable 
and 'A' is the value.

Enter fullscreen mode Exit fullscreen mode

String- Stores text (sequences of characters).Strings should be stored in double quoted.

string greet = "Hello, World!";
here string is the datatype and greet is the variable .

Enter fullscreen mode Exit fullscreen mode

Boolean - Stores true or false values.

bool she_girl = True ;
here boolan is the datatype and she_girl is the variable .

Enter fullscreen mode Exit fullscreen mode

Variable declaration


Decalaration and initialisation of variables

Variable declaration is the way you tell a programming language to set aside a part of memory to store data. You do this by specifying the variable name and sometimes its type.

Variable initialization is the process of assigning an initial value to a variable at the time of its declaration

In C++, you have to specify the type of the variable when you declare it.

Example :

To initialise the variable should be declared.

Emaple1:

// Declaration
int myVariable; 

// Initialization
myVariable = 10; 



Example2:

// Declaration
float myotherVariable; 

// Initialization
myotherVariable = 34.10 ;

Enter fullscreen mode Exit fullscreen mode

It is okay to declare and initialise the variable in same line .

int myVariable = 10 ;
float myotherVariable = 34.10 ;

Enter fullscreen mode Exit fullscreen mode

initialisation and declaration

Or

Initialisation and declaration


Rules of Variables

  • All c++ variables must have unique names . These unique names are called identifiers.

  • Names can contain letters , digits and underscores.

  • Names must begin with letter or an underscore . Example : myvariable or _myage .

  • Names are case sensitive or C++ language is case sensitive ie, myvariable and MYVARIABLE or Myvariable are not same.

  • Names cannot contain white spaces or any special characters like ! , # , % etc.

  • Reserved names cannot be variables .(Reserved or keywords will be explained later).


Previous Blog

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay