C programming for the complete newbie Hello there im Krisis you may have seen me on irc.hackersclub.com. Well I thought it was about time to write an article like everyone else. But unlike many others mine wont be on Hacking, Cracking, or Phreaking it's on C programming, you see I'm not the best hacker but I'm an ok programmer. So here it goes. This is based for absolute beginners so those of you like my friend Chrak wouldn't be interested in it. 1st lets talk about some of C 's history. C was invent by Dennis Ritchie and Kenneth Thompson. They modeled it after the language they were using called B. C was a subset of B hence the name. C was made because B was going out of style and they needed a new language to write UNIX in. Yes UNIX was made in C. C was made popular very quickly because every UNIX sold had a C compiler. A compiler is a program thats looks at your source code and transfers it into object code, after it is transfered into object code it must be linked, once it's linked it can be executed. /*************************************************************************** ********************/ 2nd Lets talk about variables they are your integers and characters and so on. You have many data types they are. int integers char characters long int bigger integers short int same as int float decimal numbers double even bigger decimal numbers To define a variable 1st you must put something like int MyNum; MyNum = 2; or for a character value it would be like char name; name = 'Jim'; notice the '' you must have those around character values, they are not needed for integers or decimals. /*************************************************************************** ********************/ 3rd I'll tell you about stuff like #include and #define. #include is used to tell the compiler that whatever is in the brackets just be included like its part of your code. #include #define is used to define something Like the color of a truck or car. #define TRUCK "red" #include and #define must come before any functions are even prototyped (I'll talk about this later). /*************************************************************************** ********************/ 4th I'll talk about functions. Every program must have at least one function. That functions name must be main(). The () tells the compiler that it is a function. All functions must return a value in the main() function a 0 is usually returned. In your functions you will want to use comment's to explain your code a comment is begun by using /* and ended by using */ . I will now show you your 1st program. #include /* Used in most standard Input Output Programs */ main() { /* Beginning Brackets used to show the beggining of a block of code */ printf("Hello World"); /* A function already written in Stdio.h */ return 0; /* Value returned from the program */ } /* Ending bracket used to show end of a block of code */ now compile your program in your compiler if your using UNIX do it like this gcc hello.c -o Hello and then run your program bye typing in ./Hello /*************************************************************************** ********************/ 5th I'll talk about output which is essential to almost all programs. I'll start you out with printf(); It is defined in Stdio.h so every time you call printf(); you must include Stdio.h . printf(); 's syntax is quite easy you just used it like this printf("What ever you want outputted"); to output variables you do it like this char dog='scruffy'; printf("My dogs name is %c", dog); notice the %c it tells the compiler to look for a character variable. Now for Integers and Decimals int age=16; printf("I am %d year's old", age); use %d to print out decimals and integers /*************************************************************************** ********************/ 6th Lets talk about multiple functions. When you have more than one function you must prototype it. Here is an example. #include void hello(); /* This is a prototype notice the void. Void tells the compiler that this function does not return a value like return 0; */ main() /* Main doesnt ever need to be prototyped */ { hello(); return 0; } void hello(); /* Your prototype must look exactly like your real function */ { printf("Im in the function hello!"); } Void is your return type. Other return types are int for returning integers use float to return decimals and so on. /*************************************************************************** ********************/ 7th I'll introduce you to input. Ill teach you how to use gets() and scanf() and fgets() properly gets() takes a variable and place data into as do scanf() and fgets() In the next example I will use all 3 #include #include main() { int x, y, z, ans; printf("What is X 's value \n "); gets(x); printf("What is Y 's value \n"); scanf("%d", &y); /* Scanf is odd I dont recommend using it try and use gets and fgets more */ /* Whatever is used to print the variable type you are using is placed in parantheses and & is used in front of whatever variable you are using */ printf("What is Z 's value"); fgets(z, 25, stdin); /* fgets is kinda tricky at first glance */ /* first off you put what variable you want then how many integers or characters long it can be and then stdin,stdin is a macro defined in stdio.h it is used to represent standard input */ ans=x+y+z; printf("Ans equals %d", ans); /*************************************************************************** ********************/ 8th Lets talk about decision statements like if and else. here is how if is used if(VariableName==5) { printf("Your variable is 5"); } else is used after if, it is used like this if(VariableName==5) { printf("Your variable is 5"); } else { printf("I dont know what your variable is"); } /*************************************************************************** ********************/ 9th Ill talk about While loops and do-while loops. Loops aren't as hard as they may seem. while loops are easy. Just watch and learn. #include main() { int x=1; while(x<2600) { printf("X=%d",x); x++; /* adds 1 to x */ } return 0; } /* While loops dont have to happen only if the right sequence happens do they execute */ Loops can be placed inside of IF and else statements if you want. That can be very helpful if you want a process to happen a bunch if something happens Like the user pressing X instead of Y. Do-While loops are just as easy. They automatically execute at least once. #include main() { int x=1; do { printf("X=%d",x); x++; }while(x<2600); return 0; } The do tells the program to do this at least once and it doesn't see the while until it has already do the do. /*************************************************************************** ********************/ 10th I'm going to tell you about another kind of loop the for loop. For loops execute a given number of times and then stop. For loops are executed like this. for(x=1; x<100; x++) { printf("X=%d",x); } That prints 1 through 100. Thats about it about for loops there not very hard. They can be pretty useful. But I dont use them alot I'm into While loops. /*************************************************************************** ********************/ 11th is all about Arrays. Arrays are consecutive places in memory. Arrays can be integers and characters. They can be just about any size. Here is an example. #include main() { int i[2] int i[1] =2600 int i[2] =1982 /* Year I was born */ printf(" I[1] = %d ",i[1]); printf(" I[2] = %d ",i[2]); return 0; } See how easy that was Arrays aren't very hard at all. /*************************************************************************** ********************/ 12th I'll tell you about passing parameters to functions. Its nots to hard but I've said that about everything. First you must prototype it before main() I hope you remember how to prototype. Here is an example of passing parameters. #include int next(int x); main() { int age; printf("please enter you age "); fgets(age, 3 ,stdin); age(age); /* Age is passed on to the next function */ return 0; } next(int x); { x++; printf("Next year you will be %d", x); return 0; } /*************************************************************************** ********************/ 13th I'll say a little sumthin about why C is good to Hacking. It's good because it is so portable C can be used on all processors and Operating Systems. So if your exploit you just wrote works on one UNIX like OS odds are it will work on another, therefore you dont have to write a whole new program just to get a root shell. /*************************************************************************** ********************/ Last but not least a sample program. #include int blah(int x,int y); main() { int a,b,r; printf("Enter some numbers "); scanf("%d", &a); scanf("%d", &b); r=blah(a,b); printf("R = %d",r); return 0; } int blah(int x, int y) { return x * y; } What does blah do and how does it work? You tell me. /*************************************************************************** ********************/ Thats it for my little tutorial on C. I hope it helped you some. But for further info on C I suggest reading C programming in 12 easy lessons by Greg Perry from Sams Publishing. It helped my a shitload on learning C. You also might want to get some books on C++ a subset of C You can mail me at reid@programmerz.org