You are on page 1of 67

' $

PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 1

Tutorial & Laboratory

 
 
Programming & Data Structure: CS11001/19001
 


Section - 9/C 

DO NOT POWER ON THE MACHINE

Department of Computer Science and Engineering


I.I.T. Kharagpur
Autumn Semester: 2007 - 2009 (27.07.2007)

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 2

Instructors

Name Tel. No. & email


A. Mukherjee 9433124004 & animeshm@cse· · ·
G. Biswas 281910 & goutam@cse· · ·
K.V.N. Kishore 9932574281 & kvnkishore@cse· · ·
Mayank Gupta 09212504753 & mayank@cse· · ·
R. Chakrabarty 9733504608 & rupsac@cse· · ·
S. Ghosh 9733388033 & Santosh.Ghosh
Siva V. AppaRao 9832793931 & siva@cse· · ·
S. Sadhu 9474487404 & sanjibs@cse· · ·
@cse.iitkgp.ernet.in, Santosh.Ghosh@gmail.com

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 3

Tutorial

1. No student will be allowed in the tutorial or


laboratory class without a C programming
book and a notebook (for tutorial).
2. Tutorial is to be done in the notebook (with
proper date) and at the end of the semester
the notebook is to be submitted for evaluation.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 4

Feedback

The number of students


• who have done computer programming:
• who have done programming in C, C++, Java
or Pascal:
• who have worked on Unix or Linux OS:

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 5

Laboratory Classes

27th July, 3rd Aug., 10th Aug. 17th Aug., 24th


Aug., 31st Aug., 7th Sept., 28th Sept., 5th
Oct., 12th Oct., 26th Oct., 2nd Nov., 9th Nov.,
16th Nov.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 6

Laboratory Marks Distribution

1. First class - Introduction (will not be


evaluated).
2. Assignment Evaluation (a): 40
3. Laboratory Test I (l1 ): 25
(7th September, 2007)
4. Laboratory Test II (l2 ): 35
(9th November, 2007)
5. Marks Obtained (t): a + l1 + l2 .
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 7

The Computing Environment

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 8

The Processor Hardware

• Intel Pentium IV
– Clock : 1.7GHz,
– Cache Memory : 12K µOP ET Cache + 8KB
L1 Data Cache, 256KB L2 Unified Cache,
– 32-bit processor.
• Main Memory : 256MB,
• Hard Disk : 40GB.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 9

OS, Editor and Compiler

• OS : Redhat Linux 9.x


• Programming Language : C
• Compiler : gcc (cc)
• Editor : emacs
• Window Manager : KDE

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 10

A Computer

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 11

VDU

Processor
Memory
Disk etc.

Keyboard
Mouse

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 12

CPU−Memory Bus

Cache Memory
Main
DMA etc. Memory
MMU

IO Bus
CPU

IO Controller IO Controller

Mouse IO Controller

Disk I
Graphics Network
Disk II VDU

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 13

CPU IO

BUS

User Prog
Compiler

OS
Memory

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 14

Central Processing Unit (CPU)

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 15

Instruction
Set

Control
Instruction
and Data
from
Registers
Memory

FPU ALU

Data Path

Figure 1: A CPU

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 16

 

A Pentium Processor Chip is not just a CPU 
 

but CPU + MMU + Cache Memory 

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 17

Memory Subsystem

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 18

Data

C
P
U Instruction
Disk
10 GB
L1 Cache L2 Cache Main Memory
KB 100 KB 100 MB

Figure 2: Computer Memory

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 19

A Compiler

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 20

Source
Language
Program

Compiler A Compiler is a
Software

Target
Language
Program

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 21

C Program

C Preprocessor
Object
Modified C Program
Module/
Library C Compiler

Assembly Language
Program
Linker Assembler

Object
Executable Module
Module (a.out) (test.o)

Figure 3: Preprocessor + Compiler + Assembler + Linker

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 22

The First C Program: first.c

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 23

/*** First C Program : first.c ***/


#include <stdio.h>
int main()
{
printf("The First C Program\n");
return 0 ;
}

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 24

How to Compile a C Program?

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 25

C Program to · · ·

• Assembly Language Program


cc -S first.c
first.c ⇒ first.s
• Object Module
cc -Wall -c first.c
first.c ⇒ first.o
• Executable Module (we shall use)
cc -Wall first.c
first.c ⇒ a.out
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 26

Let Us Compile & Execute

$ cc -Wall first.c
$ ./a.out
The First C Program
$ ls -l first.c a.out
-rwxr-xr-x 1 goutam users 11370
Jan 1 10:36 a.out
-rw-r--r-- 1 goutam users 153
Jan 1 10:36 first.c
$
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 27

Compile to Assembly Language Program

$ cc -Wall -S first.c
$ ls -l first.* a.out
-rwxr-xr-x 1 goutam users 11370
Jan 1 10:36 a.out
-rw-r--r-- 1 goutam users 153
Jan 1 10:36 first.c
-rw-r--r-- 1 goutam users 399
Jan 1 10:45 first.s
$
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 28

