Code Mania

Tutorials target

These tutorials are intended for all levels of programmers.

A little advice before we start learning the C language

I just wanted to throw a couple of things before we start this journey of learning the C language. First of all, I want to welcome you to my page and thank you for being interested. Programming is a very challenging and frustrating subject, but it is always worth it in the end.

When I first took C in college, I was very confused because not only was it hard, but it was my first programming class. I passed the class, but I didn't retain too much knowledge from it, because I just did some exercises and didn't read the book thoroughly. Recently, I changed my outlook on programming because none of the languages that I was learning clicked for me. Now, I realized why I was feeling that way. It's because I wasn't using them. I was just learning them and that's it. I finally realized that learning several languages means absolutely nothing if my problem solving skills is very low.

I now understand that the fundamental concept of computer programming is to solely solve problems. Improving your problem solving skill will help you in more ways than you think. Not only would you know how to find solutions for your problems, but you would be able to implement the programming languages you learned to accomplish any task you want. That is why I urge you to not be discouraged if you don't understand some programming languages and to be patient. You don't need to understand all languages, but to know how to solve problems.

Getting Started

Why do we need the function main in a C program?

In every C programs, a function called main is required. In this function, you may write any instructions for the computer to execute. The reason we need that function is because it is a standard defined in the C language. For the program to run, the operating system has to give it momentarily control over the computer's resources through a shell (command line or terminal). To do so, the operating system has to know where to give that control which is when it encouncters the function main.

For example, imagine that you are a co-pilot and the pilot decides to take a nap for an hour or two. Common sense tells us that the pilot can't just let anyone manage the plane while he's resting. He has to lend that power to you, the co-pilot, until he wakes up. In this example, the pilot is the operating system and you, the co-pilot, are the function main in the program.

#include<stdio.h>

int main()
{
	printf("hello World \n");
	return 0;	
}

Looking at an example of the function main below, we see that main is preceded by an int and returns a value 0. Why is that? When the operating system passes that control to the function main, it also needs to know when to get that control back. Such as the example above. When the pilot wakes up from his nap, the control of the plane is given back to him by the co-pilot.

The operating system calls the function main and expects it to return an int usually 0 to indicate that your program has successfully executed its instructions and exited normally. If it returns anything other than 0, your program contains errors and exited abnormally. Either way, control is still passed back to the operating system.