| Paintball Tips | Trojans | Downloads | Home |Linux|Linux Tutorial|C Tutorial|Message Board/Forum|Worms/Virus Removal|





free web hosting | website hosting | Business Hosting | Free Website Submission | shopping cart | php hosting
C Tutorial



C Tutorial



»Step 1
»Step 2
»Variables
»IF Commands
»Defining variables a different way; Using fgets
»Using sprintf
»What can I use C for?
»Notes












Step one - Getting GCC

The first step to do is get the C compiler. Download it
HERE. When your finished downloading it, make a new folder named gcc in your C:\ directory [we will use this folder for the whole tutorial], and put everything from the GCC unzipped folder into there. When you did that, go down to step 2


Step two - Making your first C program.

Now to make the C program. What you need to do is, open up NOTEPAD not WORDPAD, NOTEPAD. Now, you want to do the include the header file, so type in:

#include <stdio.h>

That is the header file, and is mandatory for printing letters on the screen. Next, you want the int main function. So go ahead and combine it with the header file. It should like like this:

#include <stdio.h>

int main() {

}

Now that you have that on, you want to print a message. The command for printing messages is

printf("my text here");

To put a line break, put '\n' inside of the printf command. So lets say we wanted to print out a Hello World! message on the screen. The screen should look like this:

#include <stdio.h>

int main() {
printf("Hello World!\n");
}

Now that that's done, save it. Save it as sample.c in the gcc directory (c:\gcc, the one you made)
You MUST put '.c'. If you don't than the program won't compile. Now open up the console by opening Start > Run. Type in command when you've opened run. Now a black screen should come up. Type in 'cd \gcc\bin'
Now type in 'gcc c:\sample.c -o sample.exe'
It should compile correctly. Now type in sample to open it and it should say Hello World.
Congratulations, your first C program.


Making different types of programs.

The next lesson we will learn is how to define a variable. Lets say we wanted to wait for the person to press enter after printing text to the console. We would have to define a variable to use with the Getc function. This time, lets say that the variables name is 'program'. We would do the header files and the int main(){ function and right below int main we would define program like:

int program;

Than we want to print text to the screen with the

printf("Hello world. Enter to exit.");

To wait for the users command, we need to define the sample variable to getc. To do this it should be

sample=getc(stdin);

So the whole thing should look like:

#include <stdio.h>

int sample;
int main() {
printf("Hello World! Enter to exit.");
sample=getc(stdin);
}




Using IF's

These are important in lots of programs. An example of these are in a menu; Press 1 to enter, 2 to exit or 3 for message. Lets try one out. Do the header file, the int main function and define a variable. Lets say it is varit. Do the printf with a little menu. Maybe something like :

printf("1 - Enter \n 2 - Exit");

Now here is the if. Tell the variable what its going to do [in this case its getc]. Then next is the IF. For the menu, lets say when we enter we want it to say a message, maybe MY PROGRAM. When we exit lets make it say GOODBYE. This is what the if should look like:

if (varit="1") {
printf("My program");
varit=getc(stdin);
}
if (varit='2') {
printf("GoodBye!");
varit=getc(stdin);
}

So the whole program should like:

#include <stdio.h>

int main() {
int varit;
printf("1 to enter, 2 to exit \n");
varit=getc(stdin);
if (varit == '1') {
printf("My Program");
varit=getc(stdin);
}
if (varit == '2') {
printf("GoodBye!");
varit=getc(stdin);
}
}


You can go ahead and compile it, see what it looks like.


Defining variables a different way; Using fgets

To use fgets you need to define a variable a different way. Instead of defining it with int variable; do this:

char *variable;

Than to define the variable, do this:

variable=(char *) malloc(900 * sizeof(char));

Now you need fgets, so to wait for the user to put input using fgets, type this in:

fgets(variable, 900, stdin;

So if you used it the whole thing should look like this:

#include <stdio.h>

int main() {
char *variable;
variable=(char *) malloc(900 * sizeof(char));
fgets(variable, 900, stdin);
}

Now we go down to the next lesson.


Using Sprintf

Sprintf is when you use input from a user in a command. For this lesson's example, we will make a simple story with sprintf. Do the header, and the int main() function. Now make a printf to say:

printf("Enter your name");

So right now the whole thing should look like:

#include <stdio.h>

int main() {
char *variable1;
variable1=(char *) malloc(900 * sizeof(char));
printf("Please enter your name here:\n");
fgets(variable1, 900, stdin);
}

Now we want sprintf to catch the input, so we need another variable for sprintf to output input to. Put another char *variable and lets call this variable2. Juts remember not to define it. So now the whole thing should look like:

#include <stdio.h>

int main() {
char *variable1;
char *variable2;
variable1=(char *) malloc(900 * sizeof(char));
printf("Enter your name here please:\n");
fgets(variable1, 900, stdin);
sprintf(variable2, "%s", variable1);
}

As you can see, it asks the user for input, than sprintf reads input from variable1, which makes it '%s' and turns that into variable2. Now we want to make the story to use variable2 as the name. We'll just use printf for the command to print out the story. Everytime you use "%s" in the story, you need to put at the end of it what variable that is. So here is the story:

#include <stdio.h>

int main() {
char *variable1;
char *variable2;
variable1=(char *) malloc(900 * sizeof(char));
printf("Enter your name here please:\n");
fgets(variable1, 900, stdin);
sprintf(variable2, "%s", variable1);
printf("One day there was a kid named "%s" that had a lot of friends. One day he met another kid with the same name, "%s", and they had a very good life.", variable2, variable2);

}

As you can see, we used %s 2 times, so we needed to tell printf what those two %s were. In that case, we wanted to display the person's name twice, so we put in variable2 twice.


What can I use C for?

You can make usefull applications. An example is: Lets say there is a person who cant use DOS that good, and he needs to type in a specific thing to do something. All you need to do is make the C program with the system("dos command here"); command. It will automatically do it for them. You can also set up programs that can make using your computer faster and better. For example; if your computer needs to know your IP alot, and your sick of typing in netstat alot, you can make a program to do it for you when you open it. There are many things you can use this language for.


Notes

Copyright © 2002 Giovanni Perez. Note that this tutorial is not complete yet, the next update will be soon.