0% found this document useful (0 votes)
39 views

Manual Tasks Assignment

1. Copy the Turbo C folder to the root of your hard drive (e.g. C:\TurboC). 2. Open Turbo C by double clicking the TC.exe file. This launches the IDE. 3. To create a new file, click File > New. Save files in the BIN folder within the Turbo C folder. 4. Check that the directory paths match the location of your Turbo C compiler before compiling.

Uploaded by

Alishba Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Manual Tasks Assignment

1. Copy the Turbo C folder to the root of your hard drive (e.g. C:\TurboC). 2. Open Turbo C by double clicking the TC.exe file. This launches the IDE. 3. To create a new file, click File > New. Save files in the BIN folder within the Turbo C folder. 4. Check that the directory paths match the location of your Turbo C compiler before compiling.

Uploaded by

Alishba Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Laboratory Workbook

Computer Programming

NAME: ALISHBA SALEEM


REG ID # 12619
CAMPUS : AIRPORT CAMPUS

Name ALISHBA SALEEM


Roll No ______________________
Semester _____________________

Marks ______________ Signature____________________

Department of Computer Science


2016
Introduction
Computer programming (often shortened to programming) is a process that leads from an original formulation of
a computing problem to executable computer programs. Programming involves activities such as analysis,
developing understanding, generating algorithms, verification of requirements of algorithms and implementation
of algorithms in a target programming language. The purpose of programming is to find a sequence of
instructions that will automate performing a specific task or solving a given problem.

Computer programming involves developing lists of instructions - the source code representation of software.
Source code is written in one or more programming languages. Following are types of programming languages.

1. Machine Language
The fundamental language of the computer’s processor, also called Low Level Language. All programs
are converted into machine language before they can be executed. It consists of combination of 0’s and
1’s.

2. Assembly Language
A low level language that is similar to machine language. It uses symbolic operation code called
mnemonics to represent the machine operation code.

3. High level Language


These Computer programming languages are easier to learn. Uses English like statements. This is not
machine readable thus needs to be converted to machine language before execution.
Examples: C, C ++, Visual Basic, Pascal, FORTRAN etc.

C-language
C is a high-level and general purpose programming language that is ideal for developing firmware or portable
applications. C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System (OS) in the early
1970s.

C belongs to the structured, procedural paradigms of languages. It is proven, flexible and powerful and may be
used for a variety of different applications.
Ranked among the most widely used languages, C has a compiler for most computer systems and influenced
many popular languages – notably C++.
Content
Lab No. Lab Objective

1 Introduction to C
2 Console Output
3 Data Types & Variables
4 Console Input
5 Operators in C & Math functions
6 Loops
7 Selection Statements
8 Functions
9 Arrays
10 Structures
11 Pointers
12 Graphics
13 Charts – Graphics
14 Filing
15 Hardware Programming
Lab Details
Lab Marks
Lab Objective Signature
No. Obtained
Introduction to C
Objective: Getting familiar with the IDE and working with Turbo C
1
& Microsoft Visual Studio.Installing and copying the compiler,
changing directory settings.Making first program in C.
Student Feedback:

Console Output
2 Objective:Understanding and Using format specifier and escape
sequences with printf.
Student Feedback:

Data Types & Variables


3 Objective:Studying different data types, variables, variable names,
variable declaration, variable definition and variable initialization
Student Feedback:

Console Input
4 Objective:Taking Input from the user at console screen using scanf
and getche commands.
Student Feedback:

Operators in C &Math functions


5 Objective:Arithmetic operators, conditional operators, assignment
operators, Increment/decrement operators.Studying Math functions.
Student Feedback:

Loops
Objective:Studying loops. For loops, nested for loops, while loops,
6
nested while loops, do while loops, nested do while loops.Studying
loops with cross combination.
Student Feedback:

Selection Statements
7 Objective:Decision making and conditioning using If statements, If-
else statements, switch-case.
Student Feedback:
Functions
8 Objective:User defined functions, passing values to function, and
returning values from functions.
Student Feedback:

Arrays
9 Objective:Arrays, array index, single and multi-dimensional arrays.
Arrays and loops. Sorting data in arrays.
Student Feedback:

Structures
10 Objective:Studying Structures, different ways to declare define and
initialize structures.
Student Feedback:

11 Pointers
Objective:Learnhow Pointers access data using memory address.
Student Feedback:

Graphics
12 Objective:Graphics, basic applications of graphics by making
buttons and animations.
Student Feedback:

13 Charts -Graphics
Objective:Draw different charts using C graphics.
Student Feedback:

Filing
14 Objective:Learn to create, open, write and close text files for data
storage With C language.
Student Feedback:

Hardware Programming
15 Objective:Understanding registers and accesses the hardware using
C language. Learn the basics of Inline Assembly.
Student Feedback:

