C - PowerPoint PPT Presentation

About This Presentation
Title:

C

Description:

C ... c – PowerPoint PPT presentation

Number of Views:7
Avg rating:3.0/5.0
Slides: 26
Provided by: rn3
Category:

less

Transcript and Presenter's Notes

Title: C


1
C
2
About the Crash Course
  • Cover sufficient C for simple programs
  • variables and statements
  • control
  • functions
  • arrays and strings
  • pointers
  • Slides and captured lecture (video and sound) are
    available at
  • www.cim.mcgill.ca/jer/C/

3
About the Crash Course
  • Next installments
  • part II Wednesday, November 24, 1630-1800
  • part III Thursday, November 25, 1800-1930
  • Reference
  • Kernighan Ritchie "The C programming
    language", 2nd edition. Prentice-Hall, 1988.

4
Why C?
  • C is not always a good choice ...
  • Safety-critical systems
  • Component-based programming / RAD
  • ...
  • ... but in many applications you do want C
  • Legacy systems
  • Speed, memory requirements
  • Low-level programming (close to the machine)
  • ...

5
Your First C Program
  • "The only way to learn a new programming language
    is by writing programs in it" KR
  • / A simple program that prints something /
  • include ltstdio.hgt
  • main ()
  • printf ("Hello, world!\n")

6
Declarations
  • Example
  • float x
  • double d 5
  • int p, i, a100
  • char s21
  • Syntax
  • type variable_name, ... value
  • Rules
  • declarations must precede executable statements
  • int type may be modified by long, short,
    unsigned

7
Changing Variable Values
  • Example
  • int x, y, z
  • x 2
  • x x 1
  • Getting Fancy
  • y z 4 5
  • x 1
  • x
  • x
  • y x--
  • Note
  • assignment statements return value, which may or
    may not be ignored same goes for increment
    statements

8
Formatted Output
  • Example
  • int i 10
  • float f 2.5
  • char s "hi"
  • printf ("Jack\'s integer is d\n", i)
  • printf ("Jill\0x27s float is f\n", f)
  • printf ("My string s s\n", s)
  • Syntax
  • printf (string_with_formatting, var1, var2, ...)
  • Formats d integer, f float, c character, s
    string,
  • Note "escape sequences" \n newline, \' quote,
    \0x27, etc.
  • include ltstdio.hgt is compulsory more about it
    later

9
Formatted Input
  • Example
  • include ltstdio.hgt
  • int i
  • float f
  • scanf ("d f\n", i, f)
  • / inputs an integer and a float /
  • Syntax
  • scanf (string_with_formatting, var1, var2,...)
  • Note
  • The ampersand () is necessary because scanf
    modifies the values stored in the respective
    variables by comparison, printf only uses the
    values, without modifying them. More about this
    later

10
I/O Example
  • What does this print?
  • include ltstdio.hgt
  • main ()
  • int n
  • float x
  • char mark
  • scanf ("d f c", n, x, mark)
  • printf ("Of d s,\nf got c\s\n",
  • n, "students", x, mark)
  • Type in the following input 86 85.999 A

11
I/O Example
  • include ltstdio.hgt
  • main ()
  • int n
  • float x
  • char mark
  • scanf ("d f c", n, x, mark)
  • printf ("Of d s,\nf got c\s\n",
  • n, "students", x, mark)
  • Input
  • 86 85.999 A
  • Output
  • Of 86 students,
  • 85.999001 got A's

12
Conditional Statements
  • Example
  • if (age lt 0)
  • printf ("warning negative age\n")
  • age -age
  • Syntax
  • if (condition) statement
  • if (condition) statement else statement
  • Rules
  • the condition is an int ! (no booleans)
  • parentheses required around conditional
    expression
  • use curly braces to make a compound statement

