Programming in C (Series)
Welcome to the first part in the Programming in C Series.
First you will need a compiler to compile the code for you. Here are a few:
Miracle C Compiler
DeSmet C Compiler
MinGW C
DJGPP
Other Compilers Here
After you have downloaded and installed the Compiler correctly we can start learning how to program. Although we are getting into if statements we can still learn the basic framework of a program. Here it is:
#include <stdio.h>
int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */
printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is
*/
}
return 0;
}
Now, if we look at the structure of the program we see many elements that do different things. The beginning of the program, #include <stdio.h>, tells the compiler to include the standard library, which will probably be included in nearly 100% of the programs you develope. The int main() function tells the compiler that it is entering the main part of the program. Following is the { key startin somewhat of a section. Nearly 100% of all programs will include this as well. The int age; function tells the computer it is entering the age and/or allowing for further functions. So you could have changed this, if the rest of the program was readjusted, to something like int whatever;. It ends, though, with a semicolen which looks like this: ";" without quotes. It is needed in many cases to end a specific line.
The printf(... part is telling the computer to print whatever is following. It will almost always end with a end quote and a \n to represent a new line. The scanf(... tells the computer to ask the person what their age is to input it into the computer and output based on the inputed information. Following is an if(... part that tells the computer if a certain number is less then whatever the specified number is, in this case 100, then print this. (It is shown using the printf(... command.) The rest is stuff we have already explained, but then follows the else if command that tells the computer that if none of the preceding commands are "true" then display this. The else command is contiguously included in the end of if(... statements. The program ends with return 0; and }.
One extra statement displayed in the source code is the statements such as: /*This is important!*/. Those are statements that are completely ignored by the compiler. So you could create an entire program with the /* and */ tags on it and the compiler wouldn't essentially be able to display what you intended.
You now have gone through a functioning C Program that asks your age and tells you it's decision. You can continue to the following article in the series to explain further of programming in C, as the link is at the top.
Part 2: Further explanation of structure
Part3: Loops
Part4: Pointers
Part5: Arrays