DOC PREVIEW
Columbia COMS 3156 - Recitation 9

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 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 17 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 17 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 17 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 17 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 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Software EngineeringRecitation 9Suhit GuptaTodayn CHmmm…n This is a hard subjectn I cannot finish this in one (one-hour) lecture.n If I do then –– I am a genius– I did a crappy job– Both J2Intron Invented by K&R (Brian Kernighan and Dennis Ritchie)– ...C deals with the same sort of objects that most computers do, namely characters, numbers, and addresses.n Invented in the 60’sWhy learn C (after Java)?n Both high-level and low-level languagen Better control of low-level mechanismsn Performance better than Java (Unix, 2000/XP!)n Java hides many details needed for writing OS coden Memory management responsibilityn Explicit initialization and error detectionn More room for mistakesWhat does this C program do ?#include <stdio.h>struct list{int data; struct list *next};struct list *start, *end;void add(struct list *head, struct list *list, int data};int delete(struct list *head, struct list *tail);void main(void){start=end=NULL;add(start, end, 2); add(start, end, 3);printf(“First element: %d”, delete(start, end));}void add(struct list *head, struct list *tail, int data}{if(tail==NULL){head=tail=malloc(sizeof(struct list));head->data=data; head->next=NULL;}else{tail->next= malloc(sizeof(struct list));tail=tail->next; tail->data=data; tail->next=NULL;}}3void delete (struct list *head, struct list *tail){struct list *temp;if(head==tail){free(head); head=tail=NULL;}else{temp=head->next; free(head); head=temp;}}Goals of this tutorialn To introduce some basic C concepts to you– so that you can read further details on your ownn To warn you about common mistakes made by beginners– so that you get your homework done quicklyn You will be able to understand the earlier complicated program completely !– And write more complicated code#include <stdio.h>void main(void){printf(“Hello World. \n \t and you ! \n ”);/* print out a message */return;}$Hello World.and you !$Simple Example4Summarizing the Examplen #include <stdio.h> = include header file stdio.h– No semicolon at end – Small letters only – C is case-sensitiven void main(void){ … } is the only code executedn printf(“ /* message you want printed */ ”);n \n = newline \t = tabn \ in front of other special characters within printf.– printf(“Have you heard of \”The Rock\” ? \n”);• int 4 -2,147,483,648 to 2,147,483,647 %d• char 1 -128 to 127 %c• float 4 3.4E+/-38 (7 digits) %f• double 8 1.7E+/-308 (15 digits long) %lf• long 4 -2,147,483,648 to 2,147,483,647 %l• short 2 -32,768 to 32,767• Lookup: • signed / unsigned - int, char, long, short• long double• ex: int num=20000;printf(“Cornell has about %d students.\n”, num);Simple Data Types#include <stdio.h>void main(void){int nstudents = 0; /* Initialization, required */printf(“How many students does Columbia have ?:”);scanf (“%d”, &nstudents); /* Read input */printf(“Columbia has %d students.\n”, nstudents); return ;}$How many students does Columbia have ?: 20000 (enter)Columbia has 20000 students.$Example5#include <stdio.h>void main(void){int i,j = 12; /* i not initialized, only j */float f1,f2 = 1.2;i = (int) f2; /* explicit: i <- 1, 0.2 lost */f1 = i; /* implicit: f1 <- 1.0 */f1 = f2 + (int) j; /* explicit: f1 <- 1.2 + 12.0 */f1 = f2 + j; /* implicit: f1 <- 1.2 + 12.0 */}• Explicit conversion rules for arithmetic operation x=y+z;• convert y or z as • double <- float <- int <- char, short• then type cast it to x ’s type• Moral: stick with explicit conversions - no confusion !Type conversion• Operators same as Java: • Arithmetic• int i = i+1; i++; i--; i *= 2;• +, -, *, /, %,• Relational and Logical• <, >, <=, >=, ==, !=• &&, ||, &, |, !• Syntax same as in Java:• if ( ) { } else { }• while ( ) { }• do { } while ( );• for(i=1; i <= 100; i++) { }• switch ( ) {case 1: … }• continue; break;Like Java, Like C#include <stdio.h>#define DANGERLEVEL 5 /* C Preprocessor -- substitution on appearance *//* like Java ‘final’ */void main(void){float level=1;/* if-then-else as in Java */if (level <= DANGERLEVEL){ /*replaced by 5*/printf(“Low on gas!\n”);}else printf(“Good driver !\n”);return;}Example6#include <stdio.h>void main(void){int number[12]; /* 12 cells, one cell per student */int index, sum = 0;/* Always initialize array before use */for (index = 0; index < 12; index++) {number[index] = index; }/* now, number[index]=index; will cause error:why ?*/for (index = 0; index < 12; index = index + 1) {sum += number[index]; /* sum array elements */}return;}One-Dimensional ArraysMore arraysn Stringschar name[6];name = {‘C’,’S’,’4’,’1’,’4’,’\0’}; /* ’\0’= end of string */printf(“%s”, name); /* print until ‘\0’ */– Functions to operate on strings• strcpy, strncpy, strcmp, strncmp, strcat, strncat, strstr, strchr• #include <strings.h> at program startn Multi-dimensional arraysint points[3][4];points [1][3] = 12; /* NOT points[3,4] */printf(“%d”, points[1][3]);Like Java, somewhat like Cn Type conversions– but you can typecast from any type to any type• c = (char) some_int;– So be careful !n Arrays– Always initialize before use– int number[12];printf(“%d”, number[20]);• produces undefined output, may terminate, may not even be detected.n Strings are terminated by ’\0’ characterchar name[6] = {‘C’,’S’,’4’,’1’,’4’,’\0’}; /* ’\0’= end of string */printf(“%s”, name); /* print until ‘\0’ */75 10 12.5 9. 8 c dint x = 5, y = 10;float f = 12.5, g = 9.8;char c = ‘c’, d = ‘d’;4300 4304 4308 4312 4316 4317Memory layout and addresses• Pointer = variable containing address of another variablefloat f; /* data variable */float *f_addr; /* pointer variable */f_addr = &f; /* & = address operator */? ?ff_addr4300 4304?any floatany address? 4300ff_addr4300 4304Pointers made easy - 1*f_addr = 3.2; /* indirection operator */float g=*f_addr; /* indirection:g is now 3.2 */f = 1.3;ff_addr4300 43043.2 4300ff_addr4300 43041.3 4300Pointers made easy - 28#include <stdio.h>void main(void) {int j;int *ptr;ptr=&j; /* initialize ptr before using it *//* *ptr=4 does NOT initialize ptr */*ptr=4; /* j <- 4 */j=*ptr; /* j <- ??? */}Pointers Example#include <stdio.h>void main(void) {int *ptr;/* allocate space to hold an int */ptr = malloc(sizeof(int)); /* do stuff with


View Full Document

Columbia COMS 3156 - Recitation 9

Download Recitation 9
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 Recitation 9 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 Recitation 9 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?