Understand Structure of C Program

In this article, you will Understand the Structure of C Program and I gonna teach you in the very simplest way.

Before writing serious programs in C, it would be interesting to know how c programs are written. And for that, you have to Understand Structure of C Program. I gonna start with a very basic program that will cover all the important components of the Structure of C Program. Obviously, that program won’t be “Hello World” or “Welcome Program”. However, I will give you a link for that so that you can refer to that typical program also.

A C program basically consists of the following parts −

  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

Let’s Understand Structure of C Program

Let’s Write  First Program To Understand

Program in older IDEs like turbo

/* C Program To add two number */

#include <conio.h>
#include <stdio.h>
int main() //main function with return type integer
{
int a,b,c; //variable declaration
clrscr(); // clear console window
printf(“Enter two numbers that you want to add”);
scanf(“%d %d”, &a,&b); // input from user
c=a+b;
printf(“Sum of %d and %d is %d”,a,b,c); // printing result
getch(); // to hold screen to see output on console
return 0;
}

Understand Program Structure Step By Step

Since this is an article on Introduction to C Programming, so read each and everything slowly and carefully. Introduction to C Programming is necessary to build a foundation.

Have You enjoyed the article on Structure of C Program!

Header Files and Preprocessor Directives

  • The very first two lines are used to include statements written in specified files. Statements start from # symbol are known as preprocessor directives. Symbol # is followed by command include which tells to include the content of specified files.
  • The files stdio.h (standard input output) and conio.h (console input output)are one of the header files which contains the declaration of predefined function and declaration for other identifiers. Dot h(.h) is just extension written to denote it’s a header file.
  • The stdio.h file contains declaration for printf() and scanf(). The conio.h file contains declaration for clrscr() and getch().
  • In the place of an angular bracket (<>) to include the header, you can write double quotes (“”). However, there is a big difference between these notations.
  • C language is a block-structured programming language. Pair of curly braces forms a block “{}”. Block contains instructions. In the above program, block name is identified by a name main().

Variables and Functions

  • Execution always begins with main() function. The prefix of main() is an integer, which says it returns some integer value.
  • The line int a,b,c declares variables which will be used to hold data.
  • clrscr() is used to clear the content of the output screen. But, you will use the system(“cls”) in code:: blocks or newer version of IDEs.
  • The next line is used to print a message on the screen to provide direction to the user. This is done with the help of a function printf().
  • The function scanf() is used to take input from the keyboard. Input data is then stored in the variables a and b which is declared previously.
  • Next line is an arithmetic instruction used to calculate the sum of values stored in a and b and result is then assigned to variable c.
  • Again function printf() is used to show the result on an output screen
  • getch() is used to get a character from the keyboard and this function call is required to hold the output screen. full form of getch() is get a character.
  • return 0 indicates main() function is of the return type integer.
  • Semicolon (;) indicates termination of a particular instruction.

NOTE

This is not optimised program, however good at beginner level. There are several ways you can write this program: taking void main() instead of int main(), not talking extra variable c and just print a+b instead of c and many more combination. Don’t worry you will learn each and every method that is essential for you on this website.

Why this program is not an optimized program?

Because this program includes unnecessary variable (C). Since variable consumes space of RAM and hence slows down the execution of the program.

Obviously, While writing a program you must care about space and time complexity. In general term, you must focus on reducing the size of the program and the time of execution of a program.

But On code:: blocks or newer version of IDEs you didn’t have to write clrscr(), instead you will write system(“cls”); and you need not to write getch(). Although, you need not to write two header stdio.h and conio.h, however, it’s considered best to always include the header.

Have You enjoyed the article on Structure of C Program!

Understand Structure of C Program Through Story

Problem Statement

John and Rahul are two friends. John is a milkman and Rahul is a mathematician. One day, John planned to gain more profit by selling his 10-liter milk by mixing 5-liter water in it.

John discussed this problem with Rahul and asked him to give a solution.

Answer

  • Rahul thought john wanna cheat his customers. Since John is my friend I have to give him a solution. However, I will also cheat John by not providing an optimal solution.
  • He said John, you will need two buckets named a and b to store 10-liter milk and 5-liter water separately in each bucket. since the stored quantity was an integer, Rahul wrote int (short form of an integer) before the name of each bucket.
  • Semicolon to just give a mark of this step is done.
  • clrscr() or system(“cls”) is just a curtain to avoid other people to see forgery done by John.
  • main() is a block or room in which this forgery is done. Curly brackets are two doors of that room one is used to in any other is used to exit.
  • printf(” “) is a board to give instruction to John so that he could understand everything easily.
  • scanf(“”) is to take input from John how much water he wants to add to milk.
  • %d is used to tell John the quantity he mixed and the result that came is an integer, because of integer+integer= integer.
  • & symbol is used to identify an exact bucket.
  • C is another bucket to store the mixed milk and water that John gonna sell.
  • Again, printf() is used to show the final result.

In Term Of Programming

  • That two buckets a and b is two variables in the above Program and since stored quantity is an integer so variables are of integer type. That’s why it is prefixed before the name of variables a and b. Semicolon (;) is written to tell the compiler this statement or line is done.
  • clrscr() or system(“cls”) means clear console window or output screen.
  • printf(” “) is a pre-built function of stdio header file which is used to print a message inside double quotes to give instructions to users.
  • scanf(“”) is also a pre-built function of a stdio header file which is used to take input from users.
  • c is variable of integer type which is taken to store final result that came after summing up values stored in a and b.
  • main() is a pre-built function which is a default block inside which all the statements are written. Block starts with an opening curly bracket and closes with closing curly bracket. int before main denotes this function is having a return type of int. That’s why return 0 is written at last of a program.
  • %d is a format specifier for an integer type variable, because of integer+integer= integer.
  • & is an address of operator used to tell the compiler about the address of a specific variable before which it is prefixed.
  • Again, printf() is used to show final result.

<< Prev                                                                                                             Next >>


Your friends are waiting for this content. Please Share To Aware Them!