0.7 – Start Writing C Programs

by subbu on July 22, 2013

The first C program:

Now we have enough knowledge to write a simple C program. Declaration statements, assigning statements and output statements are common executable statements (instructions) in any program. Unlike other languages, in C language instructions are placed under a collective name called main(). To specify the scope (boundaries) of main(), statements are placed in a pair of braces called scope delimiters { }. Technically main() is a function, execution of any C program starts and completes from the main().

In the structure void/int before the main() and return 0; at the end of main will be covered later in the functions chapter.

#include<stdio.h> is a preprocessor statement helps to use scanf(), printf() like input and output functions in program. These will be covered later in preprocessor directives session.

 Structure of a simple C program:

C program structure

  1. It is to include a header file called stdio.h, which allows to use input and output functions like scanf, printf etc (Covered later).
  2. It is the point from where program execution starts.
  3. Start of main() scope.
  4. Declaration section, where all the variables used in a program are declared.
  5. Assigning, input and output statements.
  6. End of main() scope.

 Note:

The main() function must not be terminated with a semicolon because it is a function not an instruction.
When we specify the int before main() then it is mandatory to write return 0; at the end of main().
We don’t write return 0; when we write void before the main()
In case of  Turbo C  We neither write void nor int before main()

Steps in writing a C program:

The common steps in writing any C program are declaration of variables, accepting the data from user, manipulating the result using assigning statements, printing the output on the monitor, placing the total program within main() and finally executing the program

Here we will see all the steps using a simple program which takes two numbers and print the sum of them.

Step1: All the variables used in a program must be declared in the declaration section of the program that is before writing any executable statements (Can be declared any where in ANSI C11).

int fir;
int sec;
int sm;

If all the variables are of the same type then all the variables can be declare in a single line using comma separator.

int fir,sec,sm;

Step2: Writing the assigning statements according to the business logic.

int fir,sec,sm;
fir=45;
sec=30;
sm=fir+sec;

Step3: Printing the result on to the monitor using printf()

int fir,sec,sm;
fir=45;
sec=30;
sm=fir+sec;
printf("Sum is %d",sm);

Step4: Placing all the program within the main()


#include<stdio.h>
int main()
{
int fir,sec,sm;
fir=45;
sec=30;
sm=fir+sec;
printf("Sum is %d",sm);
return 0;
}

Step5: Executing the program using proper IDE (Integrated Development Environment). Execution procedure changes IDE to IDE.

The second program:

Specification: Take the price, quantity of a product and print the bill.

Step1: In this example we need an int type variable to store quantity and two float type variables to store price and the bill. Hence declaring qty with int and price, bill with float

int qty;
float price, bill;

Step2: Writing the business logic using assigning statements

int qty;
float price, bill;
qty=25;
price=15.75;
bill=price*qty;

Step3: Printing the output onto the monitor using printf(). Here the value of bill is being printed using %f, which is the format specifier for float type variables

int qty;
float price, bill;
qty=25;
price=15.75;
bill=price*qty;
printf("Total bill %f",bill);

Step4: Placing all the program within the main()

#include<stdio.h>
void main()
{
int qty;
float price, bill;
qty=25;
price=15.75;
bill=price*qty;
printf("Total bill %f",bill);
}

Previous post:

Next post: