UNIT-5 Programming Concept and Logic
UNIT-5 Programming Concept and Logic
Before developing the C language, there was developed the program named
BCPL by Martin Richard in 1966. The full form of BCPL is Basic Combined
Programming Language. He had developed the BCPL program by combining
by various programming languages. Then after, Ken Thompson developed
next program named B. The B language was developed in 1969 by improving
the BCPL language. C is a general purpose programming language. C language
C language has been designed and developed by Dennis Ritchie at
AT & T’s Bell Lab in 1972. The language is associated with UNIX
operating system. The source code for the UNIX operating system is
coded in C.
Features of C
(1) It is highly portable language.
(2) It is structured programming language because the program is
divided into number of functions.
(3)C is faster and efficient high level programming language.
(4) It is extendable program.
(5) C is a flexible language. It permits to write any complex
programs.
(6) It has small size.
Advantages of C language
It is easy for debugging (correction of error) testing and
maintaining.
It is portable programming language. This means a program
written for one computer may run successfully on other
computers.
Adding new features of easier and faster.
It is easy to learn.
It is easy to interact with hardware.
It has both features of high level languages.
Disadvantages of C language
The program takes more time to design and implement the
software.
There is no run time checking.
There is no strict type checking. (For example: we can pass an
integer value for the floating data type).
It doesn’t support OOP, so to overcome C++ language was
introduced.
Structure of C program
There are three main things to be considered to solve the program. The
input, processes and output. Every programming language follows the set
of rules. A simple C program and its structure is as follows.
(1) #include<stdio.h>
(2) #include<conio.h>
(3) void main()
(4) {
(5) printf(“Welcome in C programming Language”);
(6) printf(“Thank you!”);
(7) getch();
(8) }
On the above structure,
1,2,3,4 In most of simple C program, it is fixed.
5,6 It is used to display text enclosed within “………….”.
7 It is used to hold output screen until you pressed any key from
keyboard.
8 It is the termination of C program.
Compiling Process
Compiler is a program that
transforms all the source code
written in C language into object
code for the execution of that
program. The compiler of the
program begins with saving the
source code with extension .c. This
source code will not understand by
the computer. The next step is to
convert the source code into
machine language (object code).
This task is performed by a
software or program as a compiler.
Every language has its own
compiler that converts the source
code into object code. The
compiler will compile the program
successfully if the source code is
correct, else the object code will
not be produced.
Preprocessor:
Preprocessor is a program that processes the code before it passes
through the compiler. It is often known as a macro processor. It
operates under the control of preprocessor command lines and
directives. Preprocessor directives are placed in the source program
before the main line, before the source code passes through the
compiler. It is examined by the preprocessor for any preprocessor
directives. Preprocessor directives follow the special syntax rule and
begin with the symbol # and do not require any semicolon at the end.
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.
Assembler:
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.
Linker:
In computing, a linker or link editor is a computer
system program that takes one or more object files (generated by
a compiler or an assembler) and combines them into a single
executable file, library file, or another "object" file.
Header Files:
Header files are standard file of C programming language. It
contains the function definition of library function.
#include<stdio.h>:
It is standard input output header file. It contains the function
definition of input output function such as scanf(), printf() etc.
#include<conio.h>:
It is a header file which is included in the program that is used
to clrscr(), getch() etc. The conio stands for Console Input
Output.
#include<math.h>:
This header file is used for mathematical functions. Such as
pow(), sqrt(), sin(), cos(), (tan) etc.
#include<string.h>:
It is a header file. It contains the function definition of string
processing functions such as strlen(), strcpy() etc.
Fundamental of C
Character Set:
A group of alphabetic, numeric and other characters that have
some relationship with C programming language and recognized
by compiler is called character set. The character set consists the
following:
Alphabets: A to Z and a to z.
Numbers: 0 to 9.
Special Characters:
;, :, {, }, “, /, \, !, @, $, % etc.
Use of Comments:
A comment is an explanation or description of the source code of
the program. It helps a developer to explain logic of the code and
improves program readability. At run-time, a comment is ignored
by the compiler. There are two types of comment in C language.
They are: Line comment and Block comment.
(i) Line Comment:
If we want place only one line of comment somewhere in the program, we
can write comment after double forward slashes (//). This is just one line
comment hence it is called line comment. It doesn’t have ending slash. For
example:
//Makawanpur Multiple Campus
Identifiers:
An identifier is nothing but a name assigned to an element in a program.
Example, name of a variable, function, etc. Identifiers are the user-defined
names consisting of 'C' standard character set. As the name says,
identifiers are used to identify a particular element in a program. Each
identifier must have a unique name. Following rules must be followed for
identifiers:
(i) The first character must be an alphabet or underscore than digits or
alphabets.
(ii) It must consists of letters, digits and underscore
(iii) A keyword cannot be used as an identifier.
(iv) It should not contain any whitespace character.
Keyword:
There are certain words which are reserved by the C compiler. They
have standard predefined meaning in C. These words are known as
keyboard. They can’t be used as identifiers in the program. Keywords
have fixed meanings, and the meaning cannot be changed. They act as
a building block of a 'C' program. There are a total of 32 keywords in
'C'. Keywords are written in lowercase letters.
auto const double float int short struct unsigned
Syntax
Start
Statement 1
Statement 2
Statement 3
……………………………..
……………………………..
……………………………..
……………………………..
……………………………..
Statement N
Stop
(ii) Decision Control Statement/Selective Control Structure:
The decision control statements are the decision making statements that
decides the order of execution of statements based on the conditions. In the
decision making statements, the programmer specify which conditions are to
be executed or tested with the statement to be executed if the condition is
true or false. In selective control structure, selection is made on the basis of
condition. We have to options to go when the given condition is true or false.
(1) If Statement:
It is the simplest form of condition statement in which statements are
executed if the test expression (condition) is true. When the condition is false
there is no option to go within this structure. In such situation control must
be out from the structure and statements outside this structure will be
executed.
(2) If else statement:
This is the another form of selective control structure. If condition is true then
if() portion statements are evaluated. Otherwise else part of the statement are
evaluated. This is appropriate where we have to check only one condition. The
benefit of the if() else is only one of the block of statements are evaluated. That
may be if() statement or else statement.
Syntax:
if(condition)
statement 1;
else
statement 2;
Syntax:
if(condition 1)
statement 1;
else if( condition 2)
Statement 2;
………………………………
……………………………….
……………………………….
………………………………
else if (condition N1);
statement N1;
else
statement N:
(4) Nested if else statement:
This type of control statement is used to check condition within condition. An
entire if else statement written within the body of if part or else part of
another if else is called nested if else statement. It is used when a condition is
to be checked inside another condition at a time in the same program to make
decision.
Syntax:
If(condition 1)
{
if (condition 2)
statement 1;
else
Statement 2;
}
else
Statement 3;
Switch Case Statement:
The objective of switch case statement is to check several possible constant value for an expression. The
switch case statement of C programming language allows selection and execution of particular block of
statement from several blocks of statement based upon the value of expression which is included within
switch statement and branches accordingly. The expression must be an integral values. The same task can be
performed using if-else ladder as well but as the number of alternatives increases, selection process becomes
more complex. So, the switch case statement body consists of a series of case labels and an optional default
label. The default label can appear only once and anywhere in the body of the switch statement.
Syntax
Switch(expression)
{
Case 1;
Statement 1;
Break;
Case 2;
Statement 2;
Break;
Case 3;
Statement 3;
…..
…..
…..
Case N;
Statement N;
Break;
Default:
{
Statement ;
}
-When the switch statement is executed, the value of expression is compared with
the value of case (constant1, constant 2)
-Then the block of statement associated with the case whose value is matched with
expression will be executed.
-Break statement at the end of each block indicates the end of the particular case
and cases an exit from switch statement and control will be transferred to
statement following the switch.
-If none of the case match with the value of expression, then the block of
statement under default is executed.
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
printf(\nMakawanpur Multiple Campus”); int i;
printf(\nMakawanpur Multiple Campus”); for(i=1;i<=10;i++)
printf(\nMakawanpur Multiple Campus”); {
printf(\nMakawanpur Multiple Campus”); printf(\nMakawanpur Multiple Campus”);
printf(\nMakawanpur Multiple Campus”); }
printf(\nMakawanpur Multiple Campus”); getch();
printf(\nMakawanpur Multiple Campus”); }
printf(\nMakawanpur Multiple Campus”);
printf(\nMakawanpur Multiple Campus”);
printf(\nMakawanpur Multiple Campus”);
getch();
}
The above both programs give the same output i.e. after running these programs,
you will see the name of “Makawanpur Multiple Campus” 10 times on the screen.
You can imagine if the number of times for repetition increase from 10 to 20 or 50 or
100 or even more, then which method do you find short and efficient?
for loop:
It is the most common type of loop which is used to execute program statement or
block of program statements repeatedly for specified number of time. Mainly it consists
three expressions. initialization, condition and increment/decrement. The initialization
defines the loop starting point, condition defines the loop stopping points and counter
helps to increment/decrement the value of counter variable.
Syntax:
for(initialization;condition;increment/decrement)
{
statements to be executed repeatedly;
}
(ii) while loop:
The while loop executes the program repeatedly until the given condition is
true. It checks condition at first; if it is found true then it executes the
statements written in its body part otherwise it just gets out the loop
structure.
Syntax:
Initialization;
while(condition)
{
Statement;
Increment/decrement;
}
Syntax:
Initialization
do
{
Statements
……………………
…………………..
increment/decrement
}while(termination condition);
Array and String
Array:
An array is a collection of similar type of data items treated as single unit. It
acts to store related data under the same name with an index, also known as
a subscript, which helps to access individual array element. Array data type
may be int, float, char etc. depending upon the nature of problems. In other
word, array is the collection of similar elements. These similar elements
could be all integers, or all floats or all characters etc. All elements of any
given array must be of the same type i.e. we can’t have an array of 10
numbers, of which 5 are integers and 5 are floats.
Characteristics of Array
All the array elements share the common name.
The elements of array stored in contiguous memory locations
By declaring or using array, person becomes short and simple which
handles large volume of similar type of data items.
We put the array size of fixed length as required that means once the size
is declared then the size bill be fixed at the execution time of the
program.
Advantages of Array
It is easier for handling similar types of data in a program.
It is efficient for solving problem like sorting, searching,
indexing etc.
It saves memory
It is very close to matrix, therefore it is easy for solving matrix
related problem.
Disadvantages of Array
It is not possible to hold dissimilar type of data in an array.
It is difficult to visualize the multidimensional array.
It is static in nature so it is difficult to define the size of array
during running time.
Write a program to input the salary of 10 employees and print them without
using array.
#include<stdio.h>
#include<conio.h>
void main()
{
float s1,s2,s3,s4,s5,s6,s7,s8,s9,s10;
clrscr();
printf(“\nEnter the salaries of 10 employees: “);
scanf(“%f%f%f%f%f%f%f%f%f%f”,&s1,&s2,&s3,&s4,&s5,&s6,&s7,&s8,&s9,&s10);
printf(“\nThe salaries are:”);
printf*(“\nRs.%f”,s1);
printf*(“\nRs.%f”,s2);
printf*(“\nRs.%f”,s3);
printf*(“\nRs.%f”,s4);
printf*(“\nRs.%f”,s5);
printf*(“\nRs.%f”,s6);
printf*(“\nRs.%f”,s7);
printf*(“\nRs.%f”,s8);
printf*(“\nRs.%f”,s9);
printf*(“\nRs.%f”,s10);
getch();
Write a program to input the salary of 10 employees and print them without using
array.
#include<stdio.h>
#include<conio.h>
void main()
{
float s[10];
int i;
printf(“\nEnter the salaries of 10 employees: “);
for(i=0;i<10;i++)
{
scanf(“%f”,&s[i]);
}
Printf(“\nThe salaries are as follows: “);
for(i=0;i<10;i++)
{
printf(“\nRs.%f,s[i]);
}
getch();
}
Types of Array:
(1) One Dimensional Array:
An array which has only one subscript is named as one dimensional array, a
subscript is a number of large bracket in which we put the size of the array.