Assembly and Cracking From the Ground Up |
An Introduction
by Greythorne the Technomancer This Essay Posted By Vizionarie |
C - A Mini-Tutorial Part I - Introduction to C Programming |
Some time ago, some computer programmer who thought he was extremely funny came up with the idea to make everyone's first program to be one which prints "Hello World!" on the screen. So we'll begin there: /* Your first program in C */ main() { printf("Hello World!"); } Okay, that's it! No problem. Let's go through this line-by-line, shall we? The first line is a comment. Just as in any programming language, there is a standard for commenting, C also has one. To comment out ONE line, use the character ' (Note from +gthorne: C++ and some C compilers doing double duty like Borland C and MSVC use a pair of slashes... // to denote a comment for one line only) ...to comment out a BLOCK of lines, start with a /* and end with a */. Then place your comments inside. Simple, right? Great, let's move on. Next line....main()...that's C's way of saying "Hey, this a function called main, which contains no parameters". Okay, what's a function? What are parameters?? Okay, a function is like a set of instructions which you can call again and again, from anywhere in your program. They're called other things in other languages, like Procedures, Subs, etc... Parameters are variables that the function must be given in order for it to run. For example, if your function looked something like this: int blah( int param1 ) { printf("Hi, I take a paramater"); return 0; } it means that your function takes ONE parameter, of type int, and returns type int. Don't worry about that return stuff just yet...we'll get to it, i promise. So, now we have a model for anyfunction: Return-type Function-name( Parameter-list ) { /* Do Stuff Here */ }Well, that wasn't so bad, right? Okay, one more thing.... EVERY PROGRAM MUST HAVE A FUNCTION CALLED: main ! Remember that...cause if you don't, your program won't work. This is because every program written in C starts at program main() and goes from there. Okay, next line(skip the brace :-)..... printf("Hello World!"); printf is the standard C function which prints a line to the screen. You place the string you want to print to the string inside the quotes. Note that the line ends with a semi-colon. This is your way of telling C "Hey, execute all the stuff from this semi-colon to the last semi-colon, pleaze!". After almost every action you do in C, you must follow it with a semi-colon. By the way, just in case you are wondering...there ARE ways to print parameters and variables to the screen using printf...we'll get there, settle down! Okay, that's it. Oh, almost forgot...those braces tell C where the function's actions start and stop. Don't forget those either...they're pretty important:-). Okay, let's try another example. This time, we'll use some neato escape characters provided by C. What are escape characters?? Sheesh, you are a newbie, aren't you?? Escape characters are nifty sets of characters in a language that do nifty stuff(horrible explanation, i know :-). Some standard escape characters do things like move the cursor over a tab space, move the cursor down a line(newline), etc. Okay, here goes: /* Your second C program */ main() { printf("Hey, look at me\n"); printf("I'm learning\t C!"); } Kewl, huh? Okay, let's examine this once again. Let's skip the comments and the main(), and get straight to the meaty, juicy code. Okay, we call printf(yawn), but hey!, what's this \n we threw in there? Well, that's your way of telling C "Hey! Move that damned cursor to the next line". Us programmer types call it the newline escape character. Whenever you want the cursor to move down to the next line, use \n. Okay, so then what's this \t thingy? Well, when C looks at that, it's just like you pressed the TAB key on your keyboard, hence it's called the tab escape character. Okay, last example for this one. Let's print a variable. Uh-oh...sound challenging? Don't worry, it ain't that bad. /* Your largest C program yet...complete with function calls!!! */ void printStuff(); main() { printStuff(); } void printStuff() { int x; x = 10; printf("The variable x is equal to %d", x); } Neat, huh? Okay, let's take this one line at a time. void printStuff()?? I thought braces had to come after function names?? Well, they do...but in C, you've got to tell it all the functions you're going to use(except main). Thus, before main(), you must declare all functions by using prototypes. Our's says we are declaring a function named printStuff that returns nothing(void). Remember to end this line with a semi-colon!! Okay, inside main, we see the line printStuff(); ...this is the way we call functions in C. It's just the same as we call printf. Okay, let's move down to function printStuff. Make sure that you're prototype and function match, because if they don't, C will yell at you pretty loud. Okay, we declare a variable of type int(integer) named x. I won't go through all the types here, so just trust me, okay? Next, we make x equal to 10. Pretty self-explanatory, I should say. Then, we print a string. Wait a minute! What about all that %d crap?? And why the extra stuff after the quotes?? Very good, you're learning. printf is a neat function, because it can have one or two parameters. If you just want to print a plain-ole string, with no variables involved (BORING!), then you don't need the second parameter. However, if you do want to print a variable(we did), then you need the second parameter. In our string, the %d means that in that spot, we are going to print an integer, and we're going to tell C which integer to print in the second parameter. Uuumm....I guess that's about it for this lesson. Next lesson we'll create our first interactive program. Neat, huh?? This tutorial was written by vizionarie_. If you have any comments, send them to vizionarie_@yahoo.com. |
+gthorne'97 |