320 likes | 470 Views
COMP 40: Machine Structure and Assembly Language Programming (Fall 2014 ). A Quick Look at C for C++ Programmers. Noah Mendelsohn Tufts University Email: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah. Let’s look at some code. Hello world compared. #include < iostream >
E N D
COMP 40: Machine Structure and Assembly Language Programming (Fall 2014) A Quick Look at CforC++ Programmers Noah Mendelsohn Tufts UniversityEmail: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah
Hello world compared #include <iostream> #include <string> using namespace std; int main(intargc, char *argv[]) { string world = "world"; cout<< "Hello " << world << endl; } #include <stdio.h> int main(intargc, char *argv[]) { char *world = "world"; printf("Hello %s\n", world); return 0; } C++ C
No namespaces in C Hello world compared #include <iostream> #include <string> using namespace std; int main(intargc, char *argv[]) { string world = "world"; cout<< "Hello " << world << endl; } #include <stdio.h> int main(intargc, char *argv[]) { char *world = "world"; printf("Hello %s\n", world); return 0; } C++ C
C++: stream I/O w/coutC: stdio with stdout, printf, etc. Hello world compared #include <iostream> #include <string> using namespace std; int main(intargc, char *argv[]) { string world = "world"; cout<< "Hello " << world << endl; } #include <stdio.h> int main(intargc, char *argv[]) { char *world = "world"; printf("Hello %s\n", world); return 0; } C++ C
Format string allows substitution Hello world compared #include <iostream> #include <string> using namespace std; int main(intargc, char *argv[]) { string world = "world"; cout<< "Hello " << world << endl; } #include <stdio.h> int main(intargc, char *argv[]) { char *world = "world"; printf("Hello %s\n", world); return 0; } C++ C
\n = new line char\t = tab char \\= \ char Etc. Hello world compared #include <iostream> #include <string> using namespace std; int main(intargc, char *argv[]) { string world = "world"; cout<< "Hello " << world << endl; } #include <stdio.h> int main(intargc, char *argv[]) { char *world = "world"; printf("Hello %s\n", world); return 0; } C++ C
Some structured data #include <stdio.h> int main(intargc, char *argv[]) { struct student { char *name; int age; }; structstudent students[3] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned inti; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n", students[i].name, students[i].age); }; return 0; }
C has structs,not classes Some structured data #include <stdio.h> int main(intargc, char *argv[]) { struct student { char *name; int age; }; struct student students[3] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned inti; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n", students[i].name, students[i].age); }; return 0; } structs have data only…no methods!
Some structured data #include <stdio.h> int main(intargc, char *argv[]) { struct student { char *name; int age; }; struct student students[3] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned inti; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n", students[i].name, students[i].age); }; return 0; } Initializers more or less the same as C++
Some structured data #include <stdio.h> int main(intargc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned inti; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n", students[i].name, students[i].age); }; return 0; } We can leave out array bound if initializer determines the size – same as C++
Some structured data #include <stdio.h> int main(intargc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned inti; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n", students[i].name, students[i].age); }; return 0; } Primitive types mostly the same as C++
Some structured data #include <stdio.h> int main(intargc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned inti; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n", students[i].name, students[i].age); }; return 0; } • Suppress compiler warning about unused arguments • --same as C++
Some structured data #include <stdio.h> int main(intargc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned inti; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n", students[i].name, students[i].age); }; return 0; } You will want to learn about printf andfprintfformat specifications
Single point of truth #include <stdio.h> int main(intargc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned inti; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n", students[i].name, students[i].age); }; return 0; } What’s going on here?
Single point of truth #include <stdio.h> int main(intargc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, {“albert", 22}, } ; unsigned inti; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n", students[i].name, students[i].age); }; return 0; } What if number of students changes?
Single point of truth #include <stdio.h> int main(intargc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, {“albert", 22}, } ; unsigned inti; (void)argc; (void)argv; for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n", students[i].name, students[i].age); }; return 0; } There is a single point of truth for the number of students Try to havesingle points of truth for anything in your program that’s likely to change, or on which multiple other things depend…if we change this structure, everything just works!
Access to files and input In both languages: • The operating system pre-opens the three standard streams • They can be redirected from the command line: • myprog < somefile # stdin reads from somefile • myprog> somefile # stdout writes to somefile • otherprog | myprog # stdin is output of otherprog
C++: string typeC: arrays of characters Hello world compared #include <iostream> #include <string> using namespace std; int main(intargc, char *argv[]) { string world = "world"; cout<< "Hello " << world << endl; } #include <stdio.h> int main(intargc, char *argv[]) { char *world = "world"; printf("Hello %s\n", world); return 0; } C++ C
Our first hello world Huh?How come this is a pointer? C: arrays of characters #include <stdio.h> int main(intargc, char *argv[]) { char *world = "world"; printf("Hello %s\n", world); return 0; }
These do almost the same thing C arrays addressed by pointer to first element #include <stdio.h> int main(intargc, char *argv[]) { char *world = "world"; char universe[] = “universe”; printf("Hello %s\n", world); printf("Hello %s\n", universe); return 0; } The relationship between arrays and pointers is subtle & important! This one you need to research using K&R or Harbison & Steele
A trickier hello #include <stdio.h> #include <string.h> int main(intargc, char *argv[]) { char world[] = "world"; printf("Hello %s\n", world); world[1] = '\0'; printf("Hello %s your string is %d bytes long!\n", world, strlen(world)); return 0; } What does this print?
If you understand this, you’re well on your way! #include <stdio.h> #include <string.h> int main(intargc, char *argv[]) { char world[] = "world"; printf("Hello %s\n", world); world[1] = '\0'; printf("Hello %s your string is %d bytes long!\n", world, strlen(world)); world[3] = 'm'; printf("Hello %s your string is %d bytes long!\n", world, strlen(world)); world[1] = 'o'; world[4] = '\0'; printf("Hello %s your string is %d bytes long!\n", world, strlen(world)); return 0; } What does this print?
If you understand this, you’re well on your way! #include <stdio.h> #include <string.h> int main(intargc, char *argv[]) { char *world = "world"; printf("Hello %s\n", world); world[1] = '\0'; printf("Hello %s your string is %d bytes long!\n", world, strlen(world)); world[3] = 'm'; printf("Hello %s your string is %d bytes long!\n", world, strlen(world)); world[1] = 'o'; world[4] = '\0'; printf("Hello %s your string is %d bytes long!\n", world, strlen(world)); return 0; } These examples show that:1) The logical length of a C string is determined by the terminating null character ‘\0’2) Printing using %s and checking length with strlen() respect this 3)In a correct program, there should be at least enough space for the string, but you may have allocated more 4) In buggy programs, you fail to null terminate or index off the end of the allocated space
There is no new in C! • C++ new builds objects: • Car *myCar = new Car(V8, Blue); // Create a new car • The above allocates space and initializes all the data for car • delete myCar; • Runs destructors and releases memory • Also: int *ip = new int[len]; // allocate array delete[] ip; // delete array • Also: std:vector // truly dynamic array
There is no new in C! • C++ new builds objects: • Car *myCar = new Car(V8, Blue); // Create a new car • The above allocates space and initializes all the data for car • delete myCar; • Runs destructors and releases memory • Also: int *ip = new int[len]; // allocate array delete[] ip; // delete array • Also: std:vector // truly dynamic array • C malloc/free allocate and free bytes: • struct car { ….members here… };struct car *carp = malloc(sizeofstruct car); • allocate unitialized bytes • struct car *carp = malloc(sizeof *carp); • Same, but keeps working if structure name changes • You should check the return value to make sure it worked! • free(carp); /* frees the bytes */
C++ is an extension to C • C was invented many years earlier • C is a quite small language; C++ is much more complicated • Most of C survives in C++ • Primitive data types: int, float, double, etc • Control structures (if, while, function call) • Source structures (#include and preprocessor) • Much more • C does not have: • Classes, methods, namespaces, dynamic arrays, dynamic strings, stream I/O, inheritance, built in higher level ADTs (list, vector, deque, table, etc.) • C is much closer to the hardware • Good programmers know how each C construct compiles • No single C expression compiles to huge amounts of code