Thursday, June 3, 2021

Getting started with C

How compiled program works? a old-traditional “hello world!”

Since C is a compiler based programming language, executable program must be created in two stages:

1. First, create source code by writing instructions in it and save it as .c file.

2. Second, compile this source file and generate binary file or executable file.

 

The C is called as POP or Procedural Oriented Programming language. It means it contains procedures or functions as main building blocks. One such function is called main(), a zero level or driver function.


Always in C, program is addressed to the driver function main(). Hence the pseudo code is as follows:

OK Computer [main()]:

Do nothing, no tasks

 

Output:

     [Nothing happens, no output]

is written in c language as:                                         

 main()

{

//Do nothing, no tasks

     }

Output:

     [Nothing happens, no output]

Here ‘//’ indicates comment, used for documentation of the code. It will not take part in actual program execution. So nothing prints here.

 

Suppose programmer wants to greet his user a “Hell World!” message, then he can write it as follows:

OK Computer (main()):

Display “Hello World!”

 

Output:

     Hello World!

is written in c language as                                          

 main()

{

    puts("Hello World!");

}

Output:

     Hello World!  

Here the puts() function is another one supplied by C language for putting string on display window. Before using it programmer needs to add the or include the library file stdio.h where puts()is declared.

So the actual implementation c code for above example is as follows:

#include <stdio.h>

void main()

{

    puts("Hello World!");

}

Output:

          Hello World!

Copy above code and paste it in new code file called main.c [or type it], compile it and run it to see the greeting on console window.

The above programs code line explanation is as follows:

-including library file or header file stdio.h from c compilers include folder.

-addressing to main() function which returns nothing [void] to operating system.

-code block starts of main()function.

-calling puts()function with argument “Hello World!” once.

-code block ends of main()function.

 

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

No comments:

Post a Comment