PDA

View Full Version : The Basics of C


magiCash
10th August 2006, 01:54 AM
Very Basic C Programming - *In Progress*


C is a very powerful language used to create all sorts of programs from a simple calculator to full operating systems such as linux. If you are interested, read on.

A compiler is a program used to create programs...basically it takes the code and turns it into something the computer can read. This is all you really need to complete this tutorial other than a will to learn. I use an old version of turbo C for dos to compile programs, and this is what the tutorial will be over. This version Of Turbo C may be found at http://sun.uos.ac.kr/down/tc30.zip .

Note: All lines of code in C are terminated with a semicolon or ';'

Variables....

This how c stores information. A variable is defined at the beginning of the code or funtion and may be used throughout the program. They must be defined as to what the variable is storing.

Example:

int A=10;

This tells the compiler to store the intiger "10" as A. Other basic types would look like:
float A=10.2 floating point numbers are numbers with a decimal
Char A="b"; Charactors store...charactors obviously....they may be defined as the actual letter in quotes or the ascii number of the letter

Note: These all have a limit to the size of the numbers stored in them.

C uses a series of funtions and logical operators to run.

Funtions are a chunk of code that performs a specific operation on a variable.
a funtion usually looks like:

funtionname(variable1, variable2,...);

Logical operal operators are those cool things that let you compare stuff

if(this is true;)
{then this happens;}
else {otherwise do this;}

Note: the "{" and "}" brackets do NOT have a semicolon after them

ok.....so, now that we have the VERY basics down hopefully........lets move onto the basics

Heres a small program:

#INCLUDE <stdio.h>

main {

int a=1;

if (a=1;)
{
printf("a = %i", a);
}
else {
printf("a should equal 1, not %1", a);
}
return 0;
}



and the breakdown:

#INCLUDE <stdio.h> This defines a library full of funtions you can use...there are a TON of libraries...

main { tells the program where to start.....there are other things you can put outside the main function, but thats outside this tutorial

int a=1; defines our first variable as an intiger with the value of 1. note that the line ends in a semi colon
if (a=1;) this tell the program to look at our variable and see if it is a 1, which it is in this case

{
printf("a = %i", a);
}our "then" statement, since our if statement is true...this is executed. More on the printf function later.

else {
printf("a should equal 1, not %1", a);
} This is what would show if a didn't equal 1

return 0;
}ends the program and spits a "0" out to the OS. this is not important to you right now...just finish your programs like this.


Ok....the printf funtion earlier

printf is a function in the sdtio.h library
so:
printf("a = %i", a);

in the previous program would output this to the user:

a=1

More to come....stay tuned