Explain the Structure of C Program: Getting started with C and Embedded C (Study C in 7 days)

Describe the structure of C program (2nd day)

This is the second day of our 7 days C tutorial. If you have not come across the 1st day lesson, then you can follow Getting started with C and Embedded C programming: Study C in 7 days . Now C language has its own structure to write a program, that is there are particular rules for writing codes in C. These rules are called Syntax Rules. All the codes written in a C program is based on these syntax rules. We can classify a C program as many sections, the different sections in a C program are followed, this is a general way of representation.


Documentation section

Name of program, name of programmer, date etc. are included in the documentation part. These are non executable. To start and stop documentation we are using /* and */ command. For example,

In Embedded C we are using ‘//’ for commenting like,

Linkage section

In this section we provide instructions for linking different functions from the system library to the compiler. This section is also known as Pre Processing stage, because this will run before the main() at the time of execution. The file name starts with ‘#’ symbol. For example, #include. Here we included the library function stdio.h in our program. stdio.h contain standard input output functions. Without the stdio.h header file the commands printf(), scanf() etc will not work.

Global variable declaration section

The variables declared in this section are accessible from any program. Any function other than main() can use this variables. For example,

Here the variables a, b, c are global variables. The main() and function1() can also access these variables.

Main() function

We can group some codes together, these grouped program codes are called functions. Main() is the entry function of any program. It indicates the starting of program. There are two sections in main function(),
1.Declaration part
In this part we declare all the variable used in the main() program. The variables cannot be accessible from outside of main() function. For example


2. Executable part
Different operations are included in this part. For example
We can write main() function once in a program, so main() works as a built in function. The user combines all other functions in main() section.

User defined functions

The functions defined by user and called from main() function are known as user defined functions. All the user defined functions are written after the main() function. In Embedded C, user defined functions are written before main().

Comments and Remarks

/* and */ are used anywhere in the program to indicate some information to another programmer, this will help the programmer in the Debugging process also, another user can easily understand the logic. For example

But in Embedded C we use '//' for commenting. Like,

So that's all for the 2nd day tutorial. Check out other Study C in 7 days tutorials:
Ads


0 comments:

Post a Comment

 

Copyright © 2011 CircuitsGallery| ToS | Privacy Policy |

Contact Us | Write For Us | Advertise on Circuits Gallery | About Us