Heyo here! It's time to continue the cycle of blog posts about C++.
This time we'll get deeper in variables. Almost every program contains variables, so it's very important thing to learn.What are variables?
I'll try to explain this as simple as possible. I think almost everyone of you heard what a box is and have seen one of them. So, imagine that you have a box. You can adjust its size in any way you want. This box is your program. You can put another, smaller boxes in this box. These boxes are variables. You can also place something in these boxes. The thing you will place in the box is variable's value. Of course, you can also leave them empty.
Every programming language has its own way of telling the machine that you want to create some variable. I'll be telling on how C++ does it. For other programming languages it's different, but if you will get basic principle, you'll most likely understand it for every language.
Every variable has its type. Variable's type determines which values can it store and which it's not able to. Every variable also has its own name. You cannot name two variables using a same name because the name for each variable must always be unique
. If you have two boxes with name 'iron', you cannot just tell the machine: 'bring me iron box please'. It will ask which box you need. And to explain it accurately, you need to name each variable differently. Don't worry, that do not apply for variable types - you can have multiple variables of the same type.
There are only a couple of basic variable types. You need to know them well.
- int - allows to store a number without a fractional component.
Good values: 1, 8, 14, -5, 4312
Bad values: 1.25, 2.81, 9.75324
- float - allows to store a number with or without a fractional component. Mostly it's not good to store ints in a float-type variable because it takes much more memory to keep it then.
Good values: 4.235, 544.4325
Bad values: 1, 92, "hello"
- double - same as float, but the number fraction's precision is doubled. That's why this type has such a name. Though, again, it's not recommended to store ints or floats in double-type variable since it requires even more memory to be stored than float or int. Please use a good type for each variable. It's always good to know how much memory your program uses.
- bool - logic-typed data. It can have just two values - '0' or '1', 'true' or 'false'. It's really useful when you need to tell the function or the program what to do if something is true or false.
Good values: 0, 1, true, false
Bad values: 5, 14.432, "true", "false" (see the " sign carefully)
- char - symbol-typed data. This is the most tricky type of data from all five stated. You can store a symbol or an int in a char-typed variable but mind that the max bound of 128 (or 255 - we'll look through the 'type modifiers' later). If you go below a maximum, the engine will substract 255 from your number until it'll be less or equal to the max bound.
Good values: 'H', -12, 45
Not so good* values: "Hello", "hey im long", 24234123
Bad values: 473372.3213, true, false
* If you don't have a special option, the char will store only the first symbol of the string you enter as its supposed value. For example, only 'H' of "Hello" or only 'f' of "flowers". The int that exceeds the maximal value will be modified: the system will keep substracting 255 from it until the result will be less or equal to max bound.
Declaring your own variable
- pointer - this hardcoded thing is a link to another data. We'll get deeper in it a bit later, but for now you just should know that it exists and can be used.
In C++, declaration of your variables is very simple. It's done like that:type name
For example, we want a variable to store a number. Then we'll declare it like that:int myInteger
You can put any name instead of 'myInteger' - this part is free for you to be modified. Remember some rules of variable's naming:
- You can use the symbols from English alphabet and numbers in your variable's name.
- Don't put a number as a first symbol of your variable.
I'm telling these simple rules so we won't have any problems in future.How to set a value for declared variable
First, make sure that you have already declared your variable and you're setting its value below its declaration. If it's already checked, look on how to set the value to any variable:name
You expected something more complicated? Sorry then, it's much easier than you've expected. If your variable is, for example, named 'myVar' and you want to make its value equal to 1.
One more thing: in some cases when you declare your variable, you can set it the value instantly. In that case, the syntax of variable declaration will look slightly more complicated:type name
Using this way if you know the value you want this variable to take instantly, you'll make the code shorter and better-to-read than using a traditional way. This way you'll also will instantly see which value of variable was assigned instantly. Though, in some cases you cannot do the direct assigning, we'll talk about it later: these cases require a deeper understanding of C++.Type modifiers in C++
Now we'll get deeper inside the possibilities of variable types. Knowing them will allow you to modify them better and make them much more fitting and matching your needs.
There is more extended way to declare a variable than the default one. It looks like that:modifiers type name
There are modifiers
of variable's type. They are specified before the type and are changing this variable's properties. Look at some basic type modifiers which may come in handy:
- signed/unsigned: this modifier pair can be used for any type which stores numbers (from these types we already know char, int, float and double). By default (if you write no modifiers), these types are considered as signed. But you may specify unsigned modifier. I'll explain how that works.
Well, you probably know that numbers can be both positive and negative. And some memory is required to store them up. Variable's type also determines how much memory this variable is uses and, as a conclusion, which values it can handle. unsigned modifier denies the variable which stores the number to store negative values. For example, char can store values from -128 to 127 (zero is also a number, that's why it's not from -128 to 128). By writing unsigned char, you can store numbers from 0 to 255 in the variable of the same type. That's how we can use the same amount of memory to store slightly greater values than it's possible without the unsigned modifier. Remember this as a very useful thing for memory's economy.
- const modifier makes your variable read-only. Means you can assign its value only once and (much likely) you cannot use functions to specify the value - you need to define it directly (unless the function you use will return a const modified type aswell). This way you can just read its value without ability to change it.
- short/long is another type modifier. It modifies the amount of memory which a numeric variable take to store the value of variable. For example, int can store values from −2147483648 to 2147483647. What if we do not need that big amount of numbers? Correct! We'll use short modifier! As a special case, you may write just short instead of short int. But that's only in that case!
Well, I haven't dug up much about if there's a possibility to use short float or short double, but I think they can be used aswell.
long extends the memory which variable covers. So, long int can store much more than just int. There's also a possibility to use long twice, long long. This will double the effect of long's memory extension. But don't try to do it with short - you may end up in a really bad situation with your program.
That's all for this time. You can practice around and get some nice results.
Next time we'll uncover another interesting C++'s topic.
Have a question? Leave it in a comment and receive an answer!