• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Basic » Procedure to Create a Program in C Programming Language
Next →
← Prev

Procedure to Create a Program in C Programming Language

By Dinesh Thakur

There are many “languages” like, for example C, Fortran, PASCAL etc., that help us to convert an algorithm in to something that a computer can understand. We will focus here only on C programming So. let us start looking at how do we create a “C program”.

There are basically three steps involved in converting your ideas into what is to be done to make it a working program.  There are three steps involved in converting your idea of what is to be done to a working program.

1. Creating a source code in the form of the text file acceptable to the compiler.
2. Invoking the compiler to process the source code and produce an object file.
3. Linking all the object files and libraries to produce an executable 16.

We will look at these parts in some more detail here.

The source code is where you express the the whole computation you want to do into a series of instructions.

The tradition is to use the C source code with an extension .c For example myfile.c . Some compilers insist on this extension. This is an acsii file that you can create using any one of the editors that you are familiar with. We will look at the detailed structure of this code in a little while .

We may have different parts of the code in different files. We then compile them separately or together to form object files. During this operation the compiler checks and convert your source code to a language understandable to the particular processor that is used in the computer. These object files that comes with an extension .o, for example myfile.o , are in binary format.

The next step is to link these object files and other libraries and header files etc., to produce an executable. This executable file contains a set of instruction in a language that the processor can understand and is the one which we run on the computer. We will see the use of header files and system libraries in the examples given below.

Before proceed let us summarize the steps that takes us to run a program in the form of a flow chart.

     Structure of C Programme

As we said earlier it is the tradition to write a C program file name ending with the extension .C.

Example: Below we give a program that computes the square root of a number and prints it on the scree. Since it is a C program we name it as “myfile.c” , that is with an extension “.c”.

#include 
#include 
main()
{       
            float x,y;       
                   scanf("%f\n",&x);       
            y=sqrt(x);  
                   printf("%f\n",y); 
} 

Before we proceed to the compilation of this program let us take a minute to see what does each of the lines mean?

Lines 1 and 2 are header files. These files contain the math library functions and the input output command functions. We need the math function library to use functions like “sqrt”, “log”, “power” etc. We need the stdio library to use functions like “scanf” and “printf” which reads and prints the data from the screen. More on this a little later.

The program given above just has a main body. Line 3 is declares the starting on this part. The lines inside the { } following this are the part of the main program.

Line 4 is the declaration of the variables. Again we will see more on the variable types later in this course. In this particular example we declare variables “x,y” as floating points.

Line 5 reads the value of the variable from the screen.

Line 6 computes the square root of the variable “x” and put the value into variable “y”.

Line 7 prints out this value of y.

We first compile this code to produce an object code using the command cc -c myfile.c or an equivalent command available at your machine.

The object file produced by this, myfile.o is then linked with the system math library using the command, cc -lm myfile.o -o myfile.exe , to produce the executable myfile.exe.

(the second term in the command above “-lm” links the object file to the math library. The file name that comes just after -o is the name of the executable).

We can combine compile and link into one step as will see in later part of the course.

Example: Let us now look at a C program that uses the library and another function.

#include 
#include 
main() 
{         
   float x,y;         
   printf ("%f\n",&x);         
   y=sqrt(x);         
   printf("%f\n",y);          
   printf("The program is Over"); 
} 

This simple C code adds an additional component to the one we saw earlier, it calls a function “printit”. As before you can see the “include” files, or what are called header files. Then we will see the main part of the program. This program is basically computing square root of the variable x and it is printing it out on the screen.

Then it is calling some function, which prints out a statement.Here we see on of the main feature of a C program. The main part does some of the calculations and it calls another function. The function we called, named printit, which simply prints the sentence “the program is over.” can be put it a separate file. Let is call it “print.c”. We will see that how it can be compiled and how it can be executed.There are two ways to compile this program.

To compile these programs we could type cc -c sample.c and cc -c print.c separately.This commands will create sample.o and print.o files. We can create the executable by typing the command.cc sample.o print.o -lm -o a.out In this linking command we have put together the two files and also included the math library file m.a We can now execute the program by typing a.out.You could try variations of these commands and programs to figure out why we need the math.h file and why we need to include -lm in the linker etc.

Also try it for different math functions, for example calculating the logarithm or trigonometric functions etc.Another way to compile and generate an executable from this is to simply type cc sample.c print.c -lm -o a.out In this the compilation and linking is put together.Let us try another program to illustrate the use of math library and the compilation and linking procedure.

 

You’ll also like:

  1. History of C? Why we use C programming language
  2. Write a program in C programming language to print weekdays using switch statement
  3. Difference Between Procedure Oriented Language and Object Oriented Language
  4. Difference between OOP and Procedure Oriented Programming
  5. Explain control statements those are used in C programming language
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW