Mod 1 C
Mod 1 C
Mod 1 C
1.2 INTRODUCTION
lar program or set of instructions can be design to solve it. The method
of writing the instructions is called the programming. The person who
writes such instruction is called a programmer. An another way of
defining a program is : a computer program is nothing but a combi-
nation of algorithm and data structure combined into a single unit
which is designed to solve a given problem.
rithms into actual code it should be checked for accuracy. The main
purpose of checking the algorithm is to identify the major logical er-
rors that may occur at any steps in the algorithm. Logical errors are
often difficult to detect and correct at later stage so it will be helpful if
the logical errors can be eliminated in this step.
gramming language.
The computer understand only the binary language i.e. the languages
of 0 and 1, which is also called machine language. In the initial years
of computer programming the computer programs were written us-
ing machine language which were too difficult to remember all the
instructions in the form of 0 and 1s. But with due course of time the
other languages were evolved along with the generation of comput-
ers. There are different levels of programming language as follows :
• Machine language
• Assembly language
• High level language
• 4GL language
The different levels of programming language is shown in the figure
below :
Advantage :
Disadvantage :
Disadvantage :
Advantages :
• Readability : Since high level languages are similar to English
language so they are easy to learn and understand.
• Programs written in high level languages are easy to modify
and maintain.
• High level language programs are machine independent.
• Programs written in high level language are easy to error
checking. Due to the concept of abstraction used in such type of
language the programmers don’t have to mind on the hardware level
representation of programs.
Disadvantages :
Programs written in high level language has to be compile first for
Another main draw back of HLL programs is - its poor control on the
hardwares.
There are two types of loaders depending on the way the loading is
performed : absolute loader and relocating loaders.
The absolute loader load the executable code into memory locations
specified in the object module. On the other hand, relocating loader
loads the object code into memory locations which are decided at
load time. The relocating loader can load the object code at any loca-
tion in memory.
Pseudo code derived from ‘pseudo’ which means imitation and ‘code’
means instruction (pronounced as soo-doh-kohd) is a generic way
of describing an algorithm without using any specific programming
language related notations. Pseudocode is an artificial and informal
language that helps programmers to develop algorithms. It is a “text-
based” detail (algorithmic) design tool. Pseudo code generally con-
sists of short English phrases to express a specific task. But, inter-
estingly, there are no specific rules to write pseudo code. Then why
do we need pseudo code? Implementing a problem with proper sym-
bol and syntax is a complicated job. To solve the complicated job,
first we need the complete explanation of the job, and then we extract
information from the explanation, and roughly design the solution,
which will be relevant to a programming construct. This rough work
will help in designing the proper solution with proper symbol and syn-
tax.
The general guidelines for developing pseudocodes are :
Advantage :
Its language independent nature helps the programmer to express
the design in plain natural language.
Based on the logic of a problem it can be designed without concern-
ing the syntax or any rule.
Limitations :
It is unable to provide the visual presentation of the program logic.
It has not any standard format or syntax of writing.
It cannot be compiled or executed.
1.5.2 Algorithm
Solution: First of all you need two integers. There are two ways to get
the two integers. First, you can supply the two integers from your
side; secondly, you can ask the user to supply the integers. After
getting the integers you store it into two containers. You need another
container to store the sum of the two integers. You must initialize the
third container as zero before you store the summation. Now, per-
form the addition task and store the result into the third container.
Now, display the content of the third container. This is the solution to
your problem.
Step 1 : Read a, b, c
[a, b, c are integers]
Step 2 : c 0
Step 3 : c a+b
Step 4 : Print c
Step 5 : Stop
Always remember :
For example :
If condition Then : If condition Then
Step 1 Step i
Step 2
Else
....
Step n Step j
[ End of If ] [ End of If ]
vii) The iterative or repetitive steps can be write between Repeat For
and [ End of For ] as shown below :
For example,
Fact 1
Repeat For I=1,2,3,...N
Fact Fact*I
[ End of For I ]
Print Fact
Step 1 : Read a, b, c
Step 2 : big a
Step 3 : If b>big Then
big b
Step 4 : If c>big Then
big c
Step 5 : Print big
Step 6 : Stop
Start/Stop Decision
Input/Output Connector
a) The flowchart should contain one Start and one Stop termina-
tor.
Start
As k us er to s upply
two integers
C=A+B
Display C
Stop
The following flowchart is for the program for finding the greatest num-
ber from given three numbers.
• Top-down approach
• Bottom-up approach
• Unstructured technique
• Structured technique
• Modular programming
Modular Programming :
The best way to get started with is to actually look at the program. So,
let us first create a file “first.c”. To create first.c file, we have to follow
the following steps:–:
Step 1: Open Turbo C++ Editor from the icon on your desktop. If it is
not in the desktop then go to Start and click on Run command from
the Pop up Menu. The following command window will appear.
In the open field type cmd and press enter key. The command prompt
will appear as an active window.
Now type cd\ . Now file pointer is in root Directory that is in C:\
Now type the path for Turbo C++ Editor that is cd c:\tc\bin and press
enter. The following window will appear.
Now Type TC and press Enter. The Turbo C++ Editor will open. Go
to File Menu and
Step 2: After clicking the New submenu the following window will
appear.
Step 4: Type the file name that is c:\tc\bin\first.c and press enter.
The following window will appear.
At the end of this step, you successfully create a C source file name
first.c and save the blank file. Now type the following code in your
first.c program.
#include<stdio.h>
int main()
{
printf(“This is output from my first program!\n”);
return 0;
}
The line int main() declares the main function. Every C pro-
gram must have a function named main somewhere in the
code. We will learn more about functions shortly. At run time,
program execution starts at the first line of the main function.
Now to compile this source code press Alt+F9 key or click compiler
submenu of compiler menu. If there is error in the source program
then the compiler will point out the error with line number. On suc-
cessful compilation, the compiler will give a message that the source
program is error free. Press Ctrl+F9 (or click submenu run of run
menu) to execute the compiled source code. This will give the re-
quired output. Now to see the output generated, press Alt+F5 key.
1.9.1 Identifiers
The following names are not valid identifiers for the reasons stated:
x” illegal characters ( “ ).
order-no illegal character ( - )
total sum illegal character ( blank space)
1.9.3 Constants
EXAMPLE TYPES
153 Decimal integer constant
015 Octal integer constant
0xA1 Hexadecimal integer constant
153.371 Floating point constant
‘a’ Character constant
‘1’ Character constant
“a” String constant
“153” String Constant
C does not use, nor does it require the use of, every character found
on a modern computer keyboard. The only characters required by
the C Programming Language are as follows :
A-Z
Alphabets a-z
Digits 0-9
C Data Type
Double Reference
The first category of data type is the built-in data type, which are
also known as elementary or basic type. Sometime these are called
the “primitive” type. These basic data type have several type modifi-
ers, which alter the meaning of the base data type to yield a new type.
Table 1.1 lists all combinations of the basic data types and modifiers
along with their size and ranges:
Table 1.1: Size and range of basic data type and its modifier
Moreover, besides these basic data types another special data type
is void. The void type specifies the return type of a function, when it
is not returning any value. Sometime void is used to indicate an empty
parameter to a function. After all, this data type holds the literal mean-
ing of void. At this point don’t worry about the other two categories. All
these will be discussed in the subsequent unit.
printed or scanned and how that data type is converted to the char-
acter that appears on the screen. Format specifiers for usual vari-
able types are shown in Table.1.2.
153
A int A int A
ing statement :
int A ;
A = 153 ;
scanf(“%d”,&A);
This statement will take an integer type input from standard input
device (that is keyword) and store it to A.
int A = 153;
int j;
int k;
It is always a good practice to group together declarations of the same
data type for an easy reference. For example :
int j, k;
float x,y,z;
Variable Remarks
declaration
If, however, you define a symbolic constant with the name PI and
assign it the value 3.14, you would write the name PI and the value
3.14 as shown below
#define PI 3.14
circum = PI * (2*radius);
area = PI * (radius) * (radius);
9. What is an identifiers ?
10. What is a keyword in C ?
11. What are the types of constants ?
12. What is the built in data types in C ?
13. What is a variable ?
The loader brings a program residing on disk into the main memory
of computer and run it.
Identifiers are the name given to the various program elements - vari-
ables, functions, arrays etc.
1. a) T, b) F, c) F, d) T, e) T
2. a) F, b) T, c) T, d) F, e) F
3. A compiler is a computer program (or set of programs) that trans-
lates text written in a computer language (the source language) into
another computer language (the target language).
A program that is written in a high level language must be trans-
lated into machine language before it can be executed. This is known
as compilation process.
5.
Start
COUNT = 0
PERCENTAGE=
(TOTAL /10)*100
COUNT = COUNT + 1
No
Is
COUNT <=
Yes
Stop
f1 f2 f3 f4 f5 f6 f7 .........................
1 2 3 5 8 13 21 ........................
To get next fibonacci number, we have to do sum of previous two
numbers in the series.
Algorithm :
1. Assign sum =0, A=0, B=1, i=1
2. Get the number of terms upto which you want to generate the
10. Keywords are also called the reserved words in C. They have
specific meaning to compiler. These words should not be used for
naming any other variables.
12. The built in data types are integer, character, float, double.