Need example of declaration.

Pages: 12
Aug 31, 2009 at 9:36am
hi guys,i need help with c++ declaration example.

such as intenger,character,floating and string. i really appreciate it if someone will help me out.

thanx.
Aug 31, 2009 at 9:41am
Aug 31, 2009 at 9:50am
argh,i still couldnt understand some of it..do u have any idea where i could find more? bcause i already googled, and lots of them not for newcomer like me.
Aug 31, 2009 at 10:49am
Ok, then, maybe you should post some code which you don't understand, and then we can help make it clear for you :)

Aug 31, 2009 at 10:51am
There are books on the matter, like the C++ Primer by Limpann and Lajoie but what is it you don't understand?
Aug 31, 2009 at 1:52pm
hmm,i think i just make you guys confused.sorry. i need to know example of declaration each for data types. whether integer,char,float or string.
Last edited on Aug 31, 2009 at 2:01pm
Aug 31, 2009 at 2:09pm
I think the understanding is that you want to know how to declare a variable of a certain type (such as integer, etc).

The responses are basically that declaring variables is the most fundamental aspect of computer programming,
and in virtually any given piece of C++ code you can find on the 'net, you will see examples of variable declarations.
Just look at any post here that has code in it if the tutorials are too advanced (though it does not get any simpler than the "Declaration of variables" section in http://www.cplusplus.com/doc/tutorial/variables/ ).
Aug 31, 2009 at 2:09pm
int myInt;
float myFloat;
char myChar;
std::string myStr;
long myLongInt;
double myDoublePrecisionFloat;
int myIntArray[amountOfElements];
char myCharArray[amountOfElements];
std::string YepEvenThisWorks[amountOfElements];

What don't you understand?
Aug 31, 2009 at 2:12pm
'int' type
 
int x = 5;


the (simplest) format for declaring variables is
 
type variable_name;
or type variable_name = value;

you can also declare variable of the same type in one line:
type variable1, variable2, variable3; // ... or type variable1=value, variable2=value; // ...

here is an example of many types of variable declarations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int i1;
int i2 = 5;
int i3 = 5i;
int i4, i5, i6;
int i4 = 1, i5 = 2, i6 = 3;

char c1;
char c2 = 'a';
char c3 = 97; // 97 = 'a' on ascii chart
char i4, i5, i6;
char i4 = 'A', i5 = 'B', i6 = 'C';

float f1;
float f2 = 5.5;
float f3 = 5.5f;
float i4, i5, i6;
float i4 = 1.1, i5 = 2.2, i6 = 3.3;

std::string s1;
std::string s2 = "Hello";
std::string s3("Hello"); // since string is a class...
std::string i4, i5, i6;
std::string i4 = "Hi", i5 = "Hey", i6 = "Hola";


now that we have some code posted ask about anything that confuses you...
Last edited on Aug 31, 2009 at 2:19pm
Aug 31, 2009 at 2:28pm
thanx a lot guys. ill think i get it. but Alan,what about float? i mean for double.how to declare it?
Aug 31, 2009 at 2:34pm
the same as any other... the double/float type is just used to store a number that has a decimal value.

1
2
3
double d1; // makes a double variable with no value
double d2 = 1.5; // makes a double variable with the value 1.5
double d3 = 2; // makes a double variable with the value 2 
Aug 31, 2009 at 2:42pm
-edited-
Last edited on Aug 31, 2009 at 2:47pm
Aug 31, 2009 at 2:46pm
oh i think i get it now.....thanx a lot for saving my day! i owe you guys. :D
Aug 31, 2009 at 3:03pm
Always remember to assign a value to the variable before you use it. For an example of why, try the following code:
char charArray[80];
for (int i = 0; i < 80; ++i)
std::cout << charArray[i];

You should see a bunch of characters. Maybe even different ones everytime you run the code.

As good practice, assign 0 to numerical types such as ints and floats, '\0' to chars, and "\0" to strings, e.g.
int myInt = 0;
char myChar = '\0';
std::string myStr = "\0";
Aug 31, 2009 at 3:21pm
thanx for the tip chris. for your information, i havent start compiling any programs yet,im a newcomer to c++ world. haha.
Aug 31, 2009 at 4:32pm
i havent start compiling any programs yet,im a newcomer to c++

Make sure you have a balance of "book" work and practical work. By that I mean don't just read information/tutorials, actually give them ago and create things using given ideas and some of your own.
Aug 31, 2009 at 5:35pm
chrisname : "\0" is a waste of space since "" //empty string is a null-terminated 0 lenght string (e.g: an 1 lenght array consiting a 0), while "\0" is a 2 lenght array consisting two 0s.
Aug 31, 2009 at 5:49pm
To have embedded null characters in the string you need to specify the length of the string (which does not include the terminating null):
std::string s( "\0", 1 );
Aug 31, 2009 at 5:53pm
Well... That's true, but it doesn't make much of a difference, for the most part. All instances of "\0" probably point to the same string, so the binary is only one char longer. std::string::string(const char *) will only read until the first zero, so it'll just create a zero length string. These are almost equivalent:
std::string s;
std::string s="";
The latter may run a few more instructions, since it needs to check the size of the C string.

EDIT: I'm replying to R0mai.
Last edited on Aug 31, 2009 at 5:54pm
Aug 31, 2009 at 6:03pm
helios : Other than the 1 byte memory overhead that "" has, in my eye it's much more readable than "\0", because "" is actually empty, but in "\0" there is one extra character which makes it look like it's not empty.

Anyway I've never seen "\0" before, that's why my eye caught up on this :)

Good night!
Last edited on Aug 31, 2009 at 6:04pm
Pages: 12