Note: Students are advised to write their comments about whether the objective of the lab was achieved or not in
Student Feedbacksection.
Experiment 1 .
Objective
Getting familiar with the IDE and working with Turbo C &Microsoft Visual Studio. Installing and copying the compiler,
changing directory settings. Making first program in C. Saving / copying files to USB or other storage devices.

Unit 1.1: Getting familiar with the IDE

Theory

IDE
An integrated development environment (IDE) is a software suite that consolidates the basic tools developers need
to write and test software. Typically, an IDE contains a code editor, a compiler or interpreter and a debugger that the
developer accesses through a single graphical user interface (GUI). An IDE may be a standalone application, or it
may be included as part of one or more existing and compatible applications.

There are different IDE’s to write C program. Following are the two most common;
1. Turbo C
2. Microsoft Visual Studio

1. Turbo C
Turbo C is a discontinued Integrated Development Environment and compiler for the C programming language from
Borland. First introduced in 1987, it was noted for its integrated development environment, small size, fast compile
speed, comprehensive manuals and low price.In May 1990, Borland replaced Turbo C with Turbo C++.
The use of this IDE on latest OS has become very difficult as a lot of libraries are not supported any more. The use of
this IDE is depreciated.

Working with Turbo C


Click on Start Run
and type the path
\\fileserver\Softwares\Programing Languages Section

A new window will open. From there right click on the folder named as TC3 and select copy. Now Paste in it in your C
or D partition or your USB drive. As a good approach C drive is normally not recommended for C compiler as C drive is a
windows system partition.
Assuming that the folder has been copied in the root of D drive, open D: partition- TC3 folder - BIN –
Double click and open any one of the two TC.exe icons (as shown on right).
The window that opens is the known as the IDE of Turbo C (as shown below)

Figure 1.1: Turbo C

6
Figure 1.2: Turbo C IDE
This IDE window further contains two more windows and a standard tool bar. The blue window is known as the text editor
which is used to write the programs. And the Light bluish green window at the bottom is the message box which will
display the error or other messages.
Before using the IDE there are few parameters and checks which need to be reviewed every time the IDE of windows is
used. Make sure that the folder of TC3 (also known as compiler) has been copied in the root drive i.e. in the first
window of any partition and not in sub folders. e.g.

D:\TC3 is correct but D:\TC3\CP\C language\Class work\TC3 is incorrect.

Opening a new File


Close all windows in the Turbo C IDE (if there are any) by clicking in the small green box on the right top corner of each
window .
On the standard tool bar click file, select new. A new Blue window appears.

Saving a New File


Press F2 key to save this file. Make sure the path where you save this file is same as of compiler
e.g. D:\TC3\BIN
this path is visible at the bottom of save window. The name of the file will always be followed by .cpp extension (dot cpp).

Opening an existing file


To open any previously saved file, click File, select open, in the path option type D:\TC3\BIN\*.*
Asteric means all. The asteric before the dot means all file names and the asteric after the dot means all file types.
*.cppmeans all files with dot cpp extension.

Setting Directory Paths (to be checked each time the Turbo IDE is opened)
Once you have opened and saved a new file some directories need to be changed according to the location of your
compiler in your computer otherwise the compiler make not work properly.
On the standard toolbar click on options then directories. Set the first letter of the first two paths (i.e. Include Directories
and Library Directories) according to the location of the compiler or say the letter of the directory where you copied your
compiler. The other two paths (i.e. Output Directories and Source Directories) will remain blank. e.g.

Figure 1.3: Turbo C - Directory Paths

Compilation and Output


After you have written the program press Ctrl+f9 to compile and check the output.
Alt+F5 key is used to display the output for last compilation.
Technical Exits
To minimize the screen of Turbo C editor press Alt+Enter.
If some where the program hangs up compiler at output or gets busy without passing control to programmer press
Ctrl+Pause\Break.

8
2. Microsoft Visual Studio
Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It is used to develop computer
programs for Microsoft Windows, as well as web sites, web applications and web services. Visual Studio uses Microsoft
software development platforms.

Visual Studio includes a code editor supporting IntelliSense as well as code refactoring. The integrated debugger works
both as a source-level debugger and a machine-level debugger. Visual Studio supports different programming
languages and allows the code editor and debugger to support nearly any programming language. Built-in languages
include C, C++, VB.NET, C# and F#.

Working withMicrosoft Visual Studio

Open Microsoft Visual Studio by double clicking on the Icon. The icon similar to the following (depending upon the version)
should be available on your desktop.

Figure 1.4: Visual Studio 2013

The application will run and following window will appear.

Figure 1.5: Visual Studio Start Page

