29
CS 261 - Winter 2011 Introduction to the C programming language

CS 261 - Winter 2011 Introduction to the C programming language

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 261 - Winter 2011 Introduction to the C programming language

CS 261 - Winter 2011

Introduction to the

C programming language

Page 2: CS 261 - Winter 2011 Introduction to the C programming language

Why C ?

• C is simple language, makes it easier to focus on important concepts

• Java is high level language, C is low level, important you learn both

• Lays groundwork for many other langauges

Page 3: CS 261 - Winter 2011 Introduction to the C programming language

Comparing Java and C

• No classes, inheritance, or polymorphism

• Functions are the major mechanism for encapsulation

• Arrays and structures are the major data storage mechanisms

• Pointers!! Lots of pointers.

Page 4: CS 261 - Winter 2011 Introduction to the C programming language

Function definitions

• Functions look a lot like methods you are used to in Java, but are not part of a class

return-type function-name (arguments)

{

declarations; /* must come first!! */

function-body;

}

Page 5: CS 261 - Winter 2011 Introduction to the C programming language

Two level scope

• There are two levels of scope• Variables declared outside of any

function are global (use sparingly)• Variables declared inside of function are

local. Declarations must be listed first, before statements.

• You end up passing more inforation in arguments than you do in Java

Page 6: CS 261 - Winter 2011 Introduction to the C programming language

Parameters are by-value

• Arguments to functions are passed by value

• Means the parameter is initialized with a COPY of the argument

• Will simulate by-reference using pointers

Page 7: CS 261 - Winter 2011 Introduction to the C programming language

Structures

• Structures are like classes that have only public data fields and no methods:

struct arrayBag {

int count;

EleType data[100];

};

Page 8: CS 261 - Winter 2011 Introduction to the C programming language

Declaring a structure

• When you declare a variable you must use the struct keyword

struct arrayBag foo;

No constructors, no initialization, you must use explicit initialization routine

Page 9: CS 261 - Winter 2011 Introduction to the C programming language

Access to data fields

• Access to data fields uses the same dot notation you are used to:

struct arrayBag myData;

myData.count = 3;

(but often combined with pointers…)

Page 10: CS 261 - Winter 2011 Introduction to the C programming language

Pointers

• Pointers in C are explicit (implicit in Java)

• A pointer is simply a value that can refer to another location in memory.

42

A valueA pointer

Page 11: CS 261 - Winter 2011 Introduction to the C programming language

Pointer values vs. thing pointed to

• Important to distinguish between the value of the pointer and the value of the thing the pointer points to

42

Page 12: CS 261 - Winter 2011 Introduction to the C programming language

Some syntax

• Use * to declare a pointer, also to get value of pointer. Use & to get address of a variable (address == pointer)

double * p;double d, e;p = & d;*p = 3.14159;p = & e;*p = 2.718281;printf(“values: %d %g %g %g\n”, p, *p, d, e);

&d 3.14159

p d

Page 13: CS 261 - Winter 2011 Introduction to the C programming language

Pointers and Structures

• Pointers often combined with structures. Introduce some new syntax

struct arrayBag * p;

struct arrayBag g1;

p = & g;

p->count = 1; /* same as (*p).count */

p = 0; /* null pointer, doesn’t point to anything */

Page 14: CS 261 - Winter 2011 Introduction to the C programming language

Pointers vs object pointed to

• When you declare a variable it is not initialized, true for both pointers and structures

• Always be clear whether you are talking about a pointer, or thing pointed to

• 90% of your programming errors will come from uninitialized pointers or values

Page 15: CS 261 - Winter 2011 Introduction to the C programming language

Structures and arguments

• Remember, arguments are passed by value

• If you pass a structure, the ENTIRE structure is copied into the space for the actual argument value

• Wasteful, therefore we use pointers are arguments

Page 16: CS 261 - Winter 2011 Introduction to the C programming language

Pointers and by-reference parameters

A reference parameter can be simulated by passing a pointer:

void foo (double * p) { *p = 3.14159;}

double d = 2.718281;foo( & d);printf(“what is d now? %g\n”, d);

Page 17: CS 261 - Winter 2011 Introduction to the C programming language

Structures and by-ref params

• Very common idiom:

struct arrayBag a; /* note, value, not ptr*/

arrayBagInit (&a); /* pass by ref */

arrayBagAdd (&a, 3.14159);

Page 18: CS 261 - Winter 2011 Introduction to the C programming language

Arrays aways passed by ref

void foo(double * d) {

d[0] = 3.14159;

}

double data[4];

data[0] = 42;

foo(data); /* note - NO ampresand */

printf(“what is data[0]? %g”, data[0]);

Page 19: CS 261 - Winter 2011 Introduction to the C programming language

Notice, array has become a pointer

• Look carefully at the previous slide

• Notice the value was an array when it was passed as an argument

• But inside the function, we declared it as a pointer

• Pointers can be subscripted, just like arrays

Page 20: CS 261 - Winter 2011 Introduction to the C programming language

Dynamic Memory

• No new operator. Use malloc(#bytes) instead. Use sizeof to figure how big something is

struct gate * p = (struct arrayBag *)

malloc(sizeof(struct arrayBag));

assert (p != 0); /* always a good idea */

Page 21: CS 261 - Winter 2011 Introduction to the C programming language

Assert check conditions

• We will use assert to check all sorts of conditions. Halts program if condition not found.

# include <assert.h>

… /* assert checks condition is true */

assert(whatever-condition);

Page 22: CS 261 - Winter 2011 Introduction to the C programming language

BTW, no boolean

• BTW, there is no boolean data type.• Can use ordinary integer, test is zero or not

zero. Can also use pointers, test is null or not null.

int i;

if (i != 0)

if (i) /* same thing */

double * p;

if (p != 0)

if (p) /* same thing */

Page 23: CS 261 - Winter 2011 Introduction to the C programming language

Separation of interface and implementation

• Interface files (*.h) have declarations and function prototypes

• Implementation files (*.c) have implementations

prototype is function header but no body:

int max(int a, int b);

Page 24: CS 261 - Winter 2011 Introduction to the C programming language

Will often have interface given

• We will often give you the interface file, and you write the implementation

Page 25: CS 261 - Winter 2011 Introduction to the C programming language

Preprocessor

• A Preprocessor scans C programs before they are compiled. Used to make symbolic constants, conditionally include code

# define max 423

# if (max < 3)

# endif

Page 26: CS 261 - Winter 2011 Introduction to the C programming language

Ensuring declarations seen only one

/* interface file for foo.h */

# ifndef BigComplexName

# define BigComplexName

# endif

/* that way, 2nd time does nothing */

Page 27: CS 261 - Winter 2011 Introduction to the C programming language

First Assignment

• Good news on first assignment - it doesn’t have any pointers, only arrays

• Major problem should be just figuring out how to compile a C program

• Pointers will start to show up with second assignment and beyond.

Page 28: CS 261 - Winter 2011 Introduction to the C programming language

Will see more as we go along

• After class, Take a look at programming assignments 1 and 2

• In some place (assignment 2 and later) I will give you an interface file, hand in only implementation file (whatever.c)

• (Note: there is no interface file needed for first assignment).

• Questions?

Page 29: CS 261 - Winter 2011 Introduction to the C programming language

Now, review of Big Oh

• Now, a review of Big-Oh