Tuesday, 17 May 2011

The tutorials in this site are aimed primarily at beginning level programmers, including those who have little to no prior programming experience. Intermediate level programmers will probably also find plenty of tips and tricks that may be of use in improving their programming skills.

Have you always wanted to master a programming language. The C programming language is a popular and widely used programming language for creating computer programs. Programmers around the world embrace C because it gives maximum control and efficiency to the programmer.If you are a programmer, or if you are interested in becoming a programmer, there are a couple of benefits you gain from learning C,Well atleast the basics that will help you to build more powerful programs.You say I can't show you C in 5 hours.

A simple hello program.demonstrates the const function in all c programs--the main() function.Learn C by example in 5 hoursExample 1 main()
{
puts("hello world guess who is writing a c program");
return(0);
}

That's it. In all c programs there is a main function which is followed by a { and closed by a } after a return()function.It doesn't have to be return(0) but that depends upon the type of c compiler you have. Check your compiler before you start your programming.

You saw above that puts function is used to put a whole sentence on the screen; but are there functions that will put characters on the screen/take characters: Yes and next is a table of what they are and what they do. Read them and the examples that follow.

getchar()

Gets a single character from the input/keyboard.

putchar()

Puts a single character on the screen.

The printf function is a function used to print the output to the screen.printf() needs to know if the output is an integer,real,etcLearn C by example in 5 hoursExample 2

main()
{
printf(hello);
} Assuming hello was defined earlier say by #define hello "Hello!" the output is Hello!. But if the output is an integer then %d has to be attatched to the printf statement.

This above can be shown as printf("I am %d years old",12) which will result in the following result:I am 12 years old

The %d tells that an integer is to be placed here.

Now we will look into a function called scanf().This lets you input from the kewyboard and for that input to be taken by the program and processed.Once again it is important to tell scanf() what type of data is being scanned.

That concludes the first hour of your tutorial.Now this is a list of data type identifiers.

%f=float %c=char %s =s tring %e=inputs number in scientific notation.

As you saw in the first hour of our tutorial c is a language in which you program using functions. Functions are usually identified by the following characteristic:>> functionname() In c the main() function is essential. Think of it as a constant function for all your programs and all other functions can be accessed from the main().Before I show you how we do that let us have an example where we want to pause a program before the screen is changed. This would involve the foll- owing procedure:>> write a main function then use puts function to put statements on the screen like we did in section 1 above and then before the next set of puts statements declare a pause.

This above will pause until a key is pressed on the keyboard. Granted that the above program makes no sense from a practical point of view but I want to show is the use of another function inside the main function.

C has many functions that comes with it. See your compiler manual to see what you have.Now we are going to look at conditions in c programming:>> the if command and do command.

This is a simple example of one if statement. Another If statement is the if -else statement. This can be shown as this Learn C by example in 5 hoursExample 6 if(cost >40000) { luxury=cost*0.005; printf("The luxury tax is %.2f",luxury); } else { puts("There is no luxury tax for the items"); luxury=0.0; }

Now I am going to show you how to pass an array. When you pass an array you are actually passing the address of the array.Learn C by example in 5 hoursExample 10 #define count 31
main()
{
int temps[count];
int index;
float celsius;
for(index=0; index< count;index++)
{
celsius=(5.0/9.0)*(heat[index]-32);
printf("%d\t\t%6.2f\n",heat[index],celsius);
}
}

Now we are in the fourth hour of our tutorial.We are now going to look at 1)comparing strings 2)determining string lengths. 3) combining strings 4)structures.

Comparing 2 strings:

In c it is not possible to directly compare two strings so a statement like if (string1==string2) is not valid. Most c libraries contain a function called the strcmp().This is used to compare two strings in the following manner.

if(strcmp(name1,name2)==0) puts("The names are the same"); else puts("The names are not the same.");

Determining string length.

This is done using the strlen() function.a simple programming bit showing this function looks like this:

The assumption being that adam and eve are two values of the variables name1 and name2. The end result is the combination of the 2 names.

What are structures?

A structure variable is a collection of other variables comprising different types.

What are pointers?

Ponters are variables which refer to the memory locations of other variables. This is how a structure is defined.Learn C by example in 5 hoursExample 12struct cd
{
char name[20];
char description[40];
char category[12];
float cost;
int number;
};
main()

Notice how the main function comes after the definition of the structure. In the example above the cd was a cd disk and I was writing the definition of a cd collection program.

Now in the fifth hour I will show you how to output your data onto a disk.After all what is the use of the program if you can't save output to a disk.

Inorder to do this we have to use a pointer. The pointer in this case is FILE. The syntax to declare a file is :FILE*file_ponter;

The link between your program, the file and the computer is established with the fopen() function using the syntax shown below: pointer=fopen("FILENAME","mode");

For example to create a file by the name cd.dat we do the following:

FILE*cdfile; cdfile=fopen("CD>DAT","w");

If you will be reading from the file above use "r" instead "w" in the
second sentence.

In order to rpint information use the following command: FILE*cdfile; cdfile=fopen("PRN","w");
A file is closed by using the fclose() command.Next we will look at an exam ple of reading from a file.Learn C by example in 5 hoursExample 13 #include "stdio.h" main() { FILE*fp; int letter; if((fp=fopen("MYFILE","r"))==NULL) { puts("Cannot oepn the file"); exit(); } while((letter=fgetc(fp)) !=eof) printf("%c",letter); fclose(fp); }

The eof statement means end of file and this is included in the stdio.h header file which was declared at the start of the example. The stdio.h header file is one of many that comes with your compiler. So check your compiler specifics for other header files which will help perform other functions.

In this Site you Learn C in 5 hours Now you should be in a position to write simple programs in C and know every most common thing in C.