We first have to create a project. To create a new project, click on “New Project” option present on the left side. A
Window similar to the one shown below will appear. Expand the template option present on left side of the window.
Select “Visual C++” and under this, select “General” option. Select first option “Empty Project” from the middle
section. Provide name for the project as “Lab1” and press OK.

9
Figure 1.6: Create new project

After you click OK, following screen will be displayed which has a Solution Explorer on the right hand side. This panel will be used to
access all the files of the project.

Figure 1.7: Solution Explorer

Now right click on your project “Lab1, and go to Add and then select New item, refer to the screenshot below.

10
Figure 1.8: Add new item

A window similar to one given below will appear. Select Visual C++ and code from the left panel. Select C++ file from
the middle panel. Give a name to your source file. This will by default create a file with .cpp extension which
represents(C++) but we have to create a C file. Change the extension to .c and click Add.

Figure 1.9: Create a C file

Your file will appear in solution explorer under folder Source Files.

11
Figure 1.10: File created under Source Files

Now you can simply add a C program and execute the file.

Unit 1.2: Making first program in C

First C Program

Figure 1.11: Basic C Program

#include<stdio.h>
#include<conio.h>
The first line of this program is #include<stdio.h> . Here #include tells the compiler to include something and stdio means
Standard Input/Output where as .h means that it is a header file or in short we are including standard input/output header
file. In the same way the next line is similar to the first one but it has conio.h in it where conio means the black colour
console screen where we see our output and .h tells us that this is a header file. In other words we tell the compiler that
we are including two files and the code we will write will be of c language as the compiler also supports C++ code.

int main()
This is third line. The first word in this is void which means any thing that has no value or is useless. The next word is
main which is the brain function and is the only function readable in most programming languages. All the other functions
are called within the main function. The third word within brackets is also void. The first void means that the main function
won’t return any value while the second void means that the main function is not going to accept any value. This complete
line means that we have declared the main function here. For example a simple calculator takes two or more numbers
from you and returns the result like addition, subtraction, multiplication etc. It may be confusing at this time to understand
the void functionality but in experiment 11 we will discuss it in detail.

{
Following the main function line is a curly bracket which marks the starting of mains body. Always remember that if any
bracket is opened in C language it needs to be closed hence the last line of the above program marks the ending of main
function.

system(“cls”);
This is the first function called inside the main body. All functions will always have a round bracket closing ‘ )’ and a
12
semicolon ‘;’ at their end. In that way we easily recognize the number of functions in a program. According to this rule we
can identify 3 functions in this program. However other properties in different functions may differ but this identity will be
common in all functions. clrscr is short form of clear screen. It clears the screen of any previous output. If not used wont
create any error but output will show all previous compilation result.

printf(“My First Program”);


This line has another function known as printf which is used for printing. Within the double quotes is the main body of this
function where any character may be printed.

retrun 0;
This is the last function of this program. Its name is short form of get character. Its purpose is to get a character. Its used
in last to stop the program until a character is entered. If not used wont show any error but the output will not stop after
compilation. In this case the output is checked manually by pressing Alt+F5 key.

The C program given above will give some compile time errors in Visual Studio. Write following code in the .c file.

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

int main()
{
system("cls");
printf("My First Program");
retrun 0;
}

Save this file (Ctrl + s). Click on the green play icon present on top of the screen to compile and execute your first program.

Figure 1.12: Execute the Code

A black window will appear which is called console. The console will display the output of your program.

Figure 1.13: Program Output

Note: There are few functions of old C which is no more available in latest IDEs like clrscr(). Replace clrscr() with
system("cls") and getch() with _getch().
13
Example
Program Output
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()

