Access_Denied's Forums

Site Resources => Tutorials => Topic started by: Access_Denied on June 19, 2007, 04:52:17 AM



Title: Tutorial Three: Variables
Post by: Access_Denied on June 19, 2007, 04:52:17 AM
Welcome to tutorial number 3! In this tutorial, we'll be discussing variables. First off, if you don't know what a variable is, look it up. Second, start a new folder in your 'projects' folder from the second tutorial. I'll be calling my folder 'vars'. Now go in there and make a main.c and a Makefile. Open up main.c and let's get coding. Let's throw in a few notes:

/*
Variables
Created on (Insert Date)
 By: (Insert Name)
 */

Now, let's throw in our includes, defines, and the module info.

#include <pspdebug.h>
#include <pspkernel.h>
#include <pspcallbacks.h>
#define printf pspDebugScreenPrintf

PSP_MODULE_INFO("Variables",0,1,1);

Now, here comes the variables. I will only be teaching you three of the main variables. But I will probably have a snippet in the snippets section outlining more of them, so check there soon. Here they are:

int numbe = 8;
float decimal = 3.82;
char string[50] = "This is a test";

The first line says that we created an 'integer' with the name 'number'. So when we want to call that variable, we use the name 'number'. Integers can hold a whole number ranging from -32,768 to 32,767. Notice that our variable is worth 8.
The second line says that we create a 'float' variable with the name 'decimal'. We named it that because a 'float' can hold a decimal, unlike an integer. A float is accurate to 7 digits on each side of the decimal point. Our variable is worth 3.82.
The third line creates a 'buffer' with the name 'string'. A buffer can hold letters and phrases. But this buffer can only hold 49 letters? Why only 49? Because you need to leave an extra space, just know that. But we only used about 10 letters, because all that's in ours is "This is a test".

Now, let's start our main function, then we'll work with the variables more.

int main() {
     pspDebugScreenInit();
     pspDebugScreenClear();
     SetupCallbacks();
     decimal = decimal - number;


Notice what I did on the last line. Now, it works out the right side of the equal sign first, so we can work it out too. decimal - number. That's the same as 3.82 - 8, since the words are variables that equal those numbers. So, that would be -4.18. Now, it works out the other side. So now decimal equals -4.18. It might be hard to understand a first, but you'll get it. Now, let's print our variables to the screen:

     printf("Decimal is equal to: %f \n",decimal);

What the hell? OK, I'll explain. The %f stands for the variable. Because if we wrote the variable name inside the parentheses, it would print the variable name, not the value. So, we put a %f in there, then put the variable name afterwards. Note: %f only works for floats. Oh, and the \n? That just means start a new line.

You can also do this:

     printf("%f  %i\n",decimal,number);

That time we had both variables. We used a %i for integer this time. Then we put the variables after the comma, in the order they appeared. Now try this:

     if (number > 0) {
          printf("%i\n", number);
     }

Now that prints our variable, but only if it's greater than 0. And since it is, it will print it. Notice how that statement was structured. It even has the { and } to signify start and end. Now, let's print our string.

     printf("%s\n",string);

This time, we used %s for string.

OK, now you should have a decent understanding of variables, and how to print them. You even know how to do and 'if' statement, one of the most important programming techniques. So, let's finish our program.

     sceKernelSleepThread();
     return 0;
}



Now, use the same Makefile from the last tutorial, just change the TARGET line and the PSP_EBOOT_TITLE line. Compile, and go. If it doesn't work, compare the code. If it's the same, and it still doesn't work, post a thread bitching at me because I did something wrong. :p See you in the next tutorial.


Hosted by www.Geocities.ws

1