Be a Supporter!

C++: Datatype and Variables basics

  • 1,616 Views
  • 12 Replies
New Topic Respond to this Topic
Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
C++: Datatype and Variables basics Feb. 20th, 2006 @ 07:03 PM Reply

C++: Main. Booya!

Introduction to variables and datatypes

Let's go over some of the datatypes of C++.

bool- Evaluate to either "true" or "false," and take up 1 byte of memory

int- These represent any whole number,no decimals allowed, and take up 4 bytes of memory. Can handle then numbers -2,147,483,648 to 2,147,483,647.

char- These represent any character on a keyboard. Characters must be placed in single quotations, like these. 'A' '~' '7' and take up 1 byte of memory.

short- These are like Int, but take up only 2 bytes. Can handle the numbers -32,768 to 32, 767.

long- These represent ...ah... don't bother with these....just use int.

float- These represent decimal characters, and take up 4 bytes of memory. Can handle any number from +-(1.2 x 10^-38 to 3.4 x 10^38)

double- Like a float, but take up 8 bytes of memory. Can handle any number from +-(2.3 x 10^-308 to -1.7 x 10^308)

string- holds a string of characters that form what we like to call "words." You must have #include<string> in your header in order to store strings into variables.

Examples of the above:

bool = true
int = 25
char = 'B'
short = 120
float = -54.67
double = 129875647212309.17
string = "what in the world!?"

Now, with that out of the way, we move onto variables.

Remember algebra, when you had those crappy questions like 3y - 9 = 12 and you had to figure out what "y" was equal too? Well, "y" was the variable. Well... what exactly is y equal too? In the given expression, "y" would be equal to 7... because 3*7-9 = 12. So, we can say the following;

y = 7

Now, whenever we use the variable "y" the program will instantly know that we mean "7."

In C++ however, we must tell the program which type of data is represented by the variable, such as int, double or char. We must also be careful how we name our variables. For example; Names MUST start with either a letter or an underscore character, you cannot use C++ keywords as variable names and C++ is case sensitive! Here are some examples of how to properly initialize variables.

int _number = 10; //creates a variable of int type, named number with a value of 10
int cash = 40; //creates a variable of int type, named cash with a value of 40.
string name123 = "johnny"; //creates a variable of string type, named name, with a value of johnny
char firstLetter = 'j'; //creates a variable of char type, named firstletter, with a value of j
double money = 12.2543; //creates a variable of double type, named money, with a value of 12.25.

*remember, we need to put the semi-colon at the end of the declaration so that C++ knows we're done with our statement.

If you grow tired and weary of typing the same datatype declaration over and over, you can use typedef to change it, such as doing something like...

typedef double d; //now C++ recognizes "d" as double, so you can type d name = 100.12.

Now to try out some stuff.

Open up C++ and include all of the appropriate header files and namespace, if you forget, here it is..

#include <iostream>
#include <string>

using namespace std;

void main()
{
int cash = 20;
string name = "Johnny";

cout << name+" only has ";
cout << cash;
cout << " dollars in his checking account."<<endl;
}

Mess around with the variables a bit and try to get a feel for them using the statements. As homework, create a blank cash sum and name, like this...

int cash;
string name;

then "ask" the user to enter in an amount using a cin statement, and then display their name and the amount they have to the console window.

Random notes;

*NOTE- If you only want to ONLY allow positive numbers, you can put "unsigned" before the datatype, such as
unsigned int cash = 40;

*NOTE- If an integer goes past it's max number, it wraps back to the beginning.

*NOTE- Knowing the amount of memory a variable takes up is important for using a function called sizeOf() which you'll learn about later, which basically parses through information based on it's size.


Perpetually looking for time to return to the arts.

BBS Signature
Khao
Khao
  • Member since: Sep. 20, 2003
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to C++: Datatype and Variables basics Feb. 20th, 2006 @ 08:27 PM Reply

very useful =)
and happy 1000th post johnny ;)

Kings-Cant-Fall
Kings-Cant-Fall
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to C++: Datatype and Variables basics Feb. 20th, 2006 @ 08:48 PM Reply

Awww I haven't had a chance to flame your ass yet:)

Nice tutorial, it will make a good addition!

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to C++: Datatype and Variables basics Feb. 20th, 2006 @ 10:36 PM Reply

When you specify the range an int datatype can hold you may want to go into explaining how that is so with binary and what not. Maybe you could include that in another tutorial.

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to C++: Datatype and Variables basics Feb. 23rd, 2006 @ 08:51 AM Reply

