Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to...

37
Introduction to Programming Lecture One N.R.Aravind I.I.T. Hyderabad 12 Sep 2017 1 / 37

Transcript of Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to...

Page 1: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Introduction to ProgrammingLecture One

N.R.Aravind

I.I.T. Hyderabad

12 Sep 2017

1 / 37

Page 2: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Ken Thompson and Dennis RitchieDennis Ritchie: Creator of the C language

Wrote the Unix operating system

[Source:Internet]

2 / 37

Page 3: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Administrative

Class webpagehttp://www.iith.ac.in/∼aravind/id1033

Office hours: Tue, Wed AN (Off: 232,E-Block)

Syllabus, grading policy, references, slides etc.

Installation instructions

3 / 37

Page 4: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

References

The C Programming Language by Kernighanand Ritchie

Practical C Programming by Steve Oualline

4 / 37

Page 5: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Grading Policy

15 marks: Attendance (Lab: 100%)

Skip a lab ⇒ Send me an email; attendanother lab session the same week

20 marks: Assignments

30 marks: Final Exam (Written)

35 marks: Final Lab Exam

No plagiarism

5 / 37

Page 6: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Lab Schedule

Lab Attendance: 100%

210 and 219

Wed: ME+ MA (Tomorrow’s labrescheduled)

Thu: CS + ES

Fri: CE + CH

Mon: EP + MS

6 / 37

Page 7: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Lab Exercises

To be shared by email, coursepage

Sample programs to read and execute

List of programs for you to write

T.A.s (Teaching Assistants)

7 / 37

Page 8: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Topics in this lecture

Introduction

The Print Statement

Variables and the Assignment statement

Input statement

8 / 37

Page 9: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

The C Programming Language

Invented around 1970

Operating systems like Linux

Computer graphics

For other languages (C is fast!)

Embedded systems

Solve engineering/scientific problems

Games

9 / 37

Page 10: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Machine Language

Source: Internet

10 / 37

Page 11: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Assembly Language

Source: Internet

11 / 37

Page 12: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Compilation

Source: Mark Burgess, Tutorial

12 / 37

Page 13: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Margaret Hamilton, Former Director of SoftwareEngineering, MITCode for Apollo 11Source: Internet

13 / 37

Page 14: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

RAM (Random AccessMemory) (Working memory)

Hard Disk (Secondarymemory)

Source: Wikipedia

14 / 37

Page 15: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

RAM Memory

Capacitor charge: 0 or 1Transistor: Switchsource: internet

15 / 37

Page 16: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

RAM Memory

source: www.csecsu.edu16 / 37

Page 17: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Memory

0 0 0 1 0 0 0 11 1 1 1 1 1 1 10 0 1 0 0 0 1 10 0 0 0 0 1 1 00 0 0 1 1 1 0 11 1 0 0 1 0 1 0

. . .

. . .

. . .

17 / 37

Page 18: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Memory

0 0 0 1 0 0 0 11 1 1 1 1 1 1 10 0 1 0 0 0 1 10 0 0 0 0 1 1 00 0 0 1 1 1 0 11 1 0 0 1 0 1 0

. . .

. . .

. . .

18 / 37

Page 19: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Memory

0 0 0 1 0 0 0 11 1 1 1 1 1 1 10 0 1 0 0 0 1 10 0 0 0 0 1 1 00 0 0 1 1 1 0 11 1 0 0 1 0 1 0

. . .

. . .

. . .

19 / 37

Page 20: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Memory

2300 0 0 0 1 0 0 0 12301 1 1 1 1 1 1 1 12302 0 0 1 0 0 0 1 12303 0 0 0 0 0 1 1 02304 0 0 0 1 1 1 0 12305 1 1 0 0 0 0 1 02306 . . .2307 . . .2308 . . .

20 / 37

Page 21: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

A simple C program: helloWorld.c

#include<stdio.h>main(){

printf(“Hello, World”);}

21 / 37

Page 22: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

A simple C program: helloWorld.c

#include<stdio.h>// stdio.h: Standard input-output header file// Contains declaration of printfmain() // Main point of execution{

printf(“Hello, World”);// printf(”Some text”);

}

22 / 37

Page 23: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Compiling and running a C program

$ gcc helloWorld.c -o hello$ ./helloHello, World $

23 / 37

Page 24: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Compiling and running a C program

$ gcc helloWorld.c -o hello

gcc: Gnu C Compiler

Translates the C program into machinecode named “hello”

-o: specifies the outpiut file name

$ ./hello

Run (execute) the program named ”hello”

To run a file named ”xyz”, type ./xyz.

24 / 37

Page 25: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Structure of C program

#include< >main(){Line 1Line 2Line 3...}Line 1 is first executed, followed by Line 2, followedby Line 3 etc.Sequential execution

25 / 37

Page 26: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

A simple C program: helloWorld.c

#include<stdio.h>main(){

printf(“Hello, World”);}

26 / 37

Page 27: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

More about printf

#include<stdio.h>main(){

char text[20]=“Hello, World”;printf(’‘%s”,text);

}

$./helloHello, World$

text: H e l l o , W o r l d \0

27 / 37

Page 28: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

More about printf

#include<stdio.h>main(){

char text[20]=“World”;printf(’‘Hello, %s”,text);

}

$./helloHello, World$

%s is replaced by the value in text.

28 / 37

Page 29: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

More about printf

#include<stdio.h>main(){

char movie[20]=“Interstellar”;char director[30]=“Christopher Nolan”;printf (“%s directed the film %s”, director,

movie);}What’s the output?$:Christopher Nolan directed the film Interstellar

The two %s are replaced by the values in director,movie resp.

29 / 37

Page 30: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

The assignment statement

#include<stdio.h>main(){

int a,b,c;a=10;b=20;c=a*b;

// a=10, b=20, c=400.printf(”The value of a is %d”,a);printf(”\n The value of b is %d”,b);printf(”\n The value of c is %d”,c);

30 / 37

Page 31: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

The assignment statement

// a=10, b=20, c=400.a=a+b;b=a+b;c=c+1;

// a=? b=? c=?// a=30 b=50 c=401printf(”\n The value of a is %d”,a);printf(”\n The value of b is %d”,b);printf(”\n The value of c is %d \n”,c);}

31 / 37

Page 32: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Variables in memory

0 0 0 1 0 0 0 11 1 1 1 1 1 1 10 0 1 0 0 0 1 10 0 0 0 0 1 1 00 0 0 1 1 1 0 11 1 0 0 1 0 1 0

. . .

. . .

. . .

32 / 37

Page 33: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Variables in memory

Address Value2300 172301 2552302 352303 62304 292305 1942306 .2307 .2308 .

int a,b,c;

33 / 37

Page 34: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Variables in Memory

Address Value2300 17

a 2301 2552302 352303 6

b 2304 292305 1942306 .

c 2307 .2308 .

int a,b,c;

34 / 37

Page 35: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Input: helloUser.c

#include<stdio.h>main(){

char userName[20];printf(’‘Hello, user. What’s your name? ”);scanf(“%s”,userName);

}

35 / 37

Page 36: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

Input a number

#include<stdio.h>main(){

int num;printf(’‘Enter a number: ”);scanf(“%d”,&num);

}

36 / 37

Page 37: Introduction to Programming Lecture Onearavind/Files-ID1303/ProgC-Lec1.pdf · Introduction to Programming Lecture One Author: N.R.Aravind Created Date: 9/13/2017 11:58:38 AM ...

TO BE CONTINUED...