CS113 Introduction to C - PowerPoint PPT Presentation

About This Presentation
Title:

CS113 Introduction to C

Description:

CS113 Introduction to C – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 17
Provided by: ioannisa
Category:

less

Transcript and Presenter's Notes

Title: CS113 Introduction to C


1
CS113Introduction to C
  • Instructor Ioannis A. VetsikasE-mail
    vetsikas_at_cs.cornell.edu
  • Lecture 3 August 30
  • webpage
  • http//www.cs.cornell.edu/Courses/cs113/2000FA/cs1
    13.htm

2
Logical Operators (additions)
  • Unary negation operator !
  • Ex if (!var) same as if (var 0)
  • What is if (!!var) equivalent to?
  • Lazy evaluation (logical AND/OR)
  • if ((x!0) (1/xgt1))
  • If x equals 0 then the whole boolean expression
    is false and thus (1/xgt1) does not get evaluated
    (good since otherwise it would give a divide by
    zero type error)
  • The evaluation order for and is guaranteed
    to be from left to right

3
Logical Operators (examples)
  • a1 b!2 !c
  • !(a1 bgt3) c
  • agtb bgtc

4
A little bit about Functions
  • Should perform a well-defined task
  • Why? Adds no functionality.
  • Breaking tasks into smaller ones make them easier
    to think about
  • Hiding details tends to make code less
    complicated, rendering it more readable
  • Easier to debug the code as well
  • Code can be re-used, not just within one program
    but in others.
  • Recursion easier to do
  • more on that later

5
More on functions
  • Syntax
  • return type ltnamegt (type param_name)
  • e.g. int factorial(int n)
  • e.g. void execute_loop(char c, float f)
  • Call as
  • ifactorial(3)
  • execute_loop(townInitial, distance)
  • Can also declare them
  • int factorial(int n) or int factorial(int)
  • Return (w/ or w/o value) return expr

6
Example A simple function
include ltstdio.hgt int max( int a, int b
) void main() int i 8, j 17
printf( Maximum of d and d is d\n, i, j,
max(i, j)) int max( int a, int b ) if(
a gt b ) return a else return b
7
Example 2 (Call by value)
  • What does it print?
  • A parameter of the function can be a constant,
    expression, variable etc. (anything that has a
    value!)
  • Only the value is passed (not variable!)

include ltstdio.hgt void printDouble( int x )
printf(Double of d, x) x 2
printf(is d\n, x) void main() int i
13 printDouble(i) printf(id\n, i)

8
One-Dimensional Arrays
  • Often, programs use homogeneous data. As an
    example, if we want to manipulate some grades, we
    might declare
  • int grade0, grade1, grade2
  • If we have a large number of grades, it becomes
    cumbersome to represent/manipulate the data using
    unique identifiers.
  • Arrays allow us to refer to a large number of the
    same data type using a single name. For
    instance,
  • int grade3

9
One-Dimensional Arrays (continued)
  • Makes available the use of integer variables
    grade0, grade1, grade2 in a program.
  • Declaration syntax
  • Type array_namenumber_of_elements
  • WARNING arrays are zero-indexed (numbering
    always starts at 0).
  • Now, to access elements of this array, we can
    write gradeexpr, where expr is an integral
    expression.
  • Example
  • for( i 0 i lt 3 i )
  • sum gradei

10
Pointers
  • A variable in a program is stored in a certain
    number of bytes at a particular memory location,
    or address, in the machine.
  • Pointers allow us to manipulate these addresses
    explicitly.
  • Two unary operators (inverses)
  • operator address of. Can be applied to any
    variable. Adds a star to type.
  • operator information at. Can be applied
    only to pointers. Removes a star from type
  • Pointer when declared points to an invalid
    location usually so you must make it point to a
    valid one.

11
Pointers (continued)
int a 1, b 2, p void void_p char
char_p p a b p
  • An assignment like char_p a is illegal, as
    the types do not match.
  • void is a generic pointer type can make
    assignments such as void_p char_p or void_p
    b
  • void is also the type of pointer returned by
    memory allocation functions (more later)

12
Constructs not to be pointed at
  • Do not point at constants
  • int ptr
  • ptr 3 / OK /
  • ptr 3 / illegal /
  • Do not point at arrays an array name is a
    constant.
  • int a77
  • void ptr
  • ptr a / OK /
  • ptr a / illegal /
  • Do not point at expressions that are not
    variables.
  • int k 1, ptr
  • ptr k 99 / OK /
  • ptr (k 99) / illegal /
  • Do not point at register variables (not presented
    yet!)
  • register int k1 int ptr
  • ptr k

13
Call by reference (not really)
  • Pointers allow us to perform something similar to
    call-by-reference (we are passing
    pointers/references by value)
  • call-by-reference allows a function to make
    changes to a variable that persist

void set_int_to_3( int p ) p 3
void swap( int p, int q ) int temp temp
p p q q temp
14
Arrays and Pointers
  • Assume int i, a10, p
  • The type of a is int .
  • a is equivalent to a0
  • a i is equivalent to ai
  • Correspondingly,
  • ai is equivalent to (a i)
  • In fact,
  • pi is equivalent to (p i)

for( p a p lt a10 p ) sum p
for( i 0 i lt 10 i ) sum (a i)
p a for( i 0 i lt 10 i ) sum pi
15
Example Arrays as Function Arguments(Array
passed by reference, so changes to array persist)
int change_and_sum( int a, int size ) int i,
sum 0 a0 100 for( i 0 i lt size i
) sum ai return sum void
main() int a5 0, 1, 2, 3, 4 printf(
sum of a d\n, change_and_sum( a, 5
)) printf( value of a0 d\n, a0 )
16
Arrays and Pointers (a difference)
  • An array is in essence a pointer
  • However
  • int i, a10, p
  • pa / equiv. to pa0 /
  • p / valid /
  • a / error! /
  • The name of an array is not a variable, so the
    only operator you can apply to it is
  • E.g. ai3
Write a Comment
User Comments (0)
About PowerShow.com