CSE1301 Computer Programming Lecture 4: Introduction to C - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

CSE1301 Computer Programming Lecture 4: Introduction to C

Description:

CSE1301 Computer Programming Lecture 4: Introduction to C Kymberly Fergusson http://www.csse.monash.edu.au/~kef – PowerPoint PPT presentation

Number of Views:166
Avg rating:3.0/5.0
Slides: 37
Provided by: Joey109
Category:

less

Transcript and Presenter's Notes

Title: CSE1301 Computer Programming Lecture 4: Introduction to C


1
CSE1301Computer ProgrammingLecture
4Introduction to C
Kymberly Fergusson http//www.csse.monash.edu.au/
kef
2
Topics
  • Basic structure of a C program
  • Very basic C phrases

printf(Hello World)
3
Recall
4
Machine Language
10100110 01110110 00100110 00000000 11111010
11111010 01001110 10100110 11100110
10010110 11001110 00101110 10100110
01001110 11111010 01100110 01001110
10000110 etc...
5
High-Level Language
include ltstdio.hgt int main() printf(Hello
World) return 0
Source code
Executable code
  • Compilers and linkers translate a high level
    program into executable machine code.

6
Why C?
  • Structured language
  • Standard library exists, allowing portability
  • See DD 2/e Appendix B (web links in DD 3/e)
  • Forouzan Gilberg Appendix F
  • Wide availability on a variety of computers
  • Low level activities possible
  • It can produce lean and efficient code
  • Widely used

7
History of C
  • CPL Combined Programming Language (Barron et al.,
    1963)
  • BCPL Basic CPL (Richards, 1969)
  • B (Thompson, 1970)
  • C KR C (Ritchie, 1972)
  • ANSI C American National Standards Institute C
    (X3J11, 1989)
  • C99 (JTC1/SC22/WG14, ISO/IEC 9899, 1999)

8
Basic Structure of a C Program
Example Hello World
Instructions
  • output Hello World

9
Basic Structure of a C Program
Example Hello World
C Program
include ltstdio.hgt int main() printf(Hello
World) return 0
Skeleton
10
Basic Structure of a C Program
Example Hello World
C Program
Also
include ltstdio.hgt int main() printf(Hello
World) return 0
void main()
Not recommended
11
Basic Structure of a C Program
Example Hello World
C Program
Also
include ltstdio.hgt int main() printf(Hello
World) return 0
main() return 0
Assumes int
12
Basic Structure of a C Program
Example Hello World
C Program
Also
include ltstdio.hgt int main() printf(Hello
World) return 0
main() return 0
Warning messages Return value expected
13
Basic Structure of a C Program
Example Hello World
C Program
include ltstdio.hgt int main() printf(Hello
World) return 0
Includes standard input / output library of
functions. Read Hash-include
14
Basic Structure of a C Program
Example Hello World
C Program
include ltstdio.hgt int main() printf(Hello
World) return 0
Curly braces mark the beginning and end of a
block of instructions.
15
Basic Structure of a C Program
Example Hello World
C Program
include ltstdio.hgt int main() printf(Hello
World) return 0
Instruction (function call) to output Hello
World
16
Basic Structure of a C Program
Statements (lines of instructions) always end
with a semi-colon ()
Example Hello World
C Program
include ltstdio.hgt int main() printf(Hello
World) return 0
17
Simple C Phrases
Example 1 Float
Instructions
  • let x be equal to 4.5
  • output the value of x

18
Simple C Phrases
Example 1 Float
C Program
include ltstdio.hgt int main() float x x
4.5 printf(f, x) return 0
Declares that x is a variable which can be
assigned a numerical value.
19
Simple C Phrases
Example 1 Float
C Program
include ltstdio.hgt int main() float x x
4.5 printf(f, x) return 0
Assigns the value 4.5 into the variable x.
(Remember right value is assigned to left
variable)
20
Simple C Phrases
Example 1 Float
C Program
include ltstdio.hgt int main() float x x
4.5 printf(f, x) return 0
Outputs the value of x. (f for float)
21
Simple C Phrases
Example 2 Sum
Instructions
C Program
  • input the value of x
  • input the value of y
  • output The sum is
  • output the value of x y
  • output a new line

include ltstdio.hgt int main() float x
float y scanf(f, x) scanf(f, y)
printf(The sum is ) printf(f, x y)
printf(\n) return 0
22
Simple C Phrases
Example 2 Sum
C Program
include ltstdio.hgt int main() float x
float y scanf(f, x) scanf(f, y)
printf(The sum is ) printf(f, x y)
printf(\n) return 0
x and y are both numeric variables
23
Simple C Phrases
Example 2 Sum
C Program
include ltstdio.hgt int main() float x
float y scanf(f, x) scanf(f, y)
printf(The sum is ) printf(f, x y)
printf(\n) return 0
Input the values of x and y respectively (note
the ampersand!)
24
Simple C Phrases
Example 2 Sum
C Program
include ltstdio.hgt int main() float x
float y scanf(f, x) scanf(f, y)
printf(The sum is ) printf(f, x y)
printf(\n) return 0
Outputs the string The sum is
25
Simple C Phrases
Example 2 Sum
C Program
include ltstdio.hgt int main() float x
float y scanf(f, x) scanf(f, y)
printf(The sum is ) printf(f, x y)
printf(\n) return 0
Outputs the sum of x and y
26
Simple C Phrases
Example 2 Sum
C Program
include ltstdio.hgt int main() float x
float y scanf(f, x) scanf(f, y)
printf(The sum is ) printf(f, x y)
printf(\n) return 0
Outputs a newline
27
Simple C Phrases if
Example 3 Mark
include ltstdio.hgt int main() char
name50 float mark printf("What is your
name? ") scanf("s", name) printf("What is
your mark? ") scanf("f", mark)
printf("\n") printf("Hi, ") printf("s",
name) printf("!\n")
/ The next output depends on the mark.
/ if (mark lt 50) printf("Sorry. You
failed.\n") else printf("Yay! You
passed!\n") return 0
28
Simple C Phrases if
Example 3 Mark
Declares that name is a variable which can
contain a string (i.e. words, names, etc.)
include ltstdio.hgt int main() char
name50 float mark printf("What is your
name? ") scanf("s", name) printf("What is
your mark? ") scanf("f", mark)
printf("\n") printf("Hi, ") printf("s",
name) printf("!\n")
/ The next output depends on the mark.
/ if (mark lt 50) printf("Sorry. You
failed.\n") else printf("Yay! You
passed!\n") return 0
29
Simple C Phrases if
Example 3 Mark
Outputs a prompt so the user knows what the
program wants.
include ltstdio.hgt int main() char
name50 float mark printf("What is your
name? ") scanf("s", name) printf("What is
your mark? ") scanf("f", mark)
printf("\n") printf("Hi, ") printf("s",
name) printf("!\n")
/ The next output depends on the mark.
/ if (mark lt 50) printf("Sorry. You
failed.\n") else printf("Yay! You
passed!\n") return 0
30
Simple C Phrases if
Example 3 Mark
include ltstdio.hgt int main() char
name50 float mark printf("What is your
name? ") scanf("s", name) printf("What is
your mark? ") scanf("f", mark)
printf("\n") printf("Hi, ") printf("s",
name) printf("!\n")
Inputs the string content of the variable
name.(Note no ampersand!)
/ The next output depends on the mark.
/ if (mark lt 50) printf("Sorry. You
failed.\n") else printf("Yay! You
passed!\n") return 0
31
Simple C Phrases if
Example 3 Mark
include ltstdio.hgt int main() char
name50 float mark printf("What is your
name? ") scanf("s", name) printf("What is
your mark? ") scanf("f", mark)
printf("\n") printf("Hi, ") printf("s",
name) printf("!\n")
/ The next output depends on the mark.
/ if (mark lt 50) printf("Sorry. You
failed.\n") else printf("Yay! You
passed!\n") return 0
(The usual. Note the ampersand for floats!)
32
Simple C Phrases if
Example 3 Mark
include ltstdio.hgt int main() char
name50 float mark printf("What is your
name? ") scanf("s", name) printf("What is
your mark? ") scanf("f", mark)
printf("\n") printf("Hi, ") printf("s",
name) printf("!\n")
/ The next output depends on the mark.
/ if (mark lt 50) printf("Sorry. You
failed.\n") else printf("Yay! You
passed!\n") return 0
Outputs the string name. (Note the s)
33
Simple C Phrases if
Example 3 Mark
Comments everything between / and / are
ignored by the compiler.
include ltstdio.hgt int main() char
name50 float mark printf("What is your
name? ") scanf("s", name) printf("What is
your mark? ") scanf("f", mark)
printf("\n") printf("Hi, ") printf("s",
name) printf("!\n")
/ The next output depends on the mark.
/ if (mark lt 50) printf("Sorry. You
failed.\n") else printf("Yay! You
passed!\n") return 0
34
Simple C Phrases if
Example 3 Mark
include ltstdio.hgt int main() char
name50 float mark printf("What is your
name? ") scanf("s", name) printf("What is
your mark? ") scanf("f", mark)
printf("\n") printf("Hi, ") printf("s",
name) printf("!\n")
/ The next output depends on the mark.
/ if (mark lt 50) printf("Sorry. You
failed.\n") else printf("Yay! You
passed!\n") return 0
This block of instructions is performed if the
comparison is true.
35
Simple C Phrases if
Example 3 Mark
include ltstdio.hgt int main() char
name50 float mark printf("What is your
name? ") scanf("s", name) printf("What is
your mark? ") scanf("f", mark)
printf("\n") printf("Hi, ") printf("s",
name) printf("!\n")
/ The next output depends on the mark.
/ if (mark lt 50) printf("Sorry. You
failed.\n") else printf("Yay! You
passed!\n") return 0
This block of instructions is performed if the
comparison is false.
36
Summary
  • Basic structure of a C program
  • Simple C phrases
  • Declaring numerical variables (floats)
  • Declaring string variables
  • Input (scanf) and Output (printf)
  • comments
  • The if...else... phrase

Readings Forouzan Gilberg, Chapter 1
Write a Comment
User Comments (0)
About PowerShow.com