cod-E-mphasis

C Programming: Part 2

Posted in C, Middle Level, Programming Language by Jayanta Karmakar on April 16, 2009

Hello friends! After a long time, I am back with the Programming Language C.

In regard of the last post and the comments posted by our readers, I must insist you trying all of these in Linux / UNIX also. In Linux / UNIX, you don’t need any 3rd party software line ‘Turbo C’ in Windows. In the OS itself, the ‘gcc’ (GNU’s C Compiler) compiler is preloaded. You just need to call the ‘gcc’ and then compile/run the program.

Now, we will go into the C programming!

Before everything, we will write a C program first.

Windows: Open Turbo C è write this code.

Linux / UNIX: Open terminal ècreate ‘hello.c’ in the VI editor è write this code.


#include <stdio.h>

#include <conio.h>

int main()

{

printf(“ Hello World.”);

getch();

return 0;

}

Now, we will look, what are these lines signify:

Line 1 and 2: These are called “Pre-Processor Directives”. We include these “.h” files rather “header files” in our program to use some of their properties. These properties are called “Functions”. Here, in line 5, 6 and 7, you will see some “printf”, “getch” and “return”…these are those Functions. The “printf” and “return” functions are from the header file “stdio.h” (the ’stdio’ means STANDARD INPUT OUTPUT and ‘conio’ means CONSOLE INPUT OUTPUT) and the “getch” is from the header file “conio.h”. Remember, other than these Functions, we ourselves can also write Functions as we wish.

Line 3: This line indicates the ‘Return type’ and ‘Name’ of a user written function (as I’d said in last paragraph). Now, to know what is ‘return type’ needs some more knowledge in C. So, we will discuss that later. Now, ‘name’. Here as we can see, after the ‘return type’ called ‘int’, there is the name of this function “main”. Now, a question arises, why ‘main’? The reason is, in any C program, there may exist more than 1 function and all of them are controlled by 1 function. Now, to get rid of the question, among those functions, which one is the 1st one, that controls other functions? The answer is: the main function. It’s like the main door of a building. After traversing through it, you can go anywhere in the building. So, here no other function exists, hence, the main door is the ‘main’ function.

Line 4 and 8: You can see these are nothing but, an opening parenthesis and a closing parenthesis. This opening means the starting of any function and closing means the closing of the same.

Line 5, 6 and 7: “printf” is a function, whose job is to print something. Inside the two brackets, there are two upper commas. Inside those ‘upper commas’, if you write some thing, that will be shown to you in the monitor when that program will run. Similarly, “getch” and “return” has some jobs to do. These jobs will be cleared to us when we will have some more knowledges in C.

One more thing, if you have noticed, you can see there is a semicolon (;) after every line inside the main function. This semicolon indicates the ‘End of the Line’.

Now, to compile:

Windows: (in Turbo C) F2 (to save the program) è Alt + F9.

Linux / UNIX: (after saving the program in VI editor) gcc -c -a hello.c

Now, to Run:

Windows: (after compilation) Ctrl + F9.

Linux / UNIX: (after compilation) ./hello.c

Tagged with: , ,