Thread: edit?
View Single Post
Old 11-15-2002, 03:15 PM   #3 (permalink)
anon
Guest
 
Posts: n/a
Bit screwy... Moving it from pico to Abiword to Wordpad...



This is a basic C tutorial. First
off lets discuss what is C. C is an object-oriented language. It was
created in around 1970 by Dennis M. Ritchie. You should have a compiler for this tutorial. I use cc and gcc but if you are on Windows
you can use Borland's C compiler. First off lets look at the basic syntax of C.

#include<stdio.h> //That calls a the stdio.h library for C.


main() //starts the main function.
{ //opens the function
printf("This is C!"); //prints This is C!
} //closes the main function

First off see the ; after printf("This is C!"). Well there needs to be ; at the end of most lines code. Or else the C code will not work.
If you don;t already know // stands for a comment. It is good practice to comment your code. /* */ is also used for comments for
example

printf("This is C!"); /*Prints this is C!
printf("it is!"); */

would only print This is C! Because printf("it is!"); is between the /* and the */. All C files that include C code that haven't been
compiled should be named something.c where something is the filename that you want. For some compilers if it's not a *.c file it wont
compile it! Now if you compiled a program with

printf("This is the first line!");
printf("This the second line!");

would write This is the first line!This is the second line!. Why? Just because you put it on different it doesn't move it over one line.
You need to write:

printf("This is the first line!\n");
printf("This the second line!");

the \n moves the next thing that will be printed over a line. In C \*something* can do something. Like \n moved text over a line well
there are other things that \*something* can do. so if you want to write something like:

printf("I love \!");

Well, Its going to think you mean \! not just \ as text! so it will try to find out what \! means and does instead of printing \! So what do
we do to write a \ to screen? \\

print("I love \\!");

\\ stands for \.
\? stands for ?
\' stands for '
\" stands for "

Now, lets get into strings! First of all in programming there are 3 main types of data. Boolean, true or false,
number/variable, number, and string, which means a string of letters. But just "a" can be a string too. But all numbers can be a variable
or a string. Because you can have 902 as a variable. Or you can have 902 as a string by itself or as a part of a string like "Once there
was a great boy called Vlad who went by v902." the 902 in there isn't a variabiable. So lets make a very basic program where we declare a
string called myFirstString.

#include<stdio.h>

main()
{
char myFirstString[100]="I love Strings!"
printf("%s",myFirstString)
}


Here we DECLARE myFirstString. We declare it by saying what it is. It is a string. As you can see it says char myFirsString. The char
stands for character. And when you say char something you make something a string. The [100] stands for how long the string can be. Here I
put 100 but it could be smaller or larger. Then we write ="I love Strings!". Which tells us what myFirstString is equal to. Then we print
myFirstString to the screen by doing printf("%s",myFirstString). The %s is there to reserve space for where myFirstString will be. For
example lets say we wanted to print to screen "The contents of myFirstString are: " and then here were the contents of myFirstString. We
would write

printf("The contents of myFirstString are: %s",myFirstString)

This will show The contents of myFirstString are: I love Strings!. Because we reserved a place where the contents of
myFirstString would go. Lets try something with reserving places

#include<stdio.h>

main()
{
char myFirstString[100]="First String!";
char mySecondString[100]="Second String!";
printf("myFirstString: %s\nmySecondString: %s\n",myFirstString,mySecondString);
}

I believe thats pretty self-explanatory. Now a string in C is actually an array of characters. First lets discuss what an array is. In an
array there can be infinite amount of characters. Imagine this. You have to make a program. The program has to hold every single person in
a bussiness names. Now how are we gonna do that? Make 3000 different strings?!?! No. We will make an array. Lets say we have an array
called myArray. And it holds the values, J, S, C and X. We could access those values easily by doing myArray[0] for J myArray[1] for S,
myArray[2] for C, etc.. Because computers start counting at 0 not 1. So here is what myArray looks like

myArray

0 1 2 3
J S C X

and strings are arrays, arrays of eltters, so myFirstString is actually

1 2 3 4 5 6 7 8 9 10 11 12 13
F i r s t S t r i n g !

Where myFirstString[0]=F and myFirstString[1]=i. So a string is actually an array of letters.


Now lets go onto numbers! While it was simple for strings because we only had char in numbers we have more ways to declare variables. We
have:

int-for small numbers, usually between 33,000 to -33,000
short-for short integers
long-for long intergers
float-floating point number. It can have a decimal in it and they are usually between 10^38(10 to the 38th power) and -10^38
double-double float, huge!

Lets try something now


#include<stdio.h>

main()
{
int myVar, myVar2;
myVar=20;
myVar2=30;
myVar=myVar+myVar2;
printf("%i",myVar);
}

Now it should print 50. We declared myVar and myVar2 integers because of the int. Then we said that myVar=20, and myVar=30. Then we said
myVar=20+30. and then printed myVar. As %s stood for string, %i is for integer. Now here is something you haven't learned before that you
should learn now as to save you some time debugging. You need to declare all your variables and strings in the very start of the code.
Like this wouldn't work:

#include<stdio.h>

main()
{
int myVar;
myVar=20;
int myVar2;
myVar2=30;
myVar=myVar+myVar2;
printf("%i",myVar);
}

Because you declared myVar, then had a line of code, then declared another integer. You can't do this. ALL intergers and strings have to
be declared first then code can be written. This would work though:

#include<stdio.h>

main()
{
int myVar=20;
int myVar2=30;
myVar=myVar+myVar2l;
print("%i",myVar);
}

Here are the very basics. I plan to write another tutorial, you may want to check that one out, it will cover loops, if then, functions,
and other more advanced things
  Reply With Quote