³ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄij +-+-+-+-+-+-+-+-+ ÛÛÛÛÛÛÛÛÛ²²²²²±±±±±°°°ð|O|u|t|b|r|e|a|k|ð°°°±±±±±²²²²²ÛÛÛÛÛÛÛ +-+-+-+-+-+-+-+-+ Issue #5 - Page 5 of 13 ³ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄij Radioactive_Raindeers guide to C/C++ programming. Part 2 - More basics. Hello again all you newbie code poets. Welcome to part 2 of this guide. In this issye we will start off with the variable stuff I promised I'd talk about. And maybe a bit more about functions. I haven't really decided yet. I'll just stop here. Introductions are crap and I think you'd rather read the actual guide. As you may remember we have our basic variable types; int, char and float. Now, you may have thought about how large numbers you can store in an int or how to store strings of text. Actually, there are several integer variables and a couple of float-type variables. I'll list them here with what the specifications say about the sizes. Integer type variables. short A short has to be at least 2 bytes. int An int has to be at least as large as short long A long is at least 4 bytes and cannot be smaller than an int. The usual sizes on short and long are 2 and 4 respectively. Int can be either. An integer type variable can alos be signed/unsigned. A signed variable can hold both positive and negative variables. An unsigned variable can only hold positive numbers. You can do something like this to get the integer type varible sizes on your system: <---- code start ----> #include int main(void) cout << "int is " << sizeof(int) << " bytes.\n"; cout << "short is " << sizeof(short) << " bytes.\n"; cout << "long is " << sizeof(long) << " bytes.\n"; return 0; <----- code end -----> Float type variables. float Has to be at least 4 bytes. double Has to be at least 6 bytes and can't be smaller than float. long double Has to be at least as large as double. These are usually 4, 8 and 10 bytes. To get the sizes on your system try modifying the above example. It's not that hard. Chars and arrays. A char is a variable type that can only hold a single character. Not a lot to say about it really and it's quite useless by itself if you ask me. However, if you use an array of chars you can use them like you'd use string variables in other programming languages. So what is an array? The easy answer would be that it's a variable type that stores a load of variables. That doesn't tell the whole story though. If you think of a variable as a box (like a shoebox or something) that you put numbers and characters and stuff like that in. Then an array is like a shelf that holds those boxes. Let's say we wanted to store the string "foobar" then we'd use the following code: char blah[7] = "foobar"; The 7 in there tells the compiler that we want 7 elements in our array. That's 6 for the text and 1 for a null at the end which signifies the end of the string. But we can also leave the number out like this: char blah[] = "foobar"; This makes the compiler do the counting for us. A really nice feature if you're using really long strings. Let's see what this looks like with a nice diagram. Index -> | 0 | 1 | 2 | 3 | 4 | 5 | 6 | Content -> | f | o | o | b | a | r | \0 | Note that the index starts at 0 for the first element. Let's change our array a bit. blah[5] = 'f'; // Use single quotes with characters. Double quotes with strings. This makes our array look like this: Index -> | 0 | 1 | 2 | 3 | 4 | 5 | 6 | Content -> | f | o | o | b | a | f | \0 | This is something most books on C teaches. I've only found one good use for this though. And that's replacing a character with \0, the null symbol. blah[3] = '\0'; Now the array looks like this: Index -> | 0 | 1 | 2 | 3 | 4 | 5 | 6 | Content -> | f | o | o | \0 | a | f | \0 | The nice thing about this comes in when we print it. cout << blah << "\n"; And we get "foo" printed on our screen. Neat, eh? Yeah, I know. Still boring... Anyway, back to the guide. Like I said earlier, \0 represents the end of a string. So the program stops reading the string when it finds a \0. You should also know that the index number doesn't have to be a constant. You can use an integer to choose the element you want to access. Time for another boring code example. #include #include // This is here because we want to use the strlen() function int main(void) char blah[] = "foobar"; // creating our string. for (int i = strlen(blah); i > 0; i--) blah[i] = '\0'; cout << blah << "\n"; return 0; There are two new things in this code example. The first one is the for loop and the second is the calling of strlen(). If we were to look in the header file string.h we would probably find that strlen() is a funtion that accepts an array of chars as argument and returns an integer. What we are doing here is sending the content of our variable named blah to that function. It counts the number of characters in the array and returns an integer containing the number. A for loop is a really useful piece of code. It executes the code inside the curly brackets several time. The actual number depends on what arguments we give to the loop. It takes three arguments separated by semicolons. The first argument (int i = strlen(blah) in the example) sets the initial value of the count variable. In our case it sets it to the lenght of the string. The second argument is the test condition (i > 0) which tells the loop when it should execute. Here we told it that it should execute when it contains a value greater than 0. The third argument (i--) tells the loop what to do at the end of each execution. Here we told it to decrease i by one. Just a quick summary of how this loop works: It gets the lenght of the string and puts it in i. It then checks if it passes the check. (if i is greater than 0) Since i is 6 it does. It then executes the code. Then it executes it's third argument which decreases i by one. Then it goes back to the second step and checks if i is greater than 0. And so on... When the condition is no longer met the code procedes as usual. In this example return 0 is executed and returns control to the operating system. That's it for part 2. I haven't decided what will be in part 3 yet so you'll just have to wait and see. Happy coding! / Radioactive_Raindeer r_r@diegeekdie.org