206--C-Programing by Prof.Mrs. R.M.Shaikh/Prof.A.S.Ghodke
Experiment no. 1
Objective
To study of Turbo C IDE and Programming Environment
Theory
The Development Environment -
Integrated Development Environment (IDE):
The Turbo C compiler has its own built-in text editor. The
files you create with text editor are called source files, and for C++ they
typically are named with the extension .CPP, .CP, or .C. The C Developing
Environment, also called as Programmer’s Platform, is a screen display with
windows and pull-down menus. The program listing, error messages and other
information are displayed in separate windows. The menus may be used to invoke
all the operations necessary to develop the program, including editing,
compiling, linking, and debugging and program execution.
Invoking the IDE:
To invoke the IDE from the windows you need to double click
the TC icon in the directory c:\tc\bin. The alternate approach is that we can
make a shortcut of tc.exe on the desktop. This makes you enter the IDE
interface, which initially displays only a menu bar at the top of the screen
and a status line below will appear. The menu bar displays the menu names and
the status line tells what various function keys will do.
Default Directory:
The default directory of Turbo C compiler is c:\tc\bin.
Using Menus:
If the menu bar is inactive, it may be invoked by pressing
the [F10] function key. To select different menu, move the highlight left or
right with cursor (arrow) keys. You can also revoke the selection by pressing
the key combination for the specific menu.
Opening New Window:
To type a program, you need to open an Edit Window. For this,
open file menu and click “new”. A window will appear on the screen where the
program may be typed.
Writing a Program:
When the Edit window is active, the program may be typed. Use
the certain key combinations to perform specific edit functions.
Saving a Program:
To save the program, select save command from the file menu.
This function can also be performed by pressing the [F2] button. A dialog box
will appear asking for the path and name of the file. Provide an appropriate
and unique file name. You can save the program after compiling too but saving
it before compilation is more appropriate.
Making an Executable File:
The source file is required to be turned into an executable
file. This is called “Making” of the .exe file. The steps required to create an
executable file are:
1. Create a source file, with a .c extension.
2. Compile the source code into a file with the .obj extension.
3. Link your .obj file with any needed libraries to produce an
executable program.
All the above steps can be done by using Run option from the
menu bar or using key combination Ctrl+F9 (By this linking & compiling is
done in one step).
Compiling the Source Code:
Although the source
code in your file is somewhat cryptic, and anyone who doesn't know C will struggle
to understand what it is for, it is still in what we call human-readable form.
But, for the computer to understand this source code, it must be converted into
machine-readable form. This is done by using a compiler. Hence, compiling is
the process in which source code is translated into machine understandable
language. It can be done by selecting Compile option from menu bar or using key
combination Alt+F9.
Creating an Executable File with the Linker:
After your source code is compiled, an object file is
produced. This file is often named with the extension .OBJ. This is still not
an executable program, however. To turn this into an executable program, you
must run your linker. C programs are typically created by linking together one
or more OBJ files with one or more libraries. A library is a collection of
linkable files that were supplied with your compiler.
Compiling and linking in the IDE:
In the Turbo C IDE, compiling and linking can be performed
together in one step. There are two ways to do this: you can select Make EXE
from the compile menu, or you can press the [F9] key.
Executing a Program:
If the program is compiled and linked without errors, the
program is executed by selecting Run from the Run Menu or by pressing the
[Ctrl+F9] key combination.
The Development Cycle:
If every program
worked the first time you tried it that would be the complete development
cycle: Write the program, compile the source code, link the program, and run
it. Unfortunately, almost every program, no matter how trivial, can and will
have errors, or bugs, in the program. Some bugs will cause the compile to fail,
some will cause the link to fail, and some will only show up when you run the
program. Whatever type of bug you find, you must fix it, and that involves editing
your source code, recompiling and relinking, and then rerunning the program.
Correcting Errors:
If the compiler recognizes some error, it will let you know
through the Compiler window. You’ll see that the number of errors is not listed
as 0, and the word “Error” appears instead of the word “Success” at the bottom
of the window. The errors are to be removed by returning to the edit window.
Usually these errors are a result of a typing mistake. The compiler will not
only tell you what you did wrong; they’ll point you to the exact place in your
code where you made the mistake.
Exiting IDE:
An Edit window may be closed in a number of different ways.
You can click on the small square in the upper left corner, you can select
close from the window menu, or you can press the [Alt] [F3] combination. To
exit from the IDE, select[Alt][X] Combination.
Fundamentals of C
Importance of C language:
C language is one of the most popular computer languages
today because it is a structured, high level, machine independent language. It
allows software developers to develop programs without worrying about the
hardware platforms where they will be implemented. C allows meaningful variable
names and meaningful function names to be used in programs without any loss of
efficiency and it gives a complete freedom of style, it has a set of very
flexible loop constructions and neat ways of making decisions.
The C language
has been equipped with features that allow programs to be organized in an easy
and logical way. This is vitally important for writing lengthy programs because
complex problems are only manageable with a clear organization and program
structure. C tries to make the best of a computer by linking as closely as
possible to the local environment. The increasing popularity of C is probably
due to its many desirable qualities. It is a robust language whose rich set of
built-in functions and operators can be used to write any complex program.
Simple program
to printing message:
In C writing a
program to print message “Hello world” is
#include <stdio.h>
#include <conio.h>
void main( )
{
clrscr( );
printf(“Hello world \n”);
getch( );
}
Then to run this
program first save this program in .c and then compile it, when there is no
error in program then it is ready to run. If you will run this program then it
will print
Hello world
This message
will appear on output window.
Basic structure
of a c program:
A C program
should be written as follow:
Include header file section
Global
declaration section
Main ()
Function section
{
Declaration
part
Executable
part
}
User-defined
functions
{
Statements
}
In Basic structure of a c program
some section are essential and some are not essential. The include header file
section and main () function section is very important and essential section in
every C program, and other section are added if they required.
Include Header File Section:
C program
depends upon some header files for function definition that are used in
program. Each header file by default is extended with .h. The header file
should be included using # include directive as given here.
Global Declaration Section:
This section
declares some variables that are used in more than one function. These
variables are known as global variables. This section must be declared outside
of all the functions.
Function Main section:
Every program
written in C language must contain main () function. The function main () is a
starting point of every C program. The execution of the program always begins
with the function main ().
Declaration Part:
The declaration part declares the
entire variables that are used in executable part. The initializations of
variables are also done in this section. Initialization means providing initial
value to the variables.
Executable Part:
This part contains the statements
following the declaration of the variables. This part contains a set of
statements or a single statement. These statements are enclosed between the
braces.
User Defined Function:
The functions defined by the user
are called user-defined functions. These functions are generally defined after
the main () function.
Example:
#include<stdio.h>Header file
#include<conio.h> Header file
Void main()
Main function
{
Opening curly bracket
int a,b,c;
variable declaration
clrscr();
printf(“ Enter two number”);
Executable statement
scanf(“%d%d”,&a,&b);
Executable statement
c= a+b; Executable
statement
printf(“the addition is = %d”,c);
Executable statement
getch();
}
Token:
The C language code (source
program) is formed form the combination. The world format form the character
set is building blocks of C are some time known as token. The C language code
is formed of combination of alphabets, numeric and special symbols. Tokens
represent individual entity of language. Tokens used in C language are
identifier, keyword, constant, operators and punctuation symbols.
Identifier:
A ‘C’
program consists of two types of elements user defined and system defined.
Identifier is nothing but name given to these elements. An identifier is world
used by a programmer to name a variable, function.
Identifiers are case sensitive e.g.
radius and Radius are two different identifiers. Identifier s are categorized
in two type
1.
Keyword 2.
Variable
1.
Keyword:
Keywords are the words whose meaning has already been
explained to the C compiler. The keywords cannot be used as variable names
because if we do so we are trying to assign a new meaning to the keyword, which
is not allowed by the computer. Some C compilers allow you to construct
variable names that exactly resemble the keywords. However, it would be safer
not to mix up the variable names and the keywords. The keywords are also called
‘Reserved words’.
There are only
32 keywords available in C:
2. Variable:
It
is a data name used for storing a data value. Its value may be changed during
the program execution. The value of variables keeps on changing during the
execution of a program.
Data types:
A data types
defines a set of values that a variable can store along with a set of operation
that can be performed on the variable. Different dada types used in C language
are:
Data
type
|
Size
(Bytes)
|
Range
|
Format
Specifiers
|
Int
|
2
|
– 32,768 to 32, 767
|
%d
|
unsigned int
|
2
|
0 to 65535
|
%u
|
Float
|
4
|
3.4e – 38 to +3.4e +38
|
%f
|
Long int
|
4
|
2147483648 to 2147483647
|
%ld
|
unsigned long int
|
4
|
0 to 4294967295
|
%lu
|
Double
|
8
|
1.7e – 308 to 1.7e+308
|
%lf
|
long double
|
10
|
3.4e – 4932 to 1.1e+4932
|
%lf
|
Char
|
1
|
– 128 to 127
|
%c
|
unsigned char
|
1
|
0 to 255
|
%c
|
Constants:
There are
four basic type of constant in C:
A.
integer constant
B.
floating point constant
C.
character constant
D.
string constant
Conclusion
Experiment no. 2
Objective
To study the operators, expressions, data input and output
Theory
Operators:
C programming language provides several operators to perform different kind
to operations. An operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations. C language is rich in built-in
operators and provides the following types of operators:
1. Arithmetic
Operators
2. Relational
Operators
3. Logical
Operators
4. Bitwise
Operators
1.
Arithmetic Operators:
Following
table shows all the arithmetic operators supported by C language. Assume
variable A holds 10 and variable B holds 20 then:
Operator
|
Description
|
Example
|
+
|
Adds two operands
|
A + B will give 30
|
-
|
Subtracts second operand from
the first
|
A - B will give -10
|
*
|
Multiplies both operands
|
A * B will give 200
|
/
|
Divides numerator by
de-numerator
|
B / A will give 2
|
%
|
Modulus Operator and remainder
of after an integer division
|
B % A will give 0
|
++
|
Increments operator increases
integer value by one
|
A++ will give 11
|
--
|
Decrements operator decreases integer
value by one
|
A-- will give 9
|
2.
Relational Operators
Relational operators checks relationship between two
operands. Following table shows all the relational operators supported
by C language. Assume variable A holds 10 and variable B holds 20, then:
Operator
|
Description
|
Example
|
==
|
Checks if the values of two
operands are equal or not, if yes then condition becomes true.
|
(A == B) is not true.
|
!=
|
Checks if the values of two
operands are equal or not, if values are not equal then condition becomes true.
|
(A != B) is true.
|
>
|
Checks if the value of left
operand is greater than the value of right operand, if yes then condition
becomes true.
|
(A > B) is not true.
|
<
|
Checks if the value of left
operand is less than the value of right operand, if yes then condition
becomes true.
|
(A < B) is true.
|
>=
|
Checks if the value of left
operand is greater than or equal to the value of right operand, if yes then
condition becomes true.
|
(A >= B) is not true.
|
<=
|
Checks if the value of left
operand is less than or equal to the value of right operand, if yes then
condition becomes true.
|
(A <= B) is true.
|
3.
Logical Operators:
Logical operators are used to combine expressions
containing relation operators. Following table shows all the logical
operators supported by C language. Assume variable A holds 1 and variable B holds 0, then:
Operator
|
Description
|
Example
|
&&
|
Called Logical AND operator. If
both the operands are non-zero, then condition becomes true.
|
(A && B) is false.
|
||
|
Called Logical OR Operator. If
any of the two operands is non-zero, then condition becomes true.
|
(A || B) is true.
|
!
|
Called Logical NOT Operator. Use
to reverses the logical state of its operand. If a condition is true then
Logical NOT operator will make false.
|
!(A && B) is true.
|
4.
Bitwise Operators:
A bitwise operator works on each bit of data. Bitwise
operators are used in bit level programming. The Bitwise
operators supported by C language are listed in the following table.
Operator
|
Description
|
&
|
Binary AND Operator copies a bit
to the result if it exists in both operands.
|
|
|
Binary OR Operator copies a bit
if it exists in either operand.
|
^
|
Binary XOR Operator copies the
bit if it is set in one operand but not both.
|
~
|
Binary Ones Complement Operator
is unary and has the effect of 'flipping' bits.
|
<<
|
Binary Left Shift Operator. The
left operands value is moved left by the number of bits specified by the
right operand.
|
>>
|
Binary Right Shift Operator. The
left operands value is moved right by the number of bits specified by the right
operand.
|
Input and output
Reading data
from input devices and displaying the results on the screen are the two main
tasks of any program.
Formatted Functions:
The formatted
input/output functions read and write all types of values
Input
Output
scanf( )
printf( )
Unformatted Functions
The unformatted input/output functions only work with the
character data type
Input
Output
getch()
putch()
getche()
putchar()
getchar()
put()
Example
Develop a program to calculate
power.(P=V*I)
Algorithm
Flowchart
Program
Conclusion
Experiment no. 3
Objective
To study decision making statement
and develop the program on it.
Theory
C language supports control
structures, and implements them using various control statements such as:
- Simple if statement
- if...else statement
- Nested if-else statement
4.
The else if ladder
- Simple if statement: The general form of the
Simple if statement is:
if (condition)
{
Statement;
}
Statement;
Where, a
statement may consist of a single statement, a block of statements, or nothing
(in the case of empty statement). It is conditional statement. C allows
execution of the statement or a set of statements based on a given condition.
If expression evaluates to true then the statement or block that forms the
target of if is executed.
2.
if...else
statement: The general form of the if…else statement is:
if
(condition)
{
Statement;
}
else
{
Statement
1;
}
It is also a conditional statement. C allows execution of the
statement or a set of statements based on a given condition. The else clause is optional. If expression
evaluates to true then the statements or block that forms the target ofif is executed; otherwise the
statements or block that is the target of else
will be executed, if it exists. Remember, only the code associated with if or the code associated with else executes, never both.
3.
Nested if-else statement
It
is perfectly all right if we write an entire if-else construct within either
the body of the if statement or the body of an else statement. This is called
‘nesting’ of ifs.The general form of the Nested if-else statement is:
if ( condition 1 )
{
if ( condition 2)
{
Statement 1 ;
. }
else
{
Statement 2 ;
}
}
else
{
Statement 3;
}
The else if ladder:
If there is multiple decisions
making than else if ladder is useful. The general form of the else if ladder
statement is:
if (condition 1)
statement 1 ;
else if (condition 2)
statement 2 ;
else if (condition 2)
statement 2 ;
:
:
else
default statement;
Example
1.
Develop a program to find out maximum number out of given two
numbers.
2.
Develop a program to find greater number from given three
numbers.
Algorithm
Flowchart
Program
Conclusion
Experiment no. 4
Objective
To study loop control statement and develop the program by
using loop control statement
Theory
Loop: The versatility of the computer lies in its ability to perform a set of
instructions repeatedly. This involves repeating some portion of the program
either a specified number of times or until a particular condition is being
satisfied. This repetitive operation is done through program.
They are:
(a) Using a for statement
(b) Using a while statement
(c) Using a do-while statement
For Loop:
Format
for
(initialization, Condition, increment or decrement)
{
Body
of loop;
}
‘for’ allow us to specify
three things about a loop in a single line:
(a) Setting a loop counter to an
initial value.
(b) Testing the loop counter to
determine whether its value has reached the number of repetitions desired.
(c)Increasing the value of loop counter each time the program segment
within the loop has been executed.
While Loop
It is often the case in
programming that you want to do something with fix number of times.
Format:
While
(condition)
{
Body
of loop
}
Flow chart
·
The statements within the while
loop would keep on getting executed till the condition being tested remains
true. When the condition becomes false, the control passes to the first
statement that follows the body of the while loop.
·
The condition being tested may use
relational or logical operators as shown in the following examples:
while ( i <= 10 )
while ( i >= 10 && j
<= 15 )
while ( j > 10 &&( b
< 15 || c < 20 ) )
·
As a rule the while must test a
condition that will eventuallybecome false, otherwise the loop would be
executed forever
Do-While Loop
Format
Do
{
This;
& this;
& this;
} While (condition)
General flowchart for do while
loop
Example
Develop a program for find out
factorial using loop control.
Algorithm
Flowchart
Program
Conclusion
Experiment no. 5
Objective
To study array and
develop the program using array.
Theory
Array-It is group of
element that share common name &they are differentiated from one another by
their position elements could be all integers, all floats, characters etc.
There are two types of array listed below:
1) One
dimensional Array
Format
data-type
array-name[size];
Example
Char name [30];
Int number [10];
Float temperature [12];
Where, [
]-subscript-Array
Initializing
Array - Like ordinary variable, we can initialize array, element at
the time of their declaration.
Format
Storage-class data
type array-name[size]={list of
variables};
Suppose an array is declared & initialized as follows.
Here integer type array named” number” is declared of 5
element. Each elements of an array will contain values as shown in table.
Element
Values
0 10
1 20
2 30
3 40
4 50
2)Two dimensional array
In one dimensional array we can
store list of values. Not data of table. So that in two dimensional array we can store values of table.
Example
JAN FEB MARCH
L.G 100 120 90
VIDEOCON 80 90 110
PHILIPS 110 125 115
Products sold by one TV showroom of
different company in 3 months this can be represented in two dimensional array
as
Product [3]
[3];
Format for declaration
data-type array-name[row size] [Column size]
Initializing two dimensional Array
Example
Int number [2][3]={10,20,30,30,20,10}
Int number [2][3]={(10,20,30),(30,20,10)}
Initializing in the form of matrix,
Int number [2][3]={{10 20 30}{30 20 10}};
Char name[2][6]={‘’sachin’’,’’saurav’’};
Example
Develop a program to show sum of
ten elements of array.
Algorithm
Flowchart
Program
Conclusion
Experiment no. 6
Object
To study string and develop the program by using string.
Theory
Array made up of character.
Memory one byte & last character is “\0”i.e. null
character.
Initialization
& Declaration of string
Format
Char
stringname[size];
At the time of assigning string to characters array, compiler
automatically places null characters(‘\0’) at the end of string. Therefore the
size of array should be consider after adding on character for null character
to the maximum number of character in the string.
Initializing may one of the following way-
1.
Char name [11]=” James Bond”;
2.
Char name [11]={‘J’,’a’,’m’,’e’,’s’,’
’,’B’,’O’,’n’,’d’,’\0’};
3.
Char name [ ]={‘J’,’a’,’m’,’e’,’s’,’ ’,’B’,’O’,’n’,’d’’\0’};
4.
Char name [ ]=”James Bond”
Example
1.
Develop a program for any statement of string.
Algorithm
Flowchart
Program
Conclusion
Experiment no. 7
Objective
To study function and develop the program for function.
Theory
Functions are used normally in those programs where some
specific work is required to be done repeatedly and looping fails to do the
same. Three things are necessary while using a function.
1) Declaring a
function or prototype
Format
return-type
function-name(argument)
Before defining a function , it is
required to declare the function i.e. to specify the function prototype. A function
declaration is followed by a semicolon ‘;’.
Unlike the function definition only data type are to be mentioned for arguments
in the function declaration.
2) Calling a function
Function call is made as follows
return-type
=function-name(arguments);
3) Defining a function
All the statements or the operations to be performed by a function are
given in the function definition which is normally given at the end of pogram
outside the main.
A function definition has
this form:
return-type function-name(parameter
declarations, if any)
{
declarations
statements
}
Function definitions can appear in
any order, and in one source file or several, although no function can be split
between files.
There are four types of function
depending upon the return type and argument,
1. No argument No return
2. No argument with return
3. With argument No return
4. With argument with return
Example
Develop a program to add two
integer numbers using function.
Algorithm
Flowchart
Program
Conclusion
Experiment no. 8
Objective
To study pointer and develop the program using pointer.
Theory
A pointer is a variable that
contains the address of a variable. Pointers are much used in C partly because
they are sometimes the only way to express a computation, and partly because
they usually lead to more compact and efficient code than can be obtained in
other ways.
A pointer is a group of cells
(often two or four) that can hold an address. So if ‘c’ is a char and ‘p’ is a
pointer that points to it, we could represent the situation this way:
The unary operator & gives the
address of an object, so the statement
p = &c;
assigns the address of c to the
variable p, and p is said to ``point to'' c. The & operator only applies to
objects in memory: variables and array elements. It cannot be applied to
expressions, constants, or register variables.
The unary operator * is the indirection
or dereferencing operator; when applied to a pointer, it accesses
the object the pointer points to.
The declaration of the pointer is:
int*p;
It
means that the expression *p is an integer. This definition set asides 2 bytes
in which to store the address of an integer variable and gives this storage
space the name p.
If
instead of int we declare
Char*p;
Again
in this case 2 bytes will be occupied, but this time the address stored will be
pointing to a single byte.
Example
Develop a program to show sum of
ten elements of array.
Algorithm
flowchart
Program
Conclusion
Experiment no. 9
Objective
To study structure and develop the program using structure.
Theory
Structure
One data-type, which can be used
for representing group of data items of different data types (int, chara, float
etc.). this data type is known as structure.
Structure Declaration:
Struct
structure-name
{
Variable
name;
};
Ex.
Struct
student
{
char
name[20];
char
course[10];
int fees;
};
In above example ‘student’ is the structure name. char and
int are structure member. [20], [10] are character array and ‘fees’ is integer
variable.
In structure dot operator/ member operator/ period operator
(‘.’) is used to link structure
member with structure variable.
Example
Develop a program to keep record
for student using structure.
Algorithm
Flowchart
Program
Conclusion
Experiment no. 10
Objective
To do mini project based on data structures, file handling,
graphics and basic object oriented programming concepts.
Equipments
Personal Computer, Turbo C,
etc.
Theory
Algorithm
Flowchart
Program
Conclusion
Comments
Post a Comment