Tuesday, December 27, 2011

2. First Program in C

If you are new to programming and you haven't run any code before, refer to this link first.
 And yes I'm not starting with the history of C language. But if you are curious to know about it, refer this link.
Now refer to this first basic program.

#include<stdio.h>
main()
{


printf("HELLO! WORLD");
}


Write this code in your editor(notepad, nano, vi, dev c++) and save it with .c extension.
Now you need to compile this file. To compile in dev c++ use Ctrl+F9 and to compile through gcc use: gcc FILENAME.c

Compilation is necessary to run a program. It is way through which the human understandable text is changed into machine understandable or executable code. Though we created machines but we aren't able to understand them, they have been made too complex...So compiler is same as like a translator that helps to communicate between two persons speaking different languages. Once your program has been compiled you are now ready to test it i.e. run your code and see the output.
To run your code in dev c++ use: Ctrl+F10 and to run code compiled earlier using gcc use: ./a.out

What you get? It's HELLO! WORLD printed on the screen.
That's what our code do.
#include<stdio.h> tells the compiler to include a header file named stdio.h to our current source program. What does these header files do?? why these header files are included and can we neglect them to make our code easier? The reason to include stdio.h to our code is that we need to tell the translator(compiler) that what does the printf command do? These commands are created by someone else and we are using these commands to make our work done. Compilers do have very little knowledge of the work. For eg. we don't have any instruction to print the output on the screen, seriously we don't have. So we must create it. But thanx to the other guy who made a code printf and makes our work easier. Now we just have to tell the compiler that we are using this code/function(printf) and the instructions to execute this code/function lies in this particular header file.

Similar to printf, main() is also a function, more precisely a user defined function that tells the compiler that our code starts from this very first line in this body and it must start executing every single instructions afterwards, till the end of the body. The start and end of the body is defined by the set of curly braces( { } ).

Now this is the very first program and each entity has been defined in a very basic level to get start up. If you think that I have missed something or something must be added to make the topic more clear, then every suggestion and queries are warmly welcomed.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Host