Logo

dev-resources.site

for different kinds of informations.

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

Published at
1/3/2025
Categories
dsa
cpp
vscode
coding
Author
mehfila_parkkulthil_23
Categories
4 categories in total
dsa
open
cpp
open
vscode
open
coding
open
Author
22 person written this
mehfila_parkkulthil_23
open
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

cpp Article's
30 articles in total
Favicon
C++ 1st code
Favicon
Windows 上 VSCode 的 C/C++ 延伸模組處理編碼的問題
Favicon
As 10 Linguagens de Programação mais velozes do mundo
Favicon
The 10 fastest programming languages in the world
Favicon
Concurrency in C++: Mitigating Risks
Favicon
C++ or Rust? I'd stick to my good old C++
Favicon
Day 7: Unlocking the Power of Loops in C++
Favicon
Application scenarios of QML and C++ hybrid programming
Favicon
Bag: A header-only, simplistic, C++20, zipping & unzipping library.
Favicon
ИСТОРИЯ .NET
Favicon
How to Build a Node.js Camera Addon and Using It for Image Processing
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
C++20 - s(uper)size
Favicon
5 ways of passing unique pointer to a function in C++
Favicon
Securing C++ iostream: Key Vulnerabilities and Mitigation Strategies
Favicon
Reading Text Files Like a Pro - C++
Favicon
Day 7 : C++ language | Comparison Operators
Favicon
Day 5: C++ language | Arithmetic Operators
Favicon
Day 6: C++ Language | Assignment operators
Favicon
Day 6: Learning Conditionals – The Building Blocks of Logic
Favicon
OpenMP Data-Sharing Clauses: Differences Explained
Favicon
How to correctly rotate an OBB ?
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
Day 3: C++ language | Variables | Datatypes | Part-1
Favicon
Bug of the week #2
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
Antivirus in C++
Favicon
Let’s review some code #1: C++
Favicon
Switch Case
Favicon
Recording of selected portion of screen in .MP4 format using C++ in RAD studio

Featured ones: