Programming with C

First before you start programming with C you need a compiler. If you are searching a good compiler (and free) I would recommend DJ. Delorie's DJGPP. It is a free C compiler for Dos. This C tutorial is going to help you started. Heres my help for installing DJGPP. I am not a great C programmer, so if you got some feedback about this, mail it! Ok, it's time for our first (?) program!

Hello world! Basic C program.

Hello world! is going to be the first example. It is easy to make even if you are a newbie. Let's start coding, and try to remember that if theres text like this: /* comment */ it is comment, so you don't have to write it. Here we go!


#include /* this includes stdio.h file to program. Example, if you have pcx.h -file there can be information how to read PCX-file. You can write your own include files. stdio means stantard input output. */
main() /* this is the main part of the program */
{ /* main code starts with { and ends with } */
printf("Hello world!\n"); /* printf("") is function that allows you to print text. \n means that you change the line and almost every function have to end with ; */
getch(); /* this function waits that you push a button */
} /* ends the program */

Now you can compile it! Example if you are using djgpp write gcc -o hello.exe hello.c. It should compile it if you are configured DJGPP right. Well, that was the first example.

Next step

Next program is going to be simple too. I'm going to show how to example ask a guestion and print if it is right or wrong. Let's write that code!

#include
main()
{
int pass;
/* you use int when you use numbers and char when you use letters. */
printf("What is the password?\n");
scanf("%d", &pass);
/* %d is to use with numbers. You can use %f too. %s is for words and %c for character. &pass takes number 415 from memory. If you try pass, it isn't the same thing */
if(pass==415) printf("correct!\n");
else printf("false!\n");
}

That was the second example. Theres going to be more! I promise. You just click my banners and I just make you examples.

Playing with characters.


This example shows how to use char function and how to put word in memory and read it from there. Let's see how it works!
#include
main()
{
char name[50];
/* This is the character function. [50] means size. */
printf("What is your name?\n");
scanf("%s", &name);
/* %s is for long words and & before name means that it will be stored in memory */
printf("Hello, %s\n", &name); /* Pretty much the same as before. &name reads it from memory. */
}
That's it for the thirth example. You go and study it.

Counting.. Cool


This one shows how you can count with a program
#include
main()
{
float first, second, plus, kerto, minus, jako;
/* Float is like int, but you use it with decimals */
first=5;
second=3;
plus=first + second;
/* plus gets first + second */
printf("%f + %f is %f\n", first, second, plus);
kerto=first * second;
printf("%f * %f is %f\n", first, second, kerto);
minus=first - second;
printf("%f - %f is %f\n", first, second, minus);
jako=first / second;
printf("%f / %f is %f\n", first, second, jako);
}

This one may be longer than others, but it is very simple program. I'm sure you will understand it.

______________________________________ Copyright � 1999 KaWEB2k Webmaster ______________________________________