C/C++ for Hackers: Part 7 (Variables)

Part 7 (Variables)

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.

What Are Variables?

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!

Step 1: Fire Up Ubuntu and Open a Text Editor.

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".

Creating a Variable Without Specifying It's Value and Change a Variable's Value

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:

int a;

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!

Other Types of Variables

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

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.

usage example:

char a = 'F';

The String Variable

the string variable allows us to store multiple characters in it. a string is put between 2 double quotation marks.

usage example:

string bestWebsite = "Null Byte";

The Bool Variable

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.

usage example:

bool phoenixIsAwesome = true;

The Float Variable

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!



I think you should also mention, just for the sake of completeness, variable types like double, long, long long, long double, etc.

those are more of advanced variables. these are just the very basics


You forgot to add constants, but otherwise, great job.

yeah, i kind of made a mistake there. will cover constants and it's preprocessor equivalent after i did user input though.


looking forward for the next tutorial! Great job!

I think you can use : auto a=8;
This was new idea in c++11.
Only need to add *g++ yourtext.cpp -o youtext.out*
one line like this : *g++ -std=c++11 yourtext.cpp -o yourtext.out*.
Thank you for teaching.
Great job!

Share Your Thoughts

  • Hot
  • Latest