13
More Conditionals
  • Example
  • if (x lt 0)
  • printf ("x is less than 0\n")
  • else if (x 0)
  • printf ("x is equal to 0\n")
  • else
  • printf ("x is greater than 0\n")
  • Whats wrong with this?
  • if (x lt 0)
  • if (y lt z)
  • printf ("y is less than z\n")
  • else
  • printf ("x not less than 0\n")

14
While Loops
  • Example
  • / print "hi" forever /
  • while (1)
  • printf ("hi")
  • Syntax
  • while (condition)
  • statement
  • Rules (again)
  • the condition is an int ! (no booleans)
  • parentheses required around conditional
    expression
  • use curly braces to make a compound statement

15
For Loops
  • Example
  • / print "hi" three times /
  • int i / i continues to exist when loop ends /
  • for (i 0 i lt 3 i)
  • printf ("hi")
  • Syntax
  • for (statement1 condition statement2)
  • statement3
  • Equivalent to
  • statement1
  • while (condition)
  • statement3
  • statement2

16
Loop Example
  • / print squares up to 100 /
  • main ( )
  • int j, up 100
  • for (j 0 j j lt up j)
  • printf ("d \n", j j)
  • Note
  • cant do for (int j 0 ...
  • waste of one multiplication per iteration
  • can you make it more efficient?

17
Example (contd)
  • / print squares up to 100 /
  • void main ( )
  • int j, up 100, sq
  • for (j 0 (sq j j) lt up j)
  • printf ("d \n", sq)
  • Note
  • recall equivalence to a while loop condition is
    evaluated before the loop body

18
Arrays
  • int years45
  • float temperatures 11
  • void main ()
  • years0 2000
  • temperatures11 -45.67
  • Rules
  • indices start at zero
  • maximum valid index is the size of the array
    minus 1
  • but C lets you go beyond the declared boundaries
  • temperatures11 is illegal

19
Characters
  • Characters
  • char a, b, c1, c2
  • a '0' b '\037' c1 'K' c2 c1 1
  • Assigns values 48, 31, 75, 76
  • The sequences '0',...,'9', 'a',...,'z',
    'A',...,'Z' contain characters numbered
    consecutively
  • Casting
  • printf ("c d\n", c1, (int) c1)
  • Outputs K 75

20
Strings
  • Strings are '\0'-terminated arrays of char
  • char s3 "hi" / invisible '\0' /
  • char t3 'h', 'i', '\0'
  • String operations
  • include ltstring.hgt
  • strlen ("there") / returns 5 /
  • strcpy (s, t) / copy t to s /
  • strcmp (s, t) / alphabetical comparison /

21
Exercise Caesar's code
  • A simple code used by Caesar in the Gallic wars.
  • Input sequence of capital letters
  • Output another sequence of capital letters
    obtained by shifting each letter in the original
    sequence three places in the alphabet. Note
    shifting wraps around.
  • Example KENNEDY -gt NHQQHGB

22
Functions
  • / Increment takes an integer argument and
  • returns the argument plus one.
  • /
  • int incr (int i)
  • int j
  • j i 1
  • return j
  • main ()
  • int k, m 4
  • k incr(m)
  • printf ("k d, m d\n", k, m)
  • output k 5, m 4

23
More about Functions
  • might have no return type, and no return
    statement
  • void printhi ()
  • printf ("hi\n")
  • parameters are copied and can be modified
  • int incr (int i)
  • i
  • return i
  • default (unspecified) return type is int

24
Variables within Functions
  • But this does not work
  • void no_incr (int i)
  • i
  • void main ()
  • int x 5
  • no_incr(x)
  • printf ("d\n", x)
  • beware that modifications are on internal copies
    of the parameters.
  • note void main() since main does not return a
    value

25
Exercise
  • Write a function that checks whether a sentence
    has 'title case'
  • Arguments sentence string
  • Assume the string consists of letters and blanks
    only.
  • Return true iff each word in the sentence starts
    with a capital letter and continues with
    lowercase letters.
Write a Comment
User Comments (0)
About PowerShow.com