if you want really large numbers you can use __int64 (2 underscores) as a type but then a lot of the math functions turn up errors cause they dont work with that type.

Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to C++: Datatype and Variables basics Feb. 24th, 2006 @ 12:09 PM Reply

At 2/23/06 08:51 AM, Glaiel_Gamer wrote: if you want really large numbers you can use __int64 (2 underscores) as a type but then a lot of the math functions turn up errors cause they dont work with that type.

Do you have to include a certain header filet to use that Glaiel?


Perpetually looking for time to return to the arts.

BBS Signature
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to C++: Datatype and Variables basics Feb. 24th, 2006 @ 08:17 PM Reply

At 2/24/06 12:09 PM, Johnny_Krysys wrote:
At 2/23/06 08:51 AM, Glaiel_Gamer wrote: if you want really large numbers you can use __int64 (2 underscores) as a type but then a lot of the math functions turn up errors cause they dont work with that type.
Do you have to include a certain header filet to use that Glaiel?

no, and actually __int64 is really like an integer version of double

wolverin38
wolverin38
  • Member since: Dec. 7, 2002
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to C++: Datatype and Variables basics Feb. 24th, 2006 @ 08:24 PM Reply

WOW man!! you really know your stuff (not to say other things) very helpfullll..... thanks man..

yori-nx
yori-nx
  • Member since: Nov. 9, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to C++: Datatype and Variables basics Feb. 27th, 2006 @ 10:33 PM Reply

bool- Evaluate to either "true" or "false," and take up 1 byte of memory

Sorry to nitpick, but doesn't a bool take up 1 bit of memory?

Cool tutorial. It will serve as a very handy reference to people.

mike
mike
  • Member since: Feb. 24, 2000
  • Offline.
Forum Stats
Member
Level 20
Programmer
Response to C++: Datatype and Variables basics Feb. 27th, 2006 @ 11:01 PM Reply

Actually, a bool generally still takes up 1 byte of memory, even though logically it only takes up 1 bit -- the reason for this being is that computers generally allocate and access memory in terms of bytes and word size.

Cryzabey
Cryzabey
  • Member since: Jun. 27, 2006
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to C++: Datatype and Variables basics Jul. 27th, 2006 @ 11:01 AM Reply

When I ran the script it didn't stay open so i added:

cin.get();
return 0;

to the last 2 lines (I hope thats right);

and when i tried to add more than the variables already listed I got the following errors:

c:\docume~1\owner\mydocu~1\test3~1.cpp: In function `int main(...)':
c:\docume~1\owner\mydocu~1\test3~1.cpp:10: parse error before `='

No matter what code it is it won't let me add more that arn't either a string or an int


BBS Signature
CronoMan
CronoMan
  • Member since: Jul. 19, 2004
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to C++: Datatype and Variables basics Jul. 27th, 2006 @ 11:21 AM Reply

Great tutorial :)
but I have to correct some things, and add some other things :

At 2/20/06 07:03 PM, Johnny_Krysys wrote: int- These represent any whole number,no decimals allowed, and take up 4 bytes of memory. Can handle then numbers -2,147,483,648 to 2,147,483,647.

int can (at present time) be up to 8 bytes
it follows the operating systems bit-size
on a 32-bit processor, int is 32-bit, on a 64-bit processor, int is 64-bit
etc

short- These are like Int, but take up only 2 bytes. Can handle the numbers -32,768 to 32, 767.

the definition says "lesser or equal to int" whatever that means :P not a correction, just an addition

float- These represent decimal characters, and take up 4 bytes of memory. Can handle any number from +-(1.2 x 10^-38 to 3.4 x 10^38)

also follows architecture (though the standard says that it's supposed to be 80-bit (the size of the floating point register)

but of course, it's kinda hard to store 80-bit in a file, so I guess that's why they've done it like they have

double- Like a float, but take up 8 bytes of memory. Can handle any number from +-(2.3 x 10^-308 to -1.7 x 10^308)

same as float, 32-bit x2 etc, but should also be 80-bit according to the standard afaik (if it should've existed in the first place)
(greater or equal to float)

and about __int64, that's a microsoft specific keyword
(all functions are datatypes starting with __ means that it's not a part of the standard)

and due to the inefficency of storing 1 bit, boolean values are stored as bytes.
If you want to use bits, you'll have to use bitfields


"no sound in ass"

elbekko
elbekko
  • Member since: Jul. 23, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to C++: Datatype and Variables basics Jul. 27th, 2006 @ 12:51 PM Reply

Nice tutorial =)


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature