Comments are pivotal for C++ and every other programming language. They allow you to leave remarks to yourself and anyone else who may read your code. Without them, you may be wondering why you did something that looks perculiar to you. They are also good to see how your program would work without a line of code. There are two types of comments, single line and multiline. A single line begins with // and will continue from that point until the end of that line to be a comment. Multiline comments start with a /* and end with */. Comments are always ignored by the compiler and will not affect your program in any way.

// This is a single line comment



In C++ before you use a variable, you must declare it. To declare a variable you must first decide of which type it will be. The range of the variable will vary depending on your system:
Name Description Range
char Character -128 to 127 or 0 to 255
int Integer -2147483648 to 2147483647 or 0 to 4294967295
short int
Short Integer -32768 to 32767 or 0 to 65535
long int
Long integer -2147483648 to 2147483647 or
0 to 4294967295
boolTrue or False true or false
float Floating point number 7 digits
double Double floating point number 15 digits
long double Long double floating point number 15 digits
wchar_t Wide character 1 wide character

Look at the char data type. The reason it has numbers listed for the range is because all ascii characters are numbers. Say you wanted the letter A, it could be represented as the char A or as a 65 because 65 is the equivalent to A. They go in order from there, 65 is A, 66 is B, etc. A char is not just for printable characters, some won't be able to be printed on the screen. Normally you won't need to know the whole ASCII chart, but as long as you know that not all ASCII characters can be printed, then you should be okay.

String is not a built in data type, but actually consists of zero or more characters put together. If we need to we can add the string data type by including the string library up at the top where we include the iostream:

#include <string>

Now that we have all the data types that are commonly used, let's make some variables. The first thing you'll want to do is to declare the variable. First pick a name for your variable, remember to give it a meaningful name, no spaces in the variable name, no special characters just letters and numbers, and the first character has to be a letter. Also remember that variables can only hold one value at a time. Now that we have selected the name and the datatype let's put it to use:

double x;
//This will only work if you have the #include <string> at the top
string name;

If you do not declare a variable before you use it, you will get an error. If you do not initialize a variable (set a default value to it) and use it then you will get garbage. What will be in your variable is what is left over from somewhere else on your machine. Take note of the semicolons. Sometimes the error messages will be specific and say expected semicolon, sometimes they will not and it might take you hours to figure out that all you were missing is a simple semicolon.


Say you wanted to write a program that had the salestax variable to 6%. You wouldn't want that to ever change, so you could make that a constant. A constant works the same way as a variable, but it uses the keyword const before the data type and must be set in the same line of code.

const double salestax = .06;


In order to make variables appealing, we need to be able to do mathematic operations in them. In programming the computer does mathematical operations in the order of operations (PEMDAS). The operators are part of the C++ language, and there are many more inside of the math and cmath libraries.

%Modulus Division
+=Variable is equal to itself plus another
-=Variable is equal to itself minues another
*=Variable is equal to itself times another
/=Variable is equal to itself divided by another
++Variable is equal to itself plus 1
--Variable is equal to itself minus 1

Modulus division is taking a number dividing it by the number after the modulus sign (%) and then taking the remainder of that would be modulus. In other words 56 % 10 would be 6 because 50 56 divided by 10 if 5 remainder 6. You'll see more of this in later sections dealing with functions.

It's easy to use these other operators in a variable:

const double salestax = .06;//defines a constant and sets salestax to .06
int x = 1; //Sets x to 1
int y, f; //Declares y and f
char grade='D'; //Declares grade and sets it to D
string name; //declares a string called name
name="Bob"//Sets name to Bob
y = f = 0; //Sets y and f to 0
double t = 2.5 * 2;   //Sets t to 5
int c = 0; //Makes c equal to 0
c++; // Makes c equal to 1
c--; //Makes c equal to 0
c--; //Makes c equal to -1
++c; //Makes c equal to 0
--c; //Makes c equal to -1

By now you should have an understanding of how variables work. A few things I need to point out is that the variable c can only be declare with the int statement oncein that block of code. What I mean by that is that if you declare a variable c in an if statement then that c is native to that if statement and cannot be seen anywhere else in your code. Every time you declare something in a block of code, you can't declare it again, you must use it withour the leading data type. Also notice the variable called name. When setting a string variable you must enclose it in quotation marks ("). For chars we need to use single quotation marks ('), otherwise it will not work If you don't have a good idea on how they work then reread the tutorial. In the next section we will be using the variables to print out data and take input from a user.

Next Section: Input and Output

Link Directory | link directory | GeekySpeaky: Submit Your Site! | Blog Matter

Copyright © 2004-2018 Sean Noble, All rights reserved
Read our Disclosure Policy