Compile to Object Module

$ cc -Wall -c first.c
$ ls -l first.*
-rw-r--r-- 1 goutam users 153
Jan 1 10:36 first.c
-rw-r--r-- 1 goutam users 816
Jan 1 10:50 first.o
-rw-r--r-- 1 goutam users 399
Jan 1 10:45 first.s
$
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 29

Assembly Language Program: first.s

.file "first.c"
.section .rodata
.LC0:
.string "The First C Program\n"
.text
.align 2
.globl main
.type main,@function
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 30

main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
subl $12, %esp
pushl $.LC0
call printf
addl $16, %esp
movl $0, %eax
leave

& %
ret
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 31

.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 3.2 20020903
(Red Hat Linux 8.0 3.2-7)"
 

Size: 399 Bytes 

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 32

Object Module: first.o

$ hexdump -C first.o | less


00000000 7f 45 4c 46 01 01 01 00 ... 00 |.ELF............|
00000010 01 00 03 00 01 00 00 00 ... 00 |................|
00000060 46 69 72 73 74 20 43 20 ... 0a |First C Program.|
00000070 00 00 47 43 43 3a 20 28 ... 32 |..GCC: (GNU) 3.2|
00000080 20 32 30 30 32 30 39 30 ... 48 | 20020903 (Red H|
00000090 61 74 20 4c 69 6e 75 78 ... 32 |at Linux 8.0 3.2|
00000300 00 00 00 00 10 00 00 00 ... 63 |.........first.c|
00000310 00 6d 61 69 6e 00 70 72 ... 00 |.main.printf....|
00000320 14 00 00 00 01 05 00 00 ... 00 |................|
00000330

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 33

Machine Code in Object Module: first.o

$ objdump -d first.o | less


$ objdump -d first.o | less
first.o: file format elf32-i386
Disassembly of section .text:
00000000 <main>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
......
13: 68 00 00 00 00 push $0x0
18: e8 fc ff ff ff call 19 <main+0x19>
1d: 83 c4 10 add $0x10,%esp
20: b8 00 00 00 00 mov $0x0,%eax
25: c9 leave

& %
26: c3 ret
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 34

Machine Code in Executable Module: a.out

$ objdump -d a.out | less


a.out: file format elf32-i386

08048328 <main>:
8048328: 55 push %ebp
8048329: 89 e5 mov %esp,%ebp
.....
804833b: 68 98 83 04 08 push $0x8048398
8048340: e8 23 ff ff ff call 8048268 <_init+0x38>
8048345: 83 c4 10 add $0x10,%esp
804834d: c9 leave
804834e: c3 ret
804834f: 90 nop
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 35

Let’s Start At The Very Beginning

• How to Start a Computer?


• How to write a C Program?
• How to Compile a C Program to an
Executable Module?
• How to Run (Create a Process with) an
Executable Module?

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 36

How to Start & Stop the Machine

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 37

1. Power On the machine (main cabinet first,


then the VDU).
2. Select the Linux option.
3. Enter the login name and then the password.
(a) login : cnn (nn is the machine number e.g.
c07).
(b) password : same as the login name.
4. Create one or two windows on the VDU (use
the icon).
5. One window is active at a time - use the
mouse to activate an window.
6. A window may be closed by pressing Ctrl-D.
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 38

Power-off the Machine

1. Close all windows.


2. logout from the machine (use the icon).
3. Shutdown the system.
4. Power-off the VDU (power of the main cabinet
will be put-off automatically).

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 39

How to Start the Emacs Editor

• Select an window and enter the command


emacs & .
• An emacs window is created.
• You may select the font size.
• Close the emacs window selecting the File
menu and then the Exit emacs entry.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 40

Write, Compile and Execute a C Program

1. Start emacs editor: emacs &


2. Select Open File of the File menu.
3. Give a file name: first.c (at the bottom of the
emacs window).
4. Type the following C Program and finally save
the buffer as a file first.c : Save Buffer of File
menu.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 41

/**** This is the first Program ****/


#include <stdio.h>
int main()
{
printf("Tamaso Ma Jyotir Gamaya\n") ;
return 0 ;
}

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 42

1. Run the compiler with your program as data:


$ cc -Wall first.c
2. If the compilation process ends without any
message, the file ‘first.c’ contains a well-formed
C Program and it has been translated to the
executable module ‘a.out’

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 43

1. Run the executable module:


$ ./a.out
2. It will flash the message
Tamaso Ma Jyotir Gamaya
on the VDU screen.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 44

If There is an Error

/**** This is the first Program ****/


#include <stdio.h>
int main(
{
printf("Tamaso Ma Jyotir Gamaya\n") ;
return 0 ;
}

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 45

Compiler Message

bash$ cc first.c
first.c:4: parse error before ‘{’
bash$

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 46

What to DO?

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 47

1. Open the file first.c in emacs.


2. Identify and fix the errors.
3. Save the modified file (same name).
4. Compile it again.

Any time you make a change in the file,


(a) You have to Save it and
(b) Compile it to get the new executable
module, a.out.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 48

1. If there is no more message from the compiler,


the C program is syntactically correct
(well-formed).
2. An well-formed C program may have logical
error. Error-free compilation does not
guarantee the correctness of a program.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 49

Getting the Slides

• Run Mozilla and access


http://facweb.iitkgp.ernet.in/∼goutam
• Follow the link
Programming & Data Structures ...
of Autumn: 2007 - 2008.
• Download the correct .ps/.pdf file.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 50

A Few Linux Shell (bash) Commands

• ls : Lists the directory contents.


• rm : Removes files from a directory.
• pwd : Prints name of the current/working
directory.
• mkdir : Makes Directories.
• rmdir : Removes empty directories.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 51

A Few Linux Shell (bash) Commands

• cd : Changes working directory.


• cp : Copy files and directories.
• mv : Moves/Renames files.
• man : Format and display the online manual
pages.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 52

A man Page

$ man ls
LS(1) User Commands LS(1)
NAME
ls list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current
directory by default). ....................
-a, --all
do not hide entries starting with .
& %
...........................................
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 53

File System

• Program and data are stored in a non-volatile


media as a named object called files.
• Contents of different files - C program, object
module, executable module, program data,
data for this presentation, etc.
• Files are to be organised in a systematic way.
• There are special files (in Linux) called
directories containing names of files and
subdirectories.
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 54

Directory Structure on a Machine

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 55

/ root

/pclab2 pclab2

/pclab/g07 g07
Working
Directory
assignment a.out practice first.c

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 56

Creating Directories

1. Create a subdirectory practice07 under your


working directory.
2. Close the emacs widow (if there is one).
3. Move the first.c under practice07.
4. Change current directory to practice07.
5. Open first.c using emacs again.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 57

Write, Compile and Execute

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 58

Example - II

/*** second.c ***/


#include <stdio.h>
int main() {
int n ;

printf("Enter a non Negative integer: ") ;


scanf("%d", &n) ; /* Reads an integer */
printf("\nSum of (0 + ... + %d) = %d\n",
n, n*(n + 1)/2 ) ;
return 0 ;
}

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 59

Example - III

/**** Area of a Circle: third.c ****/


#include <stdio.h>
#include <math.h>
int main() {
float radius, area ;

printf("Enter the radius :") ; scanf("%f", &radius) ;


area = 4.0 * atan(1.0) * radius * radius ;
printf("\n Circle-Area = %f for Radius = %f\n",
area, radius) ; return 0 ;
}

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 60

Compile with Mathematical Library

• $ cc -Wall -lm third.c


• Where is the mathematical library?
$ cd /lib
$ ls -l libm*.*
-rwxr-xr-x 1 root root 208984 Apr 8 2003 libm-2.3.2.so
lrwxrwxrwx 1 root root 13 Jul 15 2003 libm.so.6 ->
libm-2.3.2.so
$

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 61

Example - IV

// Convert Fahrenheit to Celsius: fourth.c


#include <stdio.h>
int main(){
float cel, fah ;
printf("Enter temp. in F: ") ;
scanf("%f", &fah) ;
cel = 5.0*(fah-32.0)/9.0 ;
printf("%6.2f F = %6.2f C\n", fah, cel) ;
return 0 ;
}

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 62

Example - V

#include <stdio.h>
int main() { // **** fifth.c
int first, second, max ;

printf("Enter two integers :") ;


scanf("%d%d", &first, &second) ;
if(first > second) max = first ;
else max = second ;
printf("\nMax(%d,%d) = %d\n",
first, second, max) ; return 0 ;
}

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 63

Example - VI

#include <stdio.h>
int main() { // **** sixth.c
int n, i, sum = 0 ;

printf("Enter a non-negative integer:") ;


scanf("%d", &n) ;
for(i = 0; i <= n; ++i) sum += i ;
printf("\nSum of (0 + ... + %d) = %d\n",
n, sum ) ;
return 0 ;
}

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 64

Example - VII

int sum(int n) { // *** seventh.c


if(n==0) return 0; else return n+sum(n-1);
}

#include <stdio.h>
int main() {
int n ;
printf("Enter a non-negative integer:") ;
scanf("%d", &n) ;
printf("\nSum of (0+...+%d)=%d\n",n,sum(n));
return 0 ; }
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 65

Try to Modify the Given Programs

1. Modify example-II to print the sum of an AP


series, where the first term (a) and the
common difference (b) and number of terms
(n) are three inputs.
2. Modify example-III to calculate the volume of
a sphere where again the radius is the input.
3. Modify example-V to read three integers and
find the smallest.

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 66

What Does it Do?

& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 67

#include <stdio.h>
int what(int n, int m)
{
if(m == 0) return n ;
else return what(m, n%m) ;
}
int main() {
int m, n ;
printf("Enter two non-negative integers:") ;
scanf("%d%d", &n, &m) ;
printf("\nwhat(%d, %d) = %d\n",m,n,what(m,n));
return 0 ;
}
& %

You might also like