Before we go to complex c programming, we need to know the basic C Program Structure. A basic C Program structure is generally consists of the following components –
- Preprocessor Directives: These is the lines included in our program which is proceed by the character #. These are invoked by the compiler to process some task before compilation of the program. Preprocessor directives process the text of our source code and generate a new source code without these directives for the compiler which is actually executed by the compiler.
- Functions: A function is a collection of statements which together perform a task. Every c program has at least a function that is the main(). A trivial c program may contain some additional function.
- Variables: A variable is nothing but a name given to a storage location in the memory and our program can manipulate this storage area.
- Statements & Expressions: A statement is a command line in c program which instruct the processor to perform a task such as display a message etc. Expressions are the legal combination of symbols that represents a value. Every Statements or Expression in a c program end with a ‘;’.
- Comment lines: Comment lines are used in any program for future reference. It help others to understand the code for what it is intended to.
Example of a basic C Program Structure
Let us look at a simple program that will print the line “Hello, World!“.
Now let us analyse the above program. Below are the breakdown of the above program.
- The first line “#include <stdio.h>” is a preprocessor directive. It tells the compiler to add the header file “stdio.h” in our program before compilation of the program. This header file is included to our program because we are using printf() function in our program which is a built in function and present in the header file “stdio.h”.
- The second line “int main()” is the main function of our program and from this line the program execution is begins. Every C Program must have the main function.
- The next line “// my first program in C” is a single comment line. If you want to add multiple comment line then you can write your comment as “/* your comment*/“. The compiler ignore all the comments line in a program.
- The next line “printf(“Hello, World!”);” causes the line “Hello, World!” to be appear on your computer screen. The printf() function is use to display a message in your computer screen. It is a built in function which is present in headerfile “stdio.h“.
- The last line “return 0;” is use to terminate the execution of the main function and return a value 0. Returning 0 indicates normal termination of the program.
Compile and Execute C Program
To create, edit, compile and run a C program different platform have different ways. As we use Windows platform and CodeBlocks compiler, below are the steps to create, compile and run a c program.
Create and edit a c program:
- Open CodeBlocks and click on “Create a new project”.
- Select “Console application” and click on “Go”.
- The select “C” and click on “Next”.
- Then set a “Project title”, click on “Next” and the click on “Finish”.
- After that from left panel, click on “main.c” present in Source file of the “Projects” tab.
- Then a window will come up and write the above program on that.
Compile and run a c program:
After writing your code follow the steps to compile and run your program.
- Click on “Build”.
- Then click on “Build and run”.
- A popup will appear after this which will show you the message “Hello, World!”.