tut2: defining your variables
this is a simple, yet important concept:
variables must be defined before use, and it attributes the amount of data they'll use, and hoe they'll be treated by the compiler.
this is a quick run-down of all of them, and will still explain them as they're used in future tutorials
int - as in integer, a number that ranges from -32768 to 32767 these numbers should be familiar to any GM user.
thats because both int, and the standard numerical variable on GM use two bytes of information to stare the data. int stands for integer.
unsigned - the same as integer, but it can only be positive (or zero) to the range is farther 0 - 65535 is also a modifyer
char - a character value that is stored in the computers information as ASCII, and therefore we can use mathematical expressions on it.
it only hold one character
float - as in floating point integer. can also be expressed as double which gives it "double accuracy" and long(explained below)
short/long - these are modifiers, so you would write short or long before int, unsigned, or float
for int it'd be -21474836 to 21474835 it only allocates two more bytes of data(4 total) and the increase is so large because it's expressed in binary which is in base 2, and exponentially based (<<you shouldn't understand that cocept yet. I'll explain it at the bottom of this) exponential
for short is the default
signed - also a default it's for int, float, and double
part2:
assigning variables:
you assign them with the assignment operator '='
one of two ways
int x; //allocated
x = 5;//initiallized
x was allocated, and then initialized. two things I'd like to mention
•those semicolons mark the end of the line, I'll talk more about them in the next tutorial
•'//' is the character for comment, everything after that isn't read by the compiler. exactly like the REM command in GM
also know as the comment command or, "that little apostrophe thingy"
second way is all in one line
does the same thing, I think you can work it out
these do the same thing since k is read as 107 (it's ASCII value)
char c1 = 'k';
char c2 = k;
the first statement works and the second doesn't
this is technically sorest, but is a very bad programming practice. it will set c to 'k' then to 'l' and then to 'm'
only one at a time so therefore c would equal m (if your reading the book I mentioned along with my tutorial you will notice that this isn't mentioned in the book.
my work is both consistent, and inconsistent with his work)
signed short int x1;
int x2;
these would be of the same data type, and allocate the same memory.
just an example to reiterate the default
int x, c, v, b;
x = c = v = b = 0;
did I just blow your mind? two new concepts
•you can declare multiple variable via the comma operator
•you can initialize multiple variables, it is important the zero is on the right side since the assignment operator (=)
evaluates right to left.
this sets b to zero v to b (which is 0) and soforth
int x = 0, c = 0, v = 4, b;
this sets x to zero c to zero and v to 4.
b is set the the nearest data address, which may or may not be zero. it's just left over data in the computers memory. if it's a spot in the memory never been use then yes, it could be zero,
but you have no way of knowing it. don't gamble on using uninitilized variables.
part3:
different bases
ADVANCED TOPIC ALERT
65
this number was expressed in base 10, decimal form
0xAE4
this is also a number, but expressed in base 6, hexadecimal form
0177
base 8 octal form
these can all be written to the integer variable type, if you haven't studied exponents then leave now (I don't mean to be rude, but
if you don't already know that this'll demoralize you from learning C/C++)
hexadecimal numbers always start with 0x (thats a zero)
so 0x54 is hexadecimal. To find the number you take the digits to powers of 16 (8 for octal, and 10 for decimal)
example:
0x54
5 X 16^1 + 4 X 16^0
remember anything to the power of zero is one
each digit of hex form can be 0 - f, or 0 - F (a number has to have all the "letters" in the same case)
numbers being themselves.
A - 10
B - 11
C - 12
D - 13
E - 14
F - 15
octal is the same followed by a zero, and numbers 0-7
and although we don't thing about it decimal form is the same
123
1 X 10^2 + 2 X 10^1 + 3 X 10^0
I sometime use hex form for writing large numbers, but rarely.
P.S. if you haven't already noticed I'll be making GM comparisons since the languages are similar to help GM programmers get a leg up on C programming. :D
good luck
P.P.S it said it was too long so I had to do it in two posts
just finished the third installment
tut3
writing an application.
now here's the good stuff
int main()
{
printf("hello world\\n");
}
the first thing a C (notice I didn't say C++)program executes is it's main function. it's execute what's in-between the
curly braces. our program also incorporates a printf statement which is just like the print statement in GM. except with different syntax
this will print hello world to the consol, and then make a newline. That is what the character \\n is. it's the newline character. the compiler treats
this as a single character much like a, or b. but when encountered instead of writing it, it makes a newline. the backslash starts all special characters such at this
to write a real backslash write //
also considered one character
printf statements can also take variable as arguments and print them
int x = 1;
main()
{
printf("%d", x);
}
this prints x in decimal form (hence the d)
there are many other character converters, and even modifiers to them.
one more thing I'd like to say on this. if you actually typed this into the compiler and ran it, you should see some annoying warning telling you main() epaulets to int.
now you should be mildly confused because I didn't tell you about returning values. main will ALMOST always return an int.
0 for: every line of code has been finished so I'm going to quit incorrectly now!
1 for: got it I just received the quit command
so the program should look like this
int x = 1;
int main()
{
printf("%d", x);
return(1);
}
the parenthesis aren't necessary, but a convention.
part 2:
scanf:
scanf has similar syntax to printf, but it read for info from the user
int d;
int main()
{
scanf("%d", &d);
printf("%d", d);
}
scanf looks for an integer the user writes, and then writes it again(on a new line)
simple, and versatile concept.
now I have a challenge for all of you.
if temperature can be converted from fahrenheit to celsius by the equation C=(F - 32)/1.8
then program a program to ask the user for the temp in fahrenheit and then writes it in Celsius.
post it here, and I'll comment on all the good and bad.
even if your program doesn't work still upload it.
P.S. make sure to keep it, because we'll be doing something else with it latter.
#include <stdio.h>
#include <stdbool.h>
main()
{
bool running = true;
while (running==true) {
float f = 0.0;
printf("Please type in the degrees in Fahrenheit: ");
scanf("%f", &f);
printf("The degrees in Celsius: %f\\n\\n", (f - 32.0)/1.8);
printf("Would you like to go again? [Y/N] ");
char c;
while (getchar() != '\\n');
scanf("%c", &c);
if (c != 'Y' && c != 'y') {
running=false;
printf("Thank you!\\n");
}
}
}
Nice tutorial.
part2: tut1: compiling with gcc
http://gcc.gnu.org/install/index.html
http://en.wikipedia.org/wiki/COMMAND.COM
This is in no way a tutorial of how to use Xcode. You'll pick that up through my videos, and trial-and-error. This is how to compile from the command line. So that you know what happens behind the scenes on Xcode.
for Mac you already have the gcc compiler if you downloaded Xcode, but can get it separate by downloading it form above. On Windows you must download it from above, unless you have it from the Windows version of Xcode.
Okay now that you have the actual software lets begin. Open a simple text document (textedit or notepad) and change it to simple text (only necessary with textedit.) Now type this into it
main()
{
printf("hello world.\\n");
}
you should recognize this as our hello world application from before.
Anyways save this as main.c into your user folder. Notice the extension is '.c'. Now open up the command prompt (Terminal for mac and cmd for windows). For Mac it's an application in the utilities folder, in your applications folder. For Windows type ‘cmd’ into run, you can also find it at your systems32 folder, and you can make one (good for gaining access to computer internals when your unauthorized) by opening note pad and typing COMMAND.COM into the file and saving it as an .exe . you should change your default window to something cooler than black and white, with ms-dos you can use the command ‘color’ and with terminal you can set it in Terminal > Preferences .
Before going on I should mention that I will passingly mention a few UNIX commands, and they of course won’t work on your pc machine, but you can find similar if not identical commands at the wikipedia link.
Another thing before going on a mac you can type in and assuming you understand they’re archaic documentation you can scrap the rest of this tutorial!
Now lets get down to business. Assuming you saved it too your user folder terminal starts up there by default, but if you didn’t change the directory to that folder using the ‘cd’ command. (note that the words directory and folder are synonymous.) and type this compiles and links your application and gives it the default name ‘a.out’. Your machine still retains the source file (main.c). Most tutorials say just to type ‘a.out’ to run it, but for some reason that doesn’t work for me. The command that will always work is: that basically means open a.out through the command prompt (terminal.) Since I don’t have a pc I can’t even begin to help you with this, but I confer with Ryan (Lombardi), and we’ll make an edit here for the pc users, and notify you. so you’ll see hello world written probably written in your boring black and white if you didn’t listen to me. just close it and go back to your main window. There are tons more options for you to use. Before going on I’d like to remind you that you can use multiple files to create your application. These files are called modules. lets say your finished with one and don’t need to use it anymore, or want to let a friend use it without seeing whats inside of it. you can compile it without linking, by making an object file. this creates main.o an object file that you can latter link to your program just like it was a source file. The upside to this is, if your machine is slow, or your program is huge then you don’t have to have the machine compile the whole application every time, just the source files, it only has to link the object files. (which is a relatively quick process.)
You can also define variables to be passed to the application at the preprocessing stage. This won’t seem very important to you unless your already familiar with the #ifdef statement, but I will elaborate on it’s importance during the discussion of the preprocessor.
It’s called the -D switch. The syntax is simple or gcc -D MACHINE = 1 main.c
fig1 makes an uninitialized variable DEBUG, and fig2 makes a variable MACHINE initialized to 1. Again you can’t see the infinite importance of this, but trust me its very important.
Optimization command -O (capital o) switch. just place it before every thing on the very last compile. Simply optimizes it.
The last, and probably most used command is the -o switch (lower case o). It allows you to name the program. This makes a compiled, and linked C program called “program”
On two parting note gcc syntax is identical for compiling C, C++, Objective-C, Objective-C++, and I believe java, but I don’t know anything java so don’t take my word on that one.
The command you may see some C programmers learn ‘cc’ is identical to gcc, but I think it only works with C programs.
P.S. This video for this one will be a little late, because I have to try a screen emulation with Ryan (Lombardi) so the windows people don’t feel left out. Wow I never thought i’d be accommodating for pc!
part2: tut2
So I’m finally into the loop of school, and writing these tutorials. This one is more of a part 2 to part2:tut1, but I’ll differentiate because this one is UNIX systems only. (Just too be clear that means Mac, and linux, but you activate root differently in linux.) You could of course do this with the sudo command, but If your doing this a lot you should activate the root account. Okay so its simple concept really. All commands in Terminal are C applications. You can run them without specifying that they need to be opened because it is implied. To imply your program should be included in this it simply has to be moved into the folder the holds all of these built in commands. This folder is called Bin. A seemingly innocent task, until you search your mac and find it nowhere. It is hidden, and you can only find it through the command prompt. It is in the root folder also known as ‘/’ or Macintosh HD. You can try to view in in finder, but to no avail. Unless you unhide it, which would be an undesirable, but very possible feat, I’ll give you the command, but implore you not to use it Back to the legit stuff. For curiosities sake lets view the innards of your mac You’re now seeing the fabled hidden files of your mac. navigate into the bin folder and view it if you’d like now try to move your compiled, and linked program into it What is this! No permission! So now you need to upgrade your account your account. Now there are three types of accounts on a Mac: The guest, administrator, and root or superuser (meaning the same thing) You’re all administrators. Unless your doing this at work... which you shouldn’t cause you’ll get fired for hacking ;) You have to become the most powerful, and un restrained user to modify bin, you must become root. okay they’ve changed it with every operating system, which really ticks me off. Honestly I can’t reword anything about rooting different systems, or explain all of them. So at this time please google it. So sorry, but its kinda beyond me.
So now your super user account is active. You’ve set the password to something archaic, yet quick to type, and we’re ready to roll. Lets bust out that super user account with type in your password, and lets roll! Okay its not that exciting, I mean you can do anything in the whole wide world now, including hacking you pals mac, I believe. Not my area of expertise. Back to bin. So your root now, you should notice your host changing, and a new home folder (what the tilde (~) actually means) it is now /var/root so if you want to navigate to the desktop you have to wright out /Users/Manion/desktop. With your own user of course. so move it using the mv command, or cp command to copy it. of in the specific mv /Users/Manion/Desktop/proj/a.out /bin/a.out
You can rename the file simultaneously, by changing its name in the new file path. Make sure your only move the executable (default name a.out). heres all the commands you must type to make it work
su root
mv old new
exit
logout
or you can use
For the password prompt type in your user password. NOTE: the exit command in the first example takes you out of the root user account.
To use you program just type in the name of the command (ie. a.out if thats what its named in bin)
P.S. tutorials will be more regular now sorry for the interruption of the first week of school ;D