C/C++ for Hackers: Part 7 (Variables)
Welcome back, fellow hackers! in this part, we will be looking at what variables are, what types of variables exist, and how to create and use them.
variables are the building blocks of every software. a variable is basically a word that contains a value. we can for example make an integer variable, called a, and give it the value of 750, then when we use the variable "a", it will give the value of 750. it is easier to explain this once the code is made, so let's compile!
once that is done, paste this code in your main.cpp file and compile it again using g++.
when you run the a.out file, you can see that our program prints out 750.
when you run through the code i gave you, you can see that i made comment lines to explain what i was doing. but i'll explain a little more in detail. first, we created a variable called a, which is the type of "integer", and set it's value equal to 750. when we used cout and passed it the variable name, you can see it printed out the value of the variable, and not "a".
sometimes, you want to create your variable, but assign it a value later on. this can be perfectly done! just declare your variable like this:
your variable will be created, but doesn't hold a value yet.
you can change a variables value later on too. here is an example:
a = 10;
this will change the variable a to 10. our compiler already knows it is an integer, since we already declared it earlier, but now, the variable a will have the value of 10 from that line. you can try it out yourself using cout!
there are other types of variables too, but i will cover the basics. take note that these variables are all the same in C, except for the "string" variable, which doesn't exist in C. to use strings in C, you need to create an array of the char variable (i will cover arrays later).
we already covered integers, so i will skip those.
the char variable allows us to store a single ASCII character in it (i will cover the ASCII table later). a single character is put between 2 single quotation marks.
char a = 'F';
the string variable allows us to store multiple characters in it. a string is put between 2 double quotation marks.
string bestWebsite = "Null Byte";
the bool variable can either be the value "true" or the value "false" (1 or 0). the "true" and "false" logic is called the boolean logic, which drives every digital device that exists.
bool phoenixIsAwesome = true;
the float variable is just like an integer, except that we can use numbers after the comma with it. an example:
float pi = 3.14;
that is it for this tutorial folks! in the next tutorial, we will learn how to get input from the user.
if you have any questions or problems, feel free to comment or PM me!