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

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

Topics to be covered

Variables or identifiers
Datatype
Declaration of variables
General rules of variables

Variables

Variables or identifiers are con…


This content originally appeared on DEV Community and was authored by Mehfila A Parkkulthil

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.

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.

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 .

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.

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 .

Boolean - Stores true or false values.

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

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 ;

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

int myVariable = 10 ;
float myotherVariable = 34.10 ;

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


This content originally appeared on DEV Community and was authored by Mehfila A Parkkulthil


Print Share Comment Cite Upload Translate Updates
APA

Mehfila A Parkkulthil | Sciencx (2025-01-03T05:28:55+00:00) Day 3: C++ language | Variables | Datatypes | Part-1. Retrieved from https://www.scien.cx/2025/01/03/day-3-c-language-variables-datatypes-part-1/

MLA
" » Day 3: C++ language | Variables | Datatypes | Part-1." Mehfila A Parkkulthil | Sciencx - Friday January 3, 2025, https://www.scien.cx/2025/01/03/day-3-c-language-variables-datatypes-part-1/
HARVARD
Mehfila A Parkkulthil | Sciencx Friday January 3, 2025 » Day 3: C++ language | Variables | Datatypes | Part-1., viewed ,<https://www.scien.cx/2025/01/03/day-3-c-language-variables-datatypes-part-1/>
VANCOUVER
Mehfila A Parkkulthil | Sciencx - » Day 3: C++ language | Variables | Datatypes | Part-1. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/03/day-3-c-language-variables-datatypes-part-1/
CHICAGO
" » Day 3: C++ language | Variables | Datatypes | Part-1." Mehfila A Parkkulthil | Sciencx - Accessed . https://www.scien.cx/2025/01/03/day-3-c-language-variables-datatypes-part-1/
IEEE
" » Day 3: C++ language | Variables | Datatypes | Part-1." Mehfila A Parkkulthil | Sciencx [Online]. Available: https://www.scien.cx/2025/01/03/day-3-c-language-variables-datatypes-part-1/. [Accessed: ]
rf:citation
» Day 3: C++ language | Variables | Datatypes | Part-1 | Mehfila A Parkkulthil | Sciencx | https://www.scien.cx/2025/01/03/day-3-c-language-variables-datatypes-part-1/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.