Introduction to C Programming
By Paribesh Sapkota
#What is programming? Describe the types of programming language with appropriate
Explain different types programming language with their merits and demerits
Ans: The process of developing and implementing various sets of instructions to enable a computer
to do a certain task. These instructions are considered computer programs and help the computer
to operate smoothly. The language used to program computers is not understood by an untrained
eye. Computer programming continues to be a necessary process as the Internet continues to
Two Basic Types of Computer Language:
• Low-Level Languages: A language that corresponds directly to a specific machine
• High-Level Languages: Any language that is independent of the machine
Low-level computer languages are either machine codes or are very close them. A computer
cannot understand instructions given to it in high-level languages or in English. It can only
understand and execute instructions given in the form of machine language i.e. binary. There are
two types of low-level languages:
• Machine Language: a language that is directly interpreted into the hardware
• Assembly Language: a slightly more user-friendly language that directly corresponds to
Machine Language (1GL):
Machine language is the lowest and most elementary level of programming language and was the
first type of programming language to be developed. Machine language is basically the only
language that a computer can understand and it is usually written in binary digits (bits) 0 and 1.
Since a computer is capable of recognizing electric signals, it understands machine language.
Advantages (merits) of Machine Language.
• Machine oriented Language.
• Machine language makes fast and efficient use of the computer.
• It requires no translator to translate the code. It is directly understood by the computer.
Disadvantages (demerits) Machine Language.
• All operation codes have to be remembered
• All memory addresses have to be remembered.
• It is hard to amend or find errors in a program written in the machine language.
Assembly Language (2GL):
Assembly language was developed to overcome some of the many inconveniences of machine
language. This is another low-level but very important language in which operation codes and
operands are given in the form of alphanumeric symbols instead of 0’s and l’s. These
alphanumeric symbols are known as mnemonic codes and can combine in a maximum of fiveletter combinations e.g. ADD for addition, SUB for subtraction, START, LABEL etc. Because of
this feature, assembly language is also known as ‘Symbolic Programming Language.’ It is not
very user friendly but execution is fast.
Advantages (merits) of Assembly Language:
• Assembly language is easier to understand and use as compared to machine language.
• It is easy to locate and correct errors.
• Program execution is faster than high level language.
• It is efficient in program execution. Hence, ALL is still used is developing firmware, device
driver and operating system kernel.
Disadvantages (demerits) of Assembly Language:
• It is machine dependent; the programmer also needs to understand the hardware.
• the good knowledge of machine architectures is required.
• Program development and debugging is more difficult and time consuming than is High
High-Level Languages (3GL):
High-level computer languages use formats that are similar to English. The purpose of
developing high-level languages was to enable people to write programs easily, in their own
native language environment (English).High-level languages are basically symbolic languages
that use English words and/or mathematical symbols rather than mnemonic codes. Each
instruction in the high-level language is translated into many machine language instructions that
the computer can understand.
Advantages (merits): of high level Language.
• High-level languages are user-friendly
• They are similar to English and use English vocabulary and well-known symbols
• They are easier to learn and maintain
• They are problem-oriented rather than ‘machine’-based.
• A program written in a high-level language can be translated into many machine languages
and can run on any computer for which there exists an appropriate translator
Disadvantages (demerits): of high level Language:
• A high-level language has to be translated into the machine language by a translator, which
takes up time
• Every programming language must have its own translator because high level language can’t
directly generate executable code.
• The object code generated by a translator might be inefficient compared to an equivalent
assembly language program
• Computer does not understand HLL directly, so the program needs conversion before
The Top-Down Approach
In the top-down approach, a complex algorithm is broken down into smaller fragments, better known
as ‘modules.’ These modules are then further broken down into smaller fragments until they can no
longer be fragmented. This process is called ‘modularization.’ However, during the modularization
process, you must always maintain the integrity and originality of the algorithm. Moreover, a topdown approach is more suitable when the software needs to be designed from scratch and very
specific details are unknown.
By breaking a bigger problem into smaller fragments, the top-down approach minimizes the
complications usually incurred while designing algorithms. Furthermore, in this approach, each
function in a code is unique and works independently of other functions. The top-down approach is
heavily used in the C programming language.
o Each module of code is to be tested separately.
o Breaking a problem down into smaller chunks makes it far easier to understand, solve and
o Testing and debugging are efficient and easier.
o Project implementation is smoother and shorter.
o Specification tends to change over time and in a top-down approach, all decisions made from
the beginning of the project depend directly or indirectly on the high-level specification.
o In Dynamic Programming, the top-down approach is slow as compared to the bottom-up
approach, as it involves recursion.
The Bottom-Up Approach
Contrary to the top-down approach, bottom-up programming focuses on designing an algorithm by
beginning at the very basic level and building up as it goes. In this approach, the modules are
designed individually and are then integrated together to form a complete algorithmic
design. Moreover, the bottom-up approach is more suitable when a system needs to be created from
So, in this method, each and every module is built and tested at an individual level (unit testing) prior
to integrating them to build a concrete solution. The unit testing is performed by leveraging specific
o Test conditions are easier to create.
o Observation of test results is easier.
o Contains less redundancy due to the presence of data encapsulation and data-hiding.
o Reusability of the code.
o In the Bottom-Up approach, we solve all sub-problems (even though some of the solutions of
the subproblems aren’t needed to solve), which requires additional calculations.
o In the Bottom-Up approach, sometimes it is difficult to identify the overall functionality of the
system in the initial stages.
History of C Programming Language:
To learn about the history of C language, let’s first start with its root and early developments. The root of all modern languages is ALGOL (Algorithmic Language). ALGOL was the first computer programming language to use a block structure, and it was introduced in 1960. In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming Language). BCPL was derived from ALGOL. In 1970, Ken Thompson created a language using BCPL called B. Both BCPL and B programming languages were typeless. After that, C was developed using BCPL and B by Dennis Ritchie at the Bell lab in 1972. So, in terms of history of C language, it was used in mainly academic environments, but at long last with the release of many C compilers for commercial use and the increasing popularity of UNIX, it began to gain extensive support among professionals.The language and compiler were both powerful enough to rewrite the UNIX kernel in C for the PDP-11. Brian Kernighan and Dennis Ritchie published The C Programming Language in 1978, which served as a reference for the language until a formal standard was established in the history of C language. Between 1973 and 1980, the language evolved slightly: unsigned, long, union, and enumeration types were added to the type structure, and structures became practically first-class objects (lacking only a notation for literals). Its environment, as well as the technology that accompanied it, saw significant changes. In the summer of 1983, American National Standard Institute (ANSI) formed the X3J11 committee under the guidance of CBEMA with the purpose of establishing a C standard. At the end of 1989, X3J11 published its report [ANSI 89], which was later recognised by ISO as ISO/IEC 9899-1990.
Algorithm in C language
An algorithm is a sequence of instructions that are carried out in a predetermined sequence in order to solve a problem or complete a work. A function is a block of code that can be called and executed from other parts of the program.
A set of instructions for resolving an issue or carrying out a certain activity. In computer science, algorithms are used for a wide range of operations, from fundamental math to intricate data processing.
One of the common algorithms used in C is the sorting algorithm. A sorting algorithm arranges a collection of items in a certain order, such as numerically or alphabetically.
There are many sorting algorithms, each with advantages and disadvantages. The most common sorting algorithms in C are quicksort, merge, and sort.
Features of the algorithm
It defines several important features of the algorithm, including:
- Inputs: Algorithms must receive inputs that can be represented as values or data.
- Output: The algorithm should produce some output. It can be a consequence of a problem or a solution designed to solve it.
- Clarity: Algorithms must be precisely defined, using unambiguous instructions that a computer or other system can follow unambiguously.
- Finiteness: The algorithm requires a limited steps. It means that it should be exited after executing a certain number of commands.
- Validity: The algorithm must be valid. In other words, it should be able to produce a solution to the problem that the algorithm is designed to solve in a reasonable amount of time.
How to write an algorithm
1. First define the problem you want the algorithm to solve.
For example, suppose we want to write an algorithm to find the maximum value from a list of numbers.
2. Break the problem down into smaller, manageable steps.
- Initialize the ‘max’ variable to the first value in the list.
- For each subsequent value in the list, compare with “max”.
- If the value is greater than “max”, set “max” to that value.
- Continue doing this until every value in the list has been compared.
- Returns the final “max” value.
Algorithm written in pseudo code:
MAX (list) max = list For i = 1 the length of the list list IF[i] > max max = list[i] End for Maximum return Maximum end
Basic writing of algorithms
Example: – The sum of two integers.
Step 1 – Get started
Step 2 – Declare three integers a, b, c
Step 3 – Define the values of a and b
Step 4 – Add the values of a and b
Step 5 – Save the output of step 4 in c
Step 6 – Print c
Step 7 – Stop
Advantages of the algorithm
Algorithms have many advantages.
- Speed and efficiency: Algorithms can process large amounts of data quickly and accurately, making them useful for tasks that are too time-consuming or error-prone for people to perform.
- Consistency: Algorithms follow a set of predetermined guidelines. It can produce consistent results without being influenced by personal biases and emotions.
- Automation: Algorithms can perform tasks automatically, leaving people free to focus on more complex or creative tasks.
- Increased accuracy: Algorithms can often achieve higher levels of accuracy than humans, especially when dealing with large amounts of data.
- Better Decision Making: Algorithms help us make more informed and objective decisions by analyzing data and identifying patterns and trends that are not easily visible to people.
- Scalability: Algorithms can be easily scaled up or down to meet changing demands and workloads.
Disadvantages of the algorithm
Algorithms are very useful for programming, but algorithms have drawbacks.
- Limited scope: Algorithms can only solve problems within their scope and may not be able to solve complex or abstract problems.
- Bias: Algorithms can perpetuate and reinforce biases in the data used for training, leading to unfair results.
- Insufficient transparency: Many algorithms conceal the process through which they arrive at their conclusions. This could make it tough to think about or check the results.
- Reliance on the fineness of the data: The correctness of the set of rules is heavily dependent on the fineness and applicability of the data utilised in instruction. Inaccurate or inaccurate effects may be the result of faulty data.
- restrained adaptability: Algorithms are designed to follow guidelines and won’t adapt to changing circumstances and conditions.
What do you mean by flowchart?
The Flowchart is the most widely used graphical representation of an algorithm and procedural design workflows. It uses various symbols to show the operations and decisions to be followed in a program. It flows in sequential order.
Types of Flowchart
- Horizontal Flowchart
- Panoramic Flowchart
- Vertical Flowchart
- Architectural Flowchart
Rules or guidelines of Flow
- Only conventional flowchart symbols should be used.
- Proper use of names and variables in the flowchart.
- If the flowchart becomes large and complex, use connector symbols.
- Flowcharts should have start and stop points.
The different flowchart symbols have different conventional meanings.
The various symbols used in Flowchart Designs are given below.
- Terminal Symbol: In the flowchart, it is represented with the help of a circle for denoting the start and stop symbol. The symbol given below is used to represent the terminal symbol.
Input/output Symbol: The input symbol is used to represent the input data, and the output symbol is used to display the output operation. The symbol given below is used for representing the Input/output symbol.
Processing Symbol:It is represented in a flowchart with the help of a rectangle box used to represent the arithmetic and data movement instructions. The symbol given below is used to represent the processing symbol.
Decision Symbol: Diamond symbol is used for represents decision-making statements. The symbol given below is used to represent the decision symbol.
Connector Symbol:The connector symbol is used if flows discontinued at some point and continued again at another place. The following symbol is the representation of the connector symbol.
Flow lines: It represents the exact sequence in which instructions are executed. Arrows are used to represent the flow lines in a flowchart.
Advantages of Flowchart in C:
Following are the various advantages of flowchart:
- Communication: A flowchart is a better way of communicating the logic of a program.
- Synthesis: Flowchart is used as working models in designing new programs and software systems.
- Efficient Coding: Flowcharts act as a guide for a programmer in writing the actual code in a high-level language.
- Proper Debugging: Flowcharts help in the debugging process.
- Effective Analysis: Effective analysis of logical programs can be easily done with the help of a related flowchart.
- Proper Documentation: Flowchart provides better and proper documentation. It consists of various activities such as collecting, organizing, storing, and maintaining all related program records.
- Testing: A flowchart helps in the testing process.
- Efficient program maintenance: The maintenance of the program becomes easy with the help of a flowchart.
Disadvantages of Flowchart in C:
Following are the various disadvantages of flowchart:
- Time-consuming: Designing a flowchart is a very time-consuming process.
- Complex: It isn’t easy to draw a flowchart for large and complex programs.
- There is no standard in the flowchart; there is no standard to determine the quantity of detail.
- Difficult to modify: It is very difficult to modify the existing flowchart.
Design a flowchart for adding two numbers entered by the user.
Design a flowchart for finding the largest among three numbers entered by the user.
Pseudo code in C
Programmers frequently utilize pseudo code, a high-level language that is simpler to read and comprehend than actual programming code, to convey algorithms and program logic. C is a high-level, general-purpose programming language that is frequently used for creating system software, and it is one common implementation of pseudo code.
What is Pseudo Code?
A computer program, algorithm, or process that is intended to be simple to comprehend by humans is described in pseudo code, which is plain English. It is a high-level illustration of the program’s logic and flow that can serve as a guide for developing the real code. Pseudo code can be used to explain both complicated procedures, like sorting algorithms, and straightforward duties, like input validation.
Let’s consider an example of implementing a simple program that calculates the average of three numbers using pseudo code in C:
- Input three numbers
- Calculate the sum of the three numbers
- Divide the sum by 3 to get the average
- Display the average
Pseudo-code in C programming has the following advantages:
- Pseudo code is easier to read and comprehend than actual programming code since it describes program logic in everyday language. When working on intricate algorithms or processes, this can be quite helpful.
- Saving time: By working out the specifics of the program logic before creating the actual code, producing pseudo code allows you to save time in the long run. It can make the coding process simpler and help you spot any problems.
- Reduces errors: Because pseudo code enables you to develop the logic of your program in a more streamlined manner, it can help to decrease errors in your code. Before writing the real code, you can use this to find probable problems, which can save time and lower the chance of introducing issues.
- Enhances teamwork: Pseudo coding can be utilized as a tool for team design cooperation. Regardless of programming experience, it offers a universal language that anyone can understand.
- Pseudo code is portable because it is language-independent and is simple to adapt to new programming languages. Therefore, it can be used as a tool for creating cross-platform
What is a compilation?
The compilation is a process of converting the source code into object code. It is done with the help of the compiler. The compiler checks the source code for the syntactical or structural errors, and if the source code is error-free, then it generates the object code.
The c compilation process converts the source code taken as input into the object code or machine code. The compilation process can be divided into four steps, i.e., Pre-processing, Compiling, Assembling, and Linking.
The preprocessor takes the source code as an input, and it removes all the comments from the source code. The preprocessor takes the preprocessor directive and interprets it. For example, if <stdio.h>, the directive is available in the program, then the preprocessor interprets the directive and replace this directive with the content of the ‘stdio.h’ file.
The source code is the code which is written in a text editor and the source code file is given an extension “.c”. This source code is first passed to the preprocessor, and then the preprocessor expands this code. After expanding the code, the expanded code is passed to the compiler.
The code which is expanded by the preprocessor is passed to the compiler. The compiler converts this code into assembly code. Or we can say that the C compiler converts the pre-processed code into assembly code.
The assembly code is converted into object code by using an assembler. The name of the object file generated by the assembler is the same as the source file. The extension of the object file in DOS is ‘.obj,’ and in UNIX, the extension is ‘o’. If the name of the source file is ‘hello.c’, then the name of the object file would be ‘hello.obj’.
Mainly, all the programs written in C use library functions. These library functions are pre-compiled, and the object code of these library files is stored with ‘.lib’ (or ‘.a’) extension. The main working of the linker is to combine the object code of library files with the object code of our program. Sometimes the situation arises when our program refers to the functions defined in other files; then linker plays a very important role in this. It links the object code of these files to our program. Therefore, we conclude that the job of the linker is to link the object code of our program with the object code of the library files and other files. The output of the linker is the executable file. The name of the executable file is the same as the source file but differs only in their extensions. In DOS, the extension of the executable file is ‘.exe’, and in UNIX, the executable file can be named as ‘a.out’. For example, if we are using printf() function in a program, then the linker adds its associated code in an output file.
The Steps for Compiling and executing C Program
◼ Step1- Use a text editor to write, edit or correct your source code into C source code files having the “.c” file extension. A text editor is usually used to enter the C program into a file.
◼ Step2- After the program has been translated the next step in the compilation process is to translate the program using C compiler. If the compiler doesn’t find any syntax errors in the program source code, it produces an object file. The compiler produces object files with an “.obj” file extension and the same name as the source code. If compiler finds any errors in your program, it reports them and you need to return to Step1 to make corrections in your source code.
Step3- After the program has been translated into object code, it is ready to link the program using the linker. If no errors occur, the linker produces an executable program located in a disk file with an “.exe” file extension with the same name as the object file.
◼ Step4- Run the executable program and test whether it works properly as per the user requirements. If the program does not produce the desired results, it is necessary to go back, start again with Step1 and reanalyze the program’s logic and make modifications and additions to your source code.