1 / 32

A Quick Look at C for C++ Programmers

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 >

elmer
Download Presentation

A Quick Look at C for C++ Programmers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Let’s look at some code

  3. 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

  4. 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

  5. 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

  6. 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

  7. \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

  8. Structured Data

  9. 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; }

  10. 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!

  11. 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++

  12. 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++

  13. 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++

  14. 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++

  15. 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

  16. 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?

  17. 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?

  18. 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!

  19. C vs C++ File I/O

  20. 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

  21. More about C Strings

  22. 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

  23. 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; }

  24. 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

  25. 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?

  26. 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?

  27. 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

  28. Memory allocation

  29. 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

  30. 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 */

  31. A Bit of History

  32. 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

More Related