Anatomy of a Program in C

Anatomy of a Program in C

Blessing Mufaro Kashava
·Jan 20, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Structure of a program in C

Structure of a program in C

Screenshot (202).png

• Code can be referenced according to line. As seen in the above image (left edge), numbers denote the line on which code is written.

• I will use the line numbers to occasionally reference to the code elements.

Comments

  • These are descriptions or explanations of a programmer’s source code.

  • They help to identify the purpose of a program and explain other complex routines (the specific task a block of code is designed to do).

  • There are two types of comments: Multi-line and Single-line comments.

A multi-line comment is seen on line 1 and line 2. The character set /* signifies the beginning of a comment block and */ signifies the end. This type of comment is used when your comment has several sentences.

\*My name is Mufaro and
this is a multi-line comment example*\

A single-line comment is seen on line 9. The character set // is used to signify a single-line comment. Single-line comments are usually short thus they only require one line.

\\single-line comments are brief

NB: During compilation comments are completely ignored because they are not written to perform any task, but rather to show other programmers or the code owner what specific lines of code do.

Preprocessor directives

  • They help alert the compiler of additional actions to be taken on the source code before it is compiled into object code.

  • Preprocessors are programs that perform preliminary processing on the source code before actual compilation.

  • Line 5 shows a preprocessor directive #include.

  • That statement on line 5 directs the compiler to include header files in the program before compiling it.

  • The pound sign (#) denotes that whatever comes after it goes into the preprocessor.

  • Therefore, the preprocessor is what allows for the use of header files in our C source code. Header files contains pre-defined functions (e.g. printf() ).

NB: excluding the preprocessor directive will not have any adverse effect during compilation or execution but header files allow the compiler to give better assistance in determining errors in your code.

Examples of preprocessor directives are #include, #define, #ifdef, #undef.

Commonly used header files in C

conio.h – console input/output

math.h – mathematical functions

string.h – string functions

stdio.h – standard input/output

ctype.h – character handling functions

time.h – date and time functions

float.h – limits of float types

wetype.h – functions to determine the type contained in wide character data

Functions

  • These are groupings of statements that perform one specific task.

  • They allow for grouping of a logical series of activities under one name.

  • There are two types of functions in C: library functions and user-defined functions.

Library functions are pre-defined in header files. Their task has already been stated and can be used to perform that task only. An example is printf() on line 10, printf() displays output on a console screen.

User-defined functions are created by the programmer. main() function on line 7 is an example of a user-defined function. However, although it is user-defined, main() is the primary function in every C program and should never be omitted.

After a user-defined function is declared we use opening ( { ) and closing ( } ) curly braces. In-between these braces is where we write our lines of code that will essentially perform our desired task. Within the curly braces we can have:

  1. Declaration Statements
  2. Decision Control Statements
  3. Loops
  4. Call to other functions
  5. Special

NB: for every statement within a C program we add a semi-colon (;) at the end to denote the end of the statement.

The following is a simple C program that displays the output 'Hello World!' on a console screen:

#include <stdio.h>

main()
{
    printf("Hello World!");
}
 
Share this