Skip to main content

Who Else Wants To Know The Mystery Behind C++ Variables?

 C++ Variables



Variables are containers for storing data values.


In C++, there are different types of variables (defined with different keywords), for example:


int - stores integers (whole numbers), without decimals, such as 123 or -123

double - stores floating point numbers, with decimals, such as 19.99 or -19.99

char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes

string - stores text, such as "Hello World". String values are surrounded by double quotes

bool - stores values with two states: true or false

Declaring (Creating) Variables

To create a variable, specify the type and assign it a value:


Syntax

type variableName = value;

Where type is one of C++ types (such as int), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign values to the variable.


To create a variable that should store a number, look at the following example:


Example

Create a variable called myNum of type int and assign it the value 15:


int myNum = 15;

cout << myNum;

You can also declare a variable without assigning the value, and assign the value later:


Example

int myNum;

myNum = 15;

cout << myNum;

Note that if you assign a new value to an existing variable, it will overwrite the previous value:


Example

int myNum = 15;  // myNum is 15

myNum = 10;  // Now myNum is 10

cout << myNum;  // Outputs 10


Comments

Popular posts from this blog

Who Else Wants To Know The Mystery Behind C++ Output ?

 C++ Output (Print Text) The cout object, together with the << operator, is used to output values/print text: for example ; #include <iostream> using namespace std; int main() {   cout << "Hello World!";   return 0; } Note: You can add as many cout objects as you want. However, note that it does not insert a new line at the end of the output: #include <iostream> using namespace std; int main() {   cout << "Hello World!";   cout << "I am learning C++";   return 0; }

Who Else Wants To Know The Mystery Behind C++ syntex?

C++ Syntax C++ Basic Syntax Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors -... Class − A class can be defined as a template/blueprint that describes the behaviors/states that object of its type. for example ; #include <iostream> int main() {   std::cout << "Hello World!";   return 0; } Example explained Line 1: #include <iostream> is a header file library that lets us work with input and output objects, such as cout (used in line 5). Header files add functionality to C++ programs. Line 2: using namespace std means that we can use names for objects and variables from the standard library. Don't worry if you don't understand how #include <iostream> and using namespace std works. Just think of it as something that (almost) always appears in your program. Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable. Line 4: Another thing that always app...

The Secret of Successful TOP MOBILE BRANDS #bbk electronics

Everything you should know about BBK Electronics, its founder, brands & products, and net worth. The name BBK Electronics might not sound common to many out there, but, if you’ve used an Oppo, Vivo, OnePlus, Realme, or iQOO smartphone before, then you’ve used a BBK Electronics product. What is BBK Electronics? BBK Electronics. It markets smartphones under the Realme, OPPO, Vivo and OnePlus brands, and Blu-ray players, headphones and headphone amplifiers under the OPPO Digital division. BBK Electronics: Meet the world’s 2nd largest smartphone manufacturer you’ve probably never heard. Apple and Samsung are undeniably the most popular smartphone brands today. Shipping almost 300 million units last 2019, Samsung easily beats the pack to become the world’s largest phone manufacturer. All of this is expected because everybody’s heard about Samsung, and everybody either owns a Samsung phone or knows somebody who does. But what do you know about BBK Electronics, the world’s second-largest ...