Sign in to follow this  
FinalShot

Last few easy questions!

Recommended Posts

FinalShot    130
Well I have just finished Chapter 1 on Beginning C++ Game Programming. Most things are nice and clear to me apart from some.... Basically what are: string varibles (In c++ terms, I have a faint term but want it perfect) And float and double, these two terms are used for single and double precision floating numbers as the book says, but what the hell does Mike mean :) After this I am ready for Chapter 2 :) Thanks, Final

Share this post


Link to post
Share on other sites
Mushu    1396
string is a variable which contains a sequence of characters (letters), like a word.

A variable can be thought of as a container for data, like a box that holds a number, or a word, or something.

Floating-point precision means the variable can store 32 bits of information, or, unsigned numbers up to 2^32. A double is 64, so unsigned numbers up to 2^64 (iirc)

Share this post


Link to post
Share on other sites
deathwearer    367
Quote:
Original post by Mushu
Floating-point precision means the variable can store 32 bits of information, or, unsigned numbers up to 2^32. A double is 64, so unsigned numbers up to 2^64 (iirc)


Please correct me if i am wrong but

An int is a 16bit variable. The first bit is used to define if it's a positiv or negative variable. It's called the most significant bit if i remember corectly

An int can go from -32768 to +32768.

Sometime you can predict that you won't stock a negativ integger in your variable so you can make it an unsigned int, which tell the compilator to use the first bit to increase the "plage" of your variable. So with an unsigned int you can now go from 0 to 65535.

but the int stay at 16bit.




Share this post


Link to post
Share on other sites
demonkoryu    980
Quote:
Original post by deathwearer
An int is a 16bit variable.


An int has the standard machine word size of the target processor. For 16-bit CPUs like the 286, this is 16 bit. On 386 and newer machines, an int is a 32 bit large.

A short int (or simply short) is 16 bit, and a long int (long) is 32 bit.

For float, you might want to read up about it here.

Quote:

Q: What is the range of real numbers in VC++?
A:
float (4 bytes) : 1.175494351E-38 to 3.402823466E+38, significant decimal digits: 6
double (8 bytes) : 2.2250738585072014E-308 to 1.7976931348623158E+308, significant decimal digits: 15
real*10/long double (10 bytes) : 3.37E-4932 to 1.18E+4932, significant decimal digits: 19

Share this post


Link to post
Share on other sites
Sr_Guapo    876
Quote:
Original post by deathwearer
Quote:
Original post by Mushu
Floating-point precision means the variable can store 32 bits of information, or, unsigned numbers up to 2^32. A double is 64, so unsigned numbers up to 2^64 (iirc)


Please correct me if i am wrong but

An int is a 16bit variable. The first bit is used to define if it's a positiv or negative variable. It's called the most significant bit if i remember corectly

An int can go from -32768 to +32768.

Sometime you can predict that you won't stock a negativ integger in your variable so you can make it an unsigned int, which tell the compilator to use the first bit to increase the "plage" of your variable. So with an unsigned int you can now go from 0 to 65535.

but the int stay at 16bit.


Most labguages (all except VB6 IIRC) use the standard 32 bit integers and floats. Since all current CPUs are 32 bit (with the more recent ones being hybrid 32/64 - forget Itaniums), the standard data size should also be. Since many compilers will pad the variables anyways, there is hardly a reason to use any less.

To answer the OP:
A string is a complex data type that can allow for a series of characters to represent a line of text. Depending on the language (or your own implementation) a string can include many functions that let you modify it.

A variable can be any one of various data types that store information for your program. They can range from numbers (such as integers and floats) to text (char and string) to boolean values (true/false). Once you get more experience in whatever language, you will learn how to make your own data types using classes and structs.

In most x86 compilers/programming languages, a float (called single in a few languages, basic mainly) is 32 bits and a double is 64 bits. A float can hold fairly complex decimal numbers with around 6-7 significant figures. A double (since it can hold twice the information) can hold around 12-13 sig figs. This means a double can hold more decimal places and/or larger numbers. A float should be fine 99% of the time, depending on the program.

Share this post


Link to post
Share on other sites
demonkoryu    980
Quote:
Original post by Sr_Guapo

A string is a complex data type that can allow for a series of characters to represent a line of text. Depending on the language (or your own implementation) a string can include many functions that let you modify it.


In fact, there are two things to know. There are 1) "C-style" strings, which are just a pointer to a continous memory region which stores characters, like

const char[] aString = "I am a string"; // Character array

or

const char* aString = "I am a string"; // Pointer to character


The strings you define this way have a binary 0 at their end, that's why they are called "zero-terminated strings.
In memory, they look like


aString[0]='I';
aString[1]=' ';
aString[2]='a';
aString[3]='m';
...
aString[10]='i';
aString[11]='n';
aString[12]='g';
aString[13]='\0'; // <--- thats the same as (char) 0.


2) C++ has a string class in it's standard library, called std::string, which works with C strings internally, but additionally provides the many convenient functions Sr_Guapo mentioned, and also resizes the character array automatically as you work with it. The C-strings cannot be resized.

Share this post


Link to post
Share on other sites
jikbar    122
Variables are basically just pieces of memory with a certain type (int, char, float, etc.) which determines what operations C++ can do on them, and what they represent. For example, an int represents a whole number (no decimal), it uses 4 bytes in memory, and has the range of -2 billion to +2 billion. It can be added, subtracted, etc.

Variables can also be casted, which changes their type. For example, you can cast a floating-point number (decimal number, float) to an int to remove its fractional part, or you can cast an int to a float for floating point operations (casting to float is important if you want to keep the fractional part). Float's are also 4 bytes in size. A double is a float with higher precision. This extra precision comes at the cost of (slightly) slower operations and extra size (8 bytes).

Integer (char, short, int, long long) can also be signed or unsigned. Signed (the default) means that they can be negative. Unsigned means that they are only positive. Unsigned integers can go up to 4 billion since they can use the entire range for positive numbers.

Pointers are used to point to a location of a variable in memory. You can't do operations on them directly, but you can use the * operator at the beginning of the pointer to dereference it. Dereferencing returns the variable that the pointer points to, so you can do operations on it. You can look up the address of a variable with the & operator. Pointers can be assigned the location of a vriable in the form of: pointer = &variable; Pointers are also variables by themselves, they are 4 bytes long, so you can have pointers to pointers. Pointers can also point to functions, but that's an advanced topic, and can confuse even the best of us.

A string is a collection of characters, which can be represented as an std::string (a class, not sure if you've heard of those) or as a pointer to an array of characters, char*. A char is 1 byte long. The end of a string is defined by the NULL character ( a 0, but NOT the letter 0).

You use quotation marks " " to get a pointer to a constant string (which cant be modified) and single-quotes ' ' to get a pointer to a constant string without the NULL terminating character.

The const keyword makes variables (or pointers) unmodifiable.

Variables are either basic data types (int, char, short, long long, float, double, enum) or objects (anything defined by the class or struct keywords). Variables can also be unions oif data, which can be used to contain different representations (types) of data.

I hope my explanation contains all the help you need, but a C++ view of variables can only be complete when you know the ins-and-outs of classes and OOP, the ++ in C++. Good luck.

Share this post


Link to post
Share on other sites
demonkoryu    980
Quote:
Original post by jikbar
You use quotation marks " " to get a pointer to a constant string (which cant be modified) and single-quotes ' ' to get a pointer to a constant string without the NULL terminating character.


Minor nitpick: single-quotes don't give you a pointer, but a character.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this