{ My First
system(“cls”); Program
printf("My First Program");

Exercise
Carefully look at the following programs and write the output.
Program Output
#include<stdio.h>
#include<conio.h> My First Program
int main()
{
printf("My First Program");
}
#include<stdio.h>
#include<conio.h>
int main() My First Program
{
printf("My First Program");
}

#include<stdio.h>
#include<conio.h>
int main()
{ My First Program
printf("My First Program");
}

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main() My First Program
{
system(“cls”);
printf("My First Program");
}

#include<stdio.h>
#include<conio.h>
My First Program
#include<stdlib.h>
int main()
{
system("cls");
printf("My First Program");
}

#include<stdio.h>
#include<conio.h>
#include<stdlib.h> My First Program
int main()
{
system("cls");
printf("My First Program");

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
My First Program
int main()
{
system("cls");

printf("My First
Program");

14
Program Output
#include<stdio.h>
#include<conio.h>
My First Program
#include<stdlib.h>
int main()
{
system(“cls”);
printf("My First Program");

#include<conio.h>
#include<stdio.h> My First Program
#include<stdlib.h>
int main()
{
system(“cls”);
printf("My First Program");
}

#include”conio.h”
#include”stdio.h” My First Program
#include”stdlib.h”
int main()
{
system(“cls”);
printf("My First Program");
}

#include<conio.h>
#include<stdio.h> My First Program
#include<stdlib.h>
main()
{
system(“cls”);
printf("My First Program");
}

#include<CONIO.H>
#include<STDIO.H> My First Program
#include<STDLIB.H>
int main()
{
system(“cls”);
printf("My First Program");
}

Assignment
Write a C program using commands described in this lab to make a resume showing your
complete details.

15
16
Experiment 2 .
Objective
Understanding and Using format specifier and escape sequences with printf.

Theory
The C library functionprintf() sends formatted output to stdout.Following is the declaration for printf() function.

int printf(const char *format, ...)

Unit 2.1: Format specifier

format − This is the string that contains the text to be written to stdout. It can optionally contain embedded format tags that are
replaced by the values specified in subsequent additional arguments and formatted as requested.

Format specifiers are used to substitute and print values inside a printf or scanf statement which are further applicable on
variables. Below is a chart of format specifier examples using printf.

No. Type Syntax Value Example


1 Single Character %c One character within single quotes printf(“%c”,’a’);
2 String %s A sentence of an unfixed length within double quotes printf(“%s”,”Iqra Univ”);
3 Decimal Integer %d Any whole number between -32,768 to 32,767 printf(“%d”, 12345);
4 Long Integer %ld Any number between -2,147,483,648 to 2,147,483,647 printf(“%ld”,1234567);
5 Float %f Any decimal point number between 1 0-38
to 1 038 printf(“%f”, 1234.567);
6 Double %lf Any decimal point number between 10-308 to 10308 printf(“%lf”,12345678);

Unit 2.2: Escape sequences with printf

Escape Sequences are used to adjust spacing between lines or characters or the characters themselves.

No. Syntax Application Example


1 \n New Line printf(“\n”);
2 \t Tab eight spaces to right printf(“\t”);
3 \b Back space One space back printf(“\b”);
4 \r Carriage return Start of same line printf(“\r”);
5 \’ Printing single quote printf(“\’”);
6 \” Printing double quotes printf(“\””);
7 \\ Printing back space printf(“\\”);
8 \xdd Printing characters by Hexa decimal ASCII value printf(“\x45”);
9 \ddd Printing characters by decimal ASCII value printf(“\d45”);

Example
Program Output
#include<stdio.h>
#include<conio.h>

17
#include<stdlib.h> a
int main()
Iqra University

{
system(“cls”); 20
printf("\n%c",'a');
printf("\n%s","Iqra
printf("\n%d",20); University"); 35.00000
printf("\n%f",35.5);
pri ntf("\n%ld", 1234567);; 1234567

}
#include<stdio.h>
#include<conio.h> a Iqra Univeristy 20 35.00000 1234567
#include<stdlib.h>
int main()
{
system(“cls”);
printf("\n%c %s %d %f %ld",'a',"Iqra University",20,35.5,1234567);

18
Write the output for following programs and give reasons.
Program Output
#include<stdio.h>
#include<conio.h> 97
#include<stdlib.h>
int main() Iqra university
{
system(“cls”);
printf("\n%d",'a'); printf("\n%s","Iqra 35.00000
University"); printf("\n%c",20); 1234567
printf("\n%f",35.5);
pri ntf("\n%ld", 1234567);
retrun 0;
}

Assignment
Write a C program to make your resume with format specifiers and escape sequences showing your complete details.

19
Experiment 3 .

Objective
Studying different data types, variables, variable names, variable declaration, variable definitionand variable initialization.

Unit 3.1: Data types & variables

Theory
Variables are declared by first writing
data types followed by a variable name, e.g.
int a=10;

Here

int is data type,


a is variable name
and after the equals to sign (=) is the value in it 10
the value is always followed by a terminator(;)

Supported Example
No. Data Type Syntax format Specifier Value
1 Single Character char %c One character within single quotes char a=’a’;
2 Decimal Integer int %d Any whole number between -32,768 to 32,767 int a=10;
3 Long Integer long int %ld Any number between -2,147,483,648 to 2,147,483,647 long int a=12345;
4 Float float %f Any decimal point number between 10-38 to 1038 float a=1234.567;
0-308
5 Double double %lf Any decimal point number between 1 to 10308 double a=1 23456;

Variable Names
Variable names will always start with an alphabet.
Variable names can contain numbers (1,2,45,66) and underscores (_) but no other special characters (!@#$%^&*).
Variable names cannot resemble to any predefined word e.g. include, printf, getch, scanf etc..
A variable name cannot be used for multiple declarations.

Unit 3.2: Example codes

Example
Program Output
a 12 12.500000 If

21
b 13 13.500000 If
Write the output for following programs.
Program Output

Assignment
Write a C program to make a resume showing your complete details using variables.

23

You might also like