How to Create a Variable in the C++ Programming Language

in cpp •  6 years ago 

We can create variables in C++ using the following syntax:

TYPE varName;

We declare the variable first with the data type, followed by the desired name. For example, if you want to store a person's age, you could do:

int age;

The left-hand side is the data type: an integer. The right-hand side is the variable name.

You can assign a value to a variable like so:

age = 35

Simply add the equal sign for the assignment operation, the value of the variable on the right-hand side. Note this is not a comparison.

You can declare your variable using other types, too. For instance, in the following examples we declare and define the values at the same time:

double circleArea = 3.123;

bool loading = true;

std::string name = "Roger";

A double is a floating point type that allows for the representation of numbers that have a fractional part.

A bool is a boolean type that has value that is either true or false.

A string is a sequence of characters. The string type belongs to the std namespace.

Here is a program for you to play with

For more lessons on software development, visit NBK Tech World:

https://www.youtube.com/channel/UC3pR7vTMNPNmSk99YIBKUnw

See you there!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @nbktechworld! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You got a First Vote

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemitBoard to support the german speaking community meetups
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @nbktechworld! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!