Categories : C

 

This is the first in a series of tutorials designed to help newbies to the field of C programming become more aquainted with the principles and particulars of programming in C. Later on we’ll get into structure, pointers, and more advanced algortihms, but for now, let’s just become friends with C.

Why C?

C is a glue language that has been around for a long time. It is at the heart of C++, Java, C#, and others, and still one of the most useful languages around. C is extremely versatile, even more more so than Java, with almost nothing officially designed. However, with the use of header files, C is every bit as expandable as Java.

If you want to write a Linux device driver, do it in C (the Linux kernel itself is written in C). If you want to write a handy utility that you don’t see in the /usr/bin, a program compiled in C will execute more quickly than in Java. But enough talk, let’s get to it!

                          
#include                  
                          
int main(void)            
{                         
 printf("\nHello World!");
                          
 return 0;                
}                         
                          

Okay, so much like a Java Program, the code must be commented. Comments will make the code readable years down the line when you want to revisit a program to make upgrades, and comments will also help others who may be looking at your code (maybe to help you write it) understand your logic. You can also use comments to remove pieces of the program that you suspect may be buggy without actually deleting those lines (for when you find out that they work fine). So before I say another word, I will comment my code.

                                                                           
#include  //include he standard IO library                                 
                                                                           
int main(void)     //the main function returns an int and takes in nothing.
{                  //open function body                                    
 printf("\nHello World!");  //print "Hello World!" to the screen.          
                                                                           
 return 0;                  //return nothing.                              
}                                                                          
                                                                           

Ah, that’s much better, now let’s take a look at it piece by piece.

#include

The include statement tells the compiler to look for the following header file when it compiles the program. This lets the compiler know that I’m using a fucntion defined somewhere else (printf()), and allows me to include that function in the program without writing it myself.

int main(void)

This is where we define the main function of the function. The main function is the point of entry into the program. It doesn’t matter where in the source the main function is, because that’s where the Operating System will begin execution. Like the main method in Java, you don’t actually need to have a main function in your program, but unless your functions are called by a mian function somewhere, they won’t get used.

I’ve declared my main function to have a return type of int, and to take in no parameters as its argument.

{

This is a left curly brace. You use it to open the body of a function definition. Similarly you use a right curly brace to close the body of a function definition. These should be pretty self-explanatory from this point on, so I won’t discuss them further.

printf("\nHello World!");

This is the meat of the Hello World program. The printf function is defined in the stdio library, and since we included that library, we can use the function. printf is a formatted print stream, but the mechanics of that are best left to a more advanced tutorial.

The \n is the newline character. It will place the line of output on a newline of the terminal when the program is run. Hello World! is, of course the string we put to stdout.

Note that the line ends with a semi-colon. This is how commands are ended in C.

return 0;

This is a return statement. I told the function to return a value, and now I have to return one. I could just as easily have declared the return type to be void, because this does the same thing.

Well that’s about it. Stay tuned for more fun tutorial program dissections!

 Posted on : August 22, 2014
Tags:

You might also likeclose