DOC PREVIEW
U of I CS 241 - C Crash Course

This preview shows page 1-2-17-18-19-36-37 out of 37 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C Crash Course Tarek Abdelzaher The C Language Spirit Made by professional programmers for professional programmers Very flexible very efficient very liberal Does not protect the programmers from themselves Rationale programmers know what they are doing even if looks bad enough to deserve a Darwin award see http www darwinawards com UNIX and most serious system software servers compilers etc are written in C Can do everything Java and C can It ll just look uglier in C 1 Programs Define Data types and variables Algorithms for manipulating those variables Example main int year year 2007 printf hello world This is d n year Programs Define Data types and variables Algorithms for manipulating those variables Example Declaration of variables typename varname main Assignment statement int year year 2007 printf hello world This is d n year Format string 2 A Memory View of a Program Memory storage Address1 Data1 Address2 Data2 Address3 Data3 Algorithms to Read and Update Data Address4 Data4 A program is a set of variables and a set of instructions that read update them Address5 Data5 PART I Data 3 Basic Data Types Integers int double Characters char Floating point variables float Pointers contain a memory address int char float void Arrays Strings Basic Data Types Integers int double Characters char Floating point variables float Pointers contain a memory address int char float void Arrays Strings 4 Data Variable Declaration Format typename varname varname int x y float p int z q char c m Data Variable Declaration Format typename varname varname int x y float p int z q char c m C is pointer to char m is a char NOT pointer Think char c m 5 More on Pointers The i e address of operator before a variable returns the memory address of the variable This address is a pointer The i e thing pointed to by operator before a pointer returns the variable that the pointer points to int x p int y y 1 p x p y 1 Tracing the Example int x p int y y 1 p x p y 1 x y p 6 Tracing the Example int x p int y y 1 p x p y 1 x y 1 p Tracing the Example int x p int y y 1 p x p y 1 x y 1 p 7 Tracing the Example int x p int y y 1 p x p y 1 x 2 y 1 p Allocating Memory to Hold Variables pointerToBuffer malloc buffersize Example int x p p int malloc sizeof int p 10 8 Example How much is y at the end int y x p x 20 p 10 y x p Example How much is y at the end int y x p x 20 p 10 y x p BAD Dereferencing an unitialized pointer will likely segfault or overwrite something Segfault unauthorized memory access 9 Arrays int x 5 x x 0 x 1 x 2 x 3 x 4 Name of array is a pointer Example int x 5 x 1 1 x 4 10 x x 0 x 1 x 2 x 3 x 4 1 10 10 Array Name as Pointer What s the difference between the examples below Example 1 int x 10 int y y x Example 2 int x 10 int y y x 0 Array Name as Pointer What s the difference between the examples below Example 1 int x 10 int y y x Example 2 int x 10 int y y x 0 NOTHING x the array name is a pointer to the beginning of the array which is x 0 11 Question What s the difference between int array int arr 5 What s wrong with int arr 5 arr 1 1 arr 2 2 arr 5 5 Question What s the difference between the examples below Example 1 int arr 5 arr 3 6 Example 2 int arr 5 arr 3 6 NOTHING Incrementing a pointer increases the address it points to by the given number of elements of the right type 12 Question What is the value of a 3 at the end int a 4 int p a 0 4 a 1 3 a 2 10 p a p 2 20 a 3 a 1 a 2 Question What s the difference between the examples below Example 1 int arr 5 arr 3 6 Example 2 int arr 5 arr 3 6 13 Strings Null terminated Arrays of Char Example char s 5 Strings are arrays that contain the string characters followed by a Null character to indicate end of string Do not forget to leave room for the null character s s 0 s 1 s 2 s 3 s 4 Strings Null terminated Arrays of Char Example char s 5 Strings are arrays that contain the string characters followed by a Null character to indicate end of string Do not forget to leave room for the null character Question How many characters can the string below hold char s 5 14 Conventions Strings string c Character c String Operations strcpy strlen srtcat strcmp 15 strcpy strlen Syntax strcpy ptr1 ptr2 where ptr1 and ptr2 are pointers to char value strlen ptr where value is an integer and ptr is a pointer to char Example int len char str 15 strcpy str Hello world len strlen str strcpy strlen What s wrong with char str 5 strcpy str Hello 16 strncpy Syntax strncpy ptr1 ptr2 num where ptr1 and ptr2 are pointers to char num is the number of characters to be copied Example int len char str1 15 str2 15 strcpy str1 Hello world strncpy str2 str1 5 strncpy Syntax strncpy ptr1 ptr2 num where ptr1 and ptr2 are pointers to char num is the number of characters to be copied Caution strncpy blindly copies the Example characters It does not voluntarily append int len the string terminating null character char str1 15 str2 15 strcpy str1 Hello world strncpy str2 str1 5 17 strcat Syntax strcat ptr1 ptr2 where ptr1 and ptr2 are pointers to char Concatenates the two null terminates strings yielding one string pointed to by ptr1 char S 25 world char D 25 Hello strcat D S strcat Example What s wrong with char S 25 world strcat Hello S 18 strcmp Syntax diff strcmp ptr1 ptr2 where diff is an integer and ptr1 and ptr2 are pointers to char Returns zero if strings are identical int diff char s1 25 pat char s2 25 pet diff strcmp s1 s2 Structures struct employee char name 10 int salary int year month day struct employee john struct employee empPointer empPointer employee malloc sizeof employee john salary 100 empPointer salary 200 19 Type Casting Re interpret a parameter as a different type int age months float exactAge age 11 months 3 exactAge float age exactAge exactAge float months 12 Type Casting Does this example work int age months float exactAge age malloc sizeof int exactAge malloc sizeof float age 11 months 3 exactAge float age exactAge exactAge float months 12 20 PART II Algorithms Main Algorithmic Constructs Assignment Input …


View Full Document

U of I CS 241 - C Crash Course

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download C Crash Course
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Crash Course and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Crash Course 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?