create account

"Learning ANSI C" - Most popular high-level computer programming language [Episode #02] by royalmacro

View this thread on: hive.blogpeakd.comecency.com
· @royalmacro · (edited)
$3.32
"Learning ANSI C" - Most popular high-level computer programming language [Episode #02]
https://images.hive.blog/768x0/https://i.imgur.com/4iGkBtz.png

Hello guys,
Welcome to new episode of "Learning ANSI C". In the previous episode we discussed about "Introductory part of C Language". Today we'll discuss on "Structure of Program in C Language". So, let's start-


## Structure of Program in C Language

Here I show you a simple program written in C language. In this program it calculates the profit from Investment & shows the result on the computer screen:


    /*A sample program written in C to calculate profit from Investment*/
    /* Programmed by @royalmacro */
    
    #include <stdio.h>
    #define ROI 7.88 //Here Return On Interest Value is defined as Constant

    int period=5; //Global variable declaration
    
    //main function
    void main()
    {
        float principal=10569.67; //Local variables
        float profit;
        
        profit=calculate_profit(principal); //Calculate profit by calling user-defined function
        printf("Principal:$%f USD : Period:%d Years : ROI:%f% : PROFIT = $%f USD",principal,period,ROI,profit); //Shows result on Computer screen
        
    }
    //User-defined function
    calculate_profit(x)
    float x;
    {
        float p;
        p=((x*ROI)/100)*period; //calculate profit
        return(p); // return value
    }

Output on Computer Screen:

    Principal:$10569.67 USD : Period:5 Years : ROI:7.880000% : PROFIT = $4164.44998 USD


Now please, close look at the above code. You see a well structure in that program. 

**Basic Structure of C Program**
1. Documentation Portion
2. Link or, Header File Portion
3. Definition Portion
4. Global Declaration Portion
5. main() Function Portion
6. Sub-program Portion

![structure-c](https://i.imgur.com/BRhfoWS.png)

**Documentation Portion**
Every program starts with "Documentation Section". It consists of a set of comment lines which describes the name of the program, purpose, programmer's name & any other program related info.

     /*A sample program written in C to calculate profit from Investment*/
     /* Programmed by @royalmacro */
In the above program this section is called "Documentation Portion".

    You see that I write text in Documentation Section within /* &  */ . 
Every texts start with /* (slash asterix mark) & ends with */ (asterix slash mark). To write a comment or text which will not be complied must be written in that format. Otherwise, program will not be complied correctly & will show fatal errors.

There are two types of commenting -- 
(i) Single-line Commenting
(ii) Multiple-line Commenting

**(i) Single-line Commenting:**
This type of comment must starts with double slash(//) or within /* (slash asterix mark) & */ (asterix slash mark). 

    #define ROI 7.88 //Here Return On Interest Value is defined as Constant
In the example program you see the use of single line comment.
You can write this comment in another format --

    #define ROI 7.88 /*Here Return On Interest Value is defined as Constant*/

**(ii) Multiple-line Commenting:**
This type of comment must starts with /* (slash asterix mark) & ends with */ (asterix slash mark).

    /*A sample program written in C to calculate profit from Investment.
    Programmed by @royalmacro */

So, we learnt that "double slash(//)" is only used for single-line comment but "/* (slash asterix mark) & */ (asterix slash mark)" is used both for single-line & multi-line comment.

**Link or, Header File Portion**
We previously learnt that C program is basically a set of functions. When we call a function C compiler search the definition of that function & executes commands from C library of "Functions". For doing this compiler needs to link functions from C library. The "Link/Header File Section" do that job.

    #include <stdio.h>
In the above program this section is called "Link or, Header File Portion".

**"#include"** is a preprocessor directive that specifies the files to be included in C program. **"stdio.h"** is a header file in which all standard input/output functions definitions are stored. In the above program we used **printf()** function, this function's definition is stored in **"stdio.h"** header file. Without linking correct header file the compiler will be unable to compile the codes.

To link a header file we write like this in Link/Header Section-

    #include <stdio.h>

or,

    #include "stdio.h"

We must use hash(#) before "include" then write the header file with/without file path. Every built-in header file is located in default root folder, so, we do not need include the header file with path. But, if we develop custom header file and want to include in Link Section then we must cite the full path like this--

#include <C:\TURBOC3\INCLUDE\AI.H>

**Definition Portion**
In the "Definition Section" it defines any type of symbolic constants. **"#define"** defines a macro substitution.

    #define ROI 7.88 //Here Return On Interest Value is defined as Constant
In the above program we define ROI as a constant and assign a value of 7.88. We can use ROI constant in any part of the program globally. In the program there is no chance to change the value of ROI.

In the above program we used ROI in user-defined function.

     p=((x*ROI)/100)*period;

When program executed it assigns the value of ROI to 7.88 as we defined first it in the "Definition Section". If we try to assign new value of ROI in main function or user-defined function then compiler shows fatal error. So, we can not change the value if we define it as a constant in "Definition Section".

**Global Declaration Portion**
In C program we can use two different types of variables - **Local** & **Global**. Local variables are declared in one & can not be used outside of that function. But, Global variables are declared in "Global Declaration Portion" & can be used in any functions of the program.

Global variable

    int period=5; //Global variable declaration

Local variable

    float principal=10569.67; //Local variables

Look **period** is declared as global variable in Global Declaration Section & **principal** variable is locally declared in **main()** function. You can use **principal** variable within **main()** function.  But, look, we used period variable outside **main()**.

**profit** variable can only be used in **main()**

    profit=calculate_profit(principal); //Calculate profit by calling user-defined function

**period** variable can be used in both **main()** & **user-defined function**

    p=((x*ROI)/100)*period; //calculate profit

**main() Function Portion**
C compiler starts to compile from here. Every C program must have one **main()** function. Contents of **main()** function must be appear between opening & closing braces. The C program execution starts at the opening brace "{" & ends at the closing brace "}". 

It has two sections--
(i) Variable declaration section
(ii) Execution Section

    void main()
    {
    // Variable declaration section
     int a,b;
     int c;
     
     //Execution Section
     a=12;
     b=45;
     c=a+b;
     printf("%d",c);
    }

Every function must have two **parentheses**, begin parenthesis "(" & end parenthesis ")". Every statement in the variable declaration part & execution part ends with a semicolon ";". And mind it that C is a case sensitive language. So, if you write printf() function as Printf() then it'll be not complied correctly. All functions & variable declarations are written in lower-case form.

     //main function section
        void main()
        { //opening brace
            // Variable declaration section
            float principal=10569.67; //Local variables
            float profit;
            
            // Execution Section
            profit=calculate_profit(principal); //Calculate profit by calling user-defined function
            printf("Principal:$%f USD : Period:%d Years : ROI:%f% : PROFIT = $%f USD",principal,period,ROI,profit); //Shows result on Computer screen
            
        } //closing brace


**Sub-program Portion**
The sub-program portion is a set of all user-defined functions which are called in the main() function. The example of sub-program-

    //main function
        void main()
        {
            float principal=10569.67; //Local variables
            float profit;
            
            profit=calculate_profit(principal); //Calculate profit by calling user-defined function
            printf("Principal:$%f USD : Period:%d Years : ROI:%f% : PROFIT = $%f USD",principal,period,ROI,profit); //Shows result on Computer screen
            
        }
        
        //User-defined function
        calculate_profit(x)
        float x;
        {
            float p;
            p=((x*ROI)/100)*period; //calculate profit
            return(p); // return value
        }

***

**Previous Episodes:**
[\[Episode-01\]](https://hive.blog/hive-148441/@royalmacro/learning-ansi-c-most-popular-high-level-computer-programming-language)

[To be continued....]

<b><i>N.B. This tutorial was also published on my steemit blog (ID @royalmacro)</b></i>
👍  , , , , , , , , , , , , , , ,
properties (23)
authorroyalmacro
permlinklearning-ansi-c-most-popular-high-level-computer-programming-language-episode-02
categoryhive-148441
json_metadata{"tags":["c","coding","programming","tutorial","include"],"users":["royalmacro"],"image":["https://images.hive.blog/768x0/https://i.imgur.com/4iGkBtz.png","https://i.imgur.com/BRhfoWS.png"],"links":["https://hive.blog/hive-148441/@royalmacro/learning-ansi-c-most-popular-high-level-computer-programming-language"],"app":"hiveblog/0.1","format":"markdown"}
created2021-05-16 08:44:21
last_update2021-05-16 08:46:06
depth0
children2
last_payout2021-05-23 08:44:21
cashout_time1969-12-31 23:59:59
total_payout_value1.670 HBD
curator_payout_value1.652 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9,064
author_reputation181,020,262,458,461
root_title""Learning ANSI C" - Most popular high-level computer programming language [Episode #02]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id103,737,531
net_rshares6,073,820,737,848
author_curate_reward""
vote details (16)
@hivebuzz ·
Congratulations @royalmacro! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@royalmacro/upvotes.png?202105210039"></td><td>You distributed more than 42000 upvotes.<br>Your next target is to reach 43000 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@royalmacro) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Check out the last post from @hivebuzz:**
<table><tr><td><a href="/hivebuzz/@hivebuzz/tour-update6"><img src="https://images.hive.blog/64x128/https://i.imgur.com/xecznXF.png"></a></td><td><a href="/hivebuzz/@hivebuzz/tour-update6">Hive Tour Update - Account creation and Account Recovery steps</a></td></tr></table>

###### Support the HiveBuzz project. [Vote](https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22109%22%5D&approve=true) for [our proposal](https://peakd.com/me/proposals/147)!
properties (22)
authorhivebuzz
permlinkhivebuzz-notify-royalmacro-20210521t103956000z
categoryhive-148441
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2021-05-21 10:39:54
last_update2021-05-21 10:39:54
depth1
children0
last_payout2021-05-28 10:39:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,153
author_reputation370,026,549,952,125
root_title""Learning ANSI C" - Most popular high-level computer programming language [Episode #02]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id103,843,027
net_rshares0
@ykdesign ·
thanks for your support and love 🙂
properties (22)
authorykdesign
permlinkre-royalmacro-rl4v3i
categoryhive-148441
json_metadata{"tags":["hive-148441"],"app":"peakd/2022.11.1"}
created2022-11-10 13:06:51
last_update2022-11-10 13:06:51
depth1
children0
last_payout2022-11-17 13:06:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length34
author_reputation244,217,860,979,883
root_title""Learning ANSI C" - Most popular high-level computer programming language [Episode #02]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id118,221,721
net_rshares0