Group Info Group Founded 7 Years ago 46 Members
3,772 Pageviews43 Watchers

Recent Journal Entries

Group Info

The united group for programmers, coders and other types of IT engineers.
Group
Founded 7 Years ago
Aug 27, 2014

Location
Global

Group Focus
Common Interest

46 Members
43 Watchers
3,772 Pageviews

Affiliates

:iconart4coding-official::iconprogrammers-unite::iconcomputer-geeks:

Favourites

This collection does not have any deviations yet!

Journal Writers

Persona - Box by PandaTheBox
Art by :iconpandathebox:


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.
  • 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.
Declaring your own variable

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:
  1. You can use the symbols from English alphabet and numbers in your variable's name.
  2. 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 = value;

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 = value;

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:
  1. 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.
  2. 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.
  3. 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!
Yours, Simplar
More Journal Entries

Admins

Founder


:iconsimplar:

Deviants

Comments


Add a Comment:
 
:iconchrisnoeth:
ChrisNoeth Featured By Owner Dec 29, 2017  Professional Digital Artist
Please check out my new game COLOR DEFENSE: www.colordefense.de
Happy New Year!

- Chris
Reply
:iconprecipitous120:
Precipitous120 Featured By Owner Oct 15, 2017
I want to put together a collaboration project

PROGRAMMERS UNITE!

It is actually quite disappointing how few programmers there are on DA. I mean is it so rare that someone is both an artist and a programmer? I consider programming art so surely the jump between the two isn't too far a leap that it isn't impossible.

I would like us to work on an artificial intelligence that can both help us to program as well as be a heck of a lot of fun creating. Best yet most of the code already exists, I just propose we add on to it.

It is not complicated but it does need a large group of programmers because it needs to have many functions as well as compatibility across all platforms.

Here is the link:

Calling all Computer ProgrammersFirst of all, Welcome!
I've recently discovered quite an interesting but pointless app you can download to your phone and computer, the shimeji. It is a little mascot that plays around on your desktop or your internet explorer, doing pointless but somewhat entertaining stuff. I was wondering, being the amazing people we are, if we could somehow turn this cute little app into something amazing and yet free. Using a collective of different programming languages we could turn the shimeji in to a little powerhouse of productivity. An intelligent entity that occupies your computer when you don't, not maliciously but, doing all it can to speed up the performance of your computer. Programmable by even the average user, a shimeji could given a list of tasks to complete while you are away. It would be the first Artificial intelligence with a face that acts much like 90% of the movie industries killer robots or computer software with a mind of its own. The 10% being the malicious intentions


I wouldn't mind joining your group too, I need the attention. You don't find much praise in the programming field because people just don't understand what you do. Strange if you do something as simple as kick a ball, you get far more attention then if you data mine the net or create a translator. Really quite frustrating being a coder and that is the reality of it. We can do such amazing things, but we will never truly get the recognition that we crave.
Reply
:iconcheetany:
Cheetany Featured By Owner Jul 20, 2017  Hobbyist Digital Artist
I make simple games with Action Script. Is it ok for me to join and submit them here? I'm not really much of a programmer though :P
Reply
:iconprecipitous120:
Precipitous120 Featured By Owner Oct 17, 2017
Join my group and you can submit your games there
Reply
:iconcheetany:
Cheetany Featured By Owner Oct 18, 2017  Hobbyist Digital Artist
Thank you!
Reply
:iconcable-bunny8:
Cable-Bunny8 Featured By Owner Mar 14, 2017  Hobbyist General Artist
is there any difference between programming a tablet and programming a computer? just curios on the subject.Wink/Razz 
Reply
:iconprecipitous120:
Precipitous120 Featured By Owner Oct 15, 2017
No not much. All you really need is one programming language and through very very very difficult implementation you can get it to work on any devise.

I managed to make my program universally work on all computer types, only after 4 years in the making :|

Not so much to do with the difficult but rather because I never thought of compatibility as an issue
Reply
:iconcable-bunny8:
Cable-Bunny8 Featured By Owner Oct 16, 2017  Hobbyist General Artist
Oh alright than thanks so much!~  ^-^
Reply
:iconprecipitous120:
Precipitous120 Featured By Owner Oct 16, 2017
You know I actually have my own programmers group.... and I'm looking for a spokes person to help me invite in new programmers

you know.... you could be that person
Reply
(1 Reply)
Add a Comment: