Skip to content Skip to sidebar Skip to footer

Variables in C++

You must’ve gone through various basics of C++, from basic structures to keywords, and you must’ve even covered the identifiers till now. But this is the very point where the real power of C++ unveils, and it’s the core backbone of any programming language because, without it, the programmer cannot even think of programming. They are Variables.

After such a heroic intro, one must be thinking about what the variables are? And where can I use them to make my code heroic?

It’s nothing, just creating a new/existing identifier and assigning it a value.

What do you mean fuc++? Wait, don’t get carried away. Let’s explain to you the variables of C++ in brief.

Variables: Definition & Declaration

Variables are the introduction and declaration of a value into the code.

They are a container to store any data within the data types to use in the codes and programs for developing logic for the code.

The value of the assigned variable is then stored in the memory spaces of the ram with the identifiers attached to it, which will help the program access the value whenever required.

Syntax

int x = 14;

Here, we can interpret that “int” means that the value of x shall be an integer value, therefore, defining the datatype. Other values will result in errors.

Moving forward with the int, “x” is the identifier used to identify and the assigned value of 14.

So this code is easy to interpret as it is straightforward.

In most cases, variables are easy to define and interpret.

Variables: Types

There are various types of variables that are super easy to learn as they are also straightforward to implement.

Variables can be of any type within the data types and their modifiers.

The type of the variables defines the value it’ll store.

For example

Integer will store an integer value.

int x = -13;

Double will store a decimal value.

double pi = 3.14;

String will store a whole string.

string str = "Even You're a geek now! My friend :)";

Boolean will store the secrets of true and false.

bool n = true;
// or
bool n = 1;
// as we know, or we'll get to know that 1 is true and 0 means false in boolean values and truth tables.

And so on; we can even use modifiers like short int, long long int, long double, unsigned int, etc.; as already specified, we can also use the modifiers to declare a variable.

Variables: Methods & Rules

We already know that variables are so flexible and easy to use, making our life much easier. But variables also have some rules necessary to follow to use them efficiently. Besides rules, we also have effective methods to create variables like camelcase and underscores, which programmers practice daily in their high-end codes. 

  • A Variable can only contain numbers, underscores, and characters irrespective of the case(uppercase/lowercase).
  • A Variable must begin with either an alphabet or an underscore. It can not start with a number or any other character.
  • We cannot give spaces between variable names as it makes them invalid. However, we can use underscores and camel cases to make it readable and more logical to humans (examples given below).
  • A variable cannot be the same as keywords and other reserved words in C++.
  • As C++ is a case-sensitive language, the variables with the same name but different cases are different. They can never be the same on their own.

Examples

//valid variables
int luck = 150;		// I've got it all covered.
double horoscope = 92.48392;	// Boosting overconfidence...
short int age = 6;		// Age's just a number.IYKYK👀.
bool SheLovesMe = false;	// Ah shux not again!
bool SheIsADigger = true;	// RIP My Savings and Money. Run!
char _She_is_cheating = 'Y';	// Hey Siri!, Tell me the tallest building near me.
int sed_lyf = 100;	//La la la la la la lala..... :(

Here the “SheLovesMe” and “SheIsADigger” are the ones written in camelcase, which means separating words using the case of the word’s first letter(usually).

//invalid variables
bool 5tarbucks = true;	
// as the variable is starting from the number this is invalid.
int return = 10; 	
// as the variable is same as keyword it's incorrect.
bool I am sad = true; 
// Bruup! Spaces are not allowed, control your emotions.

Case Sensitivity

Example

// Case Sensitvity
bool shelovesme = false;
bool SheLovesMe = true;

Here, the “shelovesme” variable is not the same as the “SheLovesMe” variable as they are both different and stored in different memory locations (We already know she doesn’t love you and loves your money as she’s a digger).

Conclusion

In this blog, we’ve learned about another essential basic of C++ programming, i.e., variables, what they are, how to use them, and how they make our life much easier. We’ve also got to know the rules and the best practices to create variables like none other (OH YEAH!). Besides, you’ve got some little bit of confidential advice too (you know what I mean), which you must follow if you want to get the true feeling and stop the flow of the “irrelevant expenses” from your pocket (advising with experience ahem ahem).

Similarly, you must also know that GeekonPeak has just begun and is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more comprehensive and easy-to-grasp courses for you (for free). Follow us on FacebookInstagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂

Leave a comment

Sign Up to Our Newsletter

Be the first to know the latest updates