Introduction to C Lecture 1 1122022 8 00

  • Slides: 33
Download presentation
Introduction to C • Lecture 1 1/12/2022 8: 00 AM 1

Introduction to C • Lecture 1 1/12/2022 8: 00 AM 1

Textbook Brian W. Kernighan & Dennis M. Ritchie The C Programming Language 2006 ISBN:

Textbook Brian W. Kernighan & Dennis M. Ritchie The C Programming Language 2006 ISBN: 7 -111 -19626 -0 《C程序设计语言》英文版第 2版 机械 业出版社 1/12/2022 8: 00 AM 2

Recommended Textbook 谭浩强 C程序设计(第 4版) 2010 ISBN: 9787302224464 清华大学出版社 1/12/2022 8: 00 AM 3

Recommended Textbook 谭浩强 C程序设计(第 4版) 2010 ISBN: 9787302224464 清华大学出版社 1/12/2022 8: 00 AM 3

Outline • • Introduction Operators and Expressions Branches Loops Arrays & Strings Functions Pointers

Outline • • Introduction Operators and Expressions Branches Loops Arrays & Strings Functions Pointers Structures 1/12/2022 8: 00 AM 4

What is a (Computer) Program? • A sequence of instructions written to perform a

What is a (Computer) Program? • A sequence of instructions written to perform a specified task with a computer. • Examples: − Windows XP, win 7, Android, i. OS. − IE, Office(Word/Excel/Power. Point), Photoshop. − Embedded system for home appliances. 1/12/2022 8: 00 AM 5

What to learn? • The C programming language − − • Syntax Structure How

What to learn? • The C programming language − − • Syntax Structure How to program in C − − Design Code Debug Test 1/12/2022 8: 00 AM 6

What is C language? • A general-purpose computer programming language. • One of the

What is C language? • A general-purpose computer programming language. • One of the most widely used programming languages. • Created in 1970 s by Dennis Ritchie. 1/12/2022 8: 00 AM 7

Won Turing Award in 1983 & National Medal of Technology 1/12/2022 8: 00 AM

Won Turing Award in 1983 & National Medal of Technology 1/12/2022 8: 00 AM 8

1/12/2022 8: 00 AM 9

1/12/2022 8: 00 AM 9

1. 1 Let’s Start: hello, world 1/12/2022 8: 00 AM 10

1. 1 Let’s Start: hello, world 1/12/2022 8: 00 AM 10

How to print: hello, world • • • Save the texts as hello. c

How to print: hello, world • • • Save the texts as hello. c Compile it. Link it. Run it. Finally the system would print: hello, world 1/12/2022 8: 00 AM 11

The first C program • What does the code mean? p 7 int main(

The first C program • What does the code mean? p 7 int main( ) return 0; • Points − #include head files − main function : p 6 − ; end of one statement − call library function: printf − note: {}, (), ”” 1/12/2022 8: 00 AM 12

Modified hello. c 1/12/2022 8: 00 AM 13

Modified hello. c 1/12/2022 8: 00 AM 13

A wrong version (p 7) • The compiler reports an error • n: for

A wrong version (p 7) • The compiler reports an error • n: for the newline character 1/12/2022 8: 00 AM 14

Rewrite the first program • three statements == one statement • produce identical output.

Rewrite the first program • three statements == one statement • produce identical output. 1/12/2022 8: 00 AM 15

Summarize hello. c • Design − print something on the console − use printf

Summarize hello. c • Design − print something on the console − use printf • Code − include − main − printf −; • Test 1/12/2022 8: 00 AM 16

For any program, we need Your code 1/12/2022 8: 00 AM 17

For any program, we need Your code 1/12/2022 8: 00 AM 17

The second program − Fahrenheit temperatures and their Celsius equivalents • Let’s program −

The second program − Fahrenheit temperatures and their Celsius equivalents • Let’s program − Design − Code − Test 1/12/2022 8: 00 AM 18

Design • How to convert Fahrenheit to Celsius? − The equation is • How

Design • How to convert Fahrenheit to Celsius? − The equation is • How to express F and C in a program? − Variables! − Declare a variable, then use it. • How to print a to-be-known value? − another usage of printf 1/12/2022 8: 00 AM 19

Code • Declare variables • Calculate • Print 1/12/2022 8: 00 AM 20

Code • Declare variables • Calculate • Print 1/12/2022 8: 00 AM 20

Test 1/12/2022 8: 01 AM 21

Test 1/12/2022 8: 01 AM 21

After Debug 1/12/2022 8: 01 AM 22

After Debug 1/12/2022 8: 01 AM 22

1/12/2022 8: 01 AM 23

1/12/2022 8: 01 AM 23

What we have learnt? • What is a program? • How to write a

What we have learnt? • What is a program? • How to write a program? − ***. c − edit, save, compile, link, run, debug • hello, world − The structure of a C program • Fahrenheit to Celsius − Variables 1/12/2022 8: 01 AM 24

Fahrenheit to Celsius 1/12/2022 8: 01 AM 25

Fahrenheit to Celsius 1/12/2022 8: 01 AM 25

A second way to initialize a variable • scanf − read something from the

A second way to initialize a variable • scanf − read something from the console − characters, integers, float numbers, strings, etc. . • Usage 1/12/2022 8: 01 AM 26

Test again 1/12/2022 8: 01 AM 27

Test again 1/12/2022 8: 01 AM 27

Summarize F 2 C. c • Variable declaration • Arithmetic expression − / between

Summarize F 2 C. c • Variable declaration • Arithmetic expression − / between integers − * cannot be omitted • printf and scanf of integers − %d 1/12/2022 8: 01 AM 28

The third program • Deal with characters • What are characters? − letters: ABC

The third program • Deal with characters • What are characters? − letters: ABC … − symbols: +-*/… • How does the computer represent characters? − Anything in a computer is stored as binary (Text, audio, image… ). − ASCII code for English characters. 1/12/2022 8: 01 AM 29

How to represent characters? integers 1/12/2022 8: 01 AM Type int a; Read scanf(“%d”,

How to represent characters? integers 1/12/2022 8: 01 AM Type int a; Read scanf(“%d”, &a); Print printf(“%d”, a); characters ? 30

How to represent characters? integers 1/12/2022 8: 01 AM characters Type int a; char

How to represent characters? integers 1/12/2022 8: 01 AM characters Type int a; char c; Read scanf(“%d”, &a); scanf(“%c”, &c); Print printf(“%d”, a); printf(“%c”, c); 31

ASCII-Code Table 1/12/2022 8: 01 AM 32

ASCII-Code Table 1/12/2022 8: 01 AM 32

Read & Print one character ? 1/12/2022 8: 01 AM 33

Read & Print one character ? 1/12/2022 8: 01 AM 33