Translate

Variables in C++

A Variable is used to declare a piece of characters. Variables are VERY important. Here is an example of a Variable:
——————————————–
#include <iostream.h>
int main()
{
int number;
number=2;
cout<<”The number is “<<number;
return 0;
}
————
——————————–
  • int number;
The variable is int. Int stands for integer, I explained this earlier on in the basic Hello World Program I went through. We have declared a variable by typing int number.
number is the name of the variable. A Variable can be called anything as long as it isn’t a number or starts of with a number eg:
int 2222;
that is wrong ^
int 2hello;
that is wrong too ^
int he2l;
that is right ^
int hello;
that is right too ^
A variable can hold a certain type of data. It could be the user’s Input, It could be a specific number, It could be a specific letter or word. They’re are different types of Variables. Each do Different things, before I explain them, lets finish off with our Variable program.
So the name of our Variable is number. It is an int, in other words, integer. Integer means number(incase you didn’t know).
  • number=2;
We have now given the Variable number the value of 2.
cout<<”The number is “<<number;
We typed in some text, in this case, “The number is ”
After this we did <<number;
To print out a value being held by a variable onto the console, we must use the << operator again. So we end our speech:
cout<<”The number is ”
and then we type in:
<<number;
At runtime, the program will print out:
The number is 2
This is because the Variable number has been given the value of 2.
They’re are more Typed of variables, including:
————————————————-
long-This variable can hold from a range of
-2147483648 to 2147483647
————————————————-
short-This variable can hold from a range of
-32768 to 32767
————————————————-
char-This Variable can hold letters. Example, the word “hello”.
————————————————-
I have just showed you a few Variables and what they do, they’re are more variables like double, float, bool, unsigned Variables etc. They have been listed underneath this tutorial.
Now we have int, long and short. Why have 3 different variables? All 3 Variables are supposed to hold numbers anyway. So what’s the point?
Well, it’s all about the bit length. We should use the number short if we want the user to type in something less that approx. 31,000.Theyre is no point of using long as that will take up more Bytes.
  • Char
The char Variable can be used to print out words.
e.g.:
————————————-
#include <iostream.h>
int main()
{
char hello;
strcpy(hello,”Hello World”);
cout<<hello;
return 0;
}
————————————-
We have the strcpy function. It is used to have a char variable hold some text. We first type in strcpy, which stands for string copy(I explained about strings in out Hello World program).
  • strcpy(name)
We typed in the name of the char variable, in this case, “name”.
  • strcpy(name,”Hello World”);
We then typed in a comma and then The Sentence Hello World in speech marks. Now we have given the Variable name to hold the string “Hello World”.
we then printed it out by:
  • cout<<name;
this prints out the value that the variable “name” is holding.
Another way of typing in Variables is:
————————————-
#include <iostream.h>
int main()
{
int number=2;
cout<<”The number is “<<number;
return 0;
}
————————————-
Instead of typing in:
int number;
number=2;
we can do a shorter way which is:
int number=2;
Also, when declaring more than 1 variable of the same type, we can type:
int a , b, c;
Instead of:
int a;
int b;
int c;
We can also give them value the same way:
int a=2,b=4,c=5;

No comments:

Post a Comment