Title: CSE1301 Computer Programming Lecture 4: Introduction to C
1CSE1301Computer ProgrammingLecture
4Introduction to C
Kymberly Fergusson http//www.csse.monash.edu.au/
kef
2Topics
- Basic structure of a C program
- Very basic C phrases
printf(Hello World)
3Recall
4Machine Language
10100110 01110110 00100110 00000000 11111010
11111010 01001110 10100110 11100110
10010110 11001110 00101110 10100110
01001110 11111010 01100110 01001110
10000110 etc...
5High-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.
6Why 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
7History 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)
8Basic Structure of a C Program
Example Hello World
Instructions
9Basic Structure of a C Program
Example Hello World
C Program
include ltstdio.hgt int main() printf(Hello
World) return 0
Skeleton
10Basic 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
11Basic 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
12Basic 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
13Basic 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
14Basic 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.
15Basic 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
16Basic 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
17Simple C Phrases
Example 1 Float
Instructions
- let x be equal to 4.5
- output the value of x
18Simple 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.
19Simple 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)
20Simple 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)
21Simple 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
22Simple 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
23Simple 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!)
24Simple 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
25Simple 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
26Simple 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
27Simple 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
28Simple 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
29Simple 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
30Simple 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
31Simple 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!)
32Simple 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)
33Simple 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
34Simple 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.
35Simple 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.
36Summary
- 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