Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Quick Look at C for C++ Programmers Noah Mendelsohn Tufts University Web: COMP.

Similar presentations


Presentation on theme: "A Quick Look at C for C++ Programmers Noah Mendelsohn Tufts University Web: COMP."— Presentation transcript:

1 A Quick Look at C for C++ Programmers Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah COMP 40: Machine Structure and Assembly Language Programming (Fall 2014)

2 © 2010 Noah Mendelsohn Let’s look at some code

3 © 2010 Noah Mendelsohn Hello world compared 3 #include using namespace std; int main(int argc, char *argv[]) { string world = "world"; cout << "Hello " << world << endl; } C++ #include int main(int argc, char *argv[]) { char *world = "world"; printf("Hello %s\n", world); return 0; } C

4 © 2010 Noah Mendelsohn Hello world compared 4 #include using namespace std; int main(int argc, char *argv[]) { string world = "world"; cout << "Hello " << world << endl; } #include int main(int argc, char *argv[]) { char world[] = "world"; printf("Hello %s\n", world); return 0; } C++C No namespaces in C

5 © 2010 Noah Mendelsohn Hello world compared 5 #include using namespace std; int main(int argc, char *argv[]) { string world = "world"; cout << "Hello " << world << endl; } #include int main(int argc, char *argv[]) { char world[] = "world"; printf("Hello %s\n", world); return 0; } C++C C++: stream I/O w/cout C: stdio with stdout, printf, etc.

6 © 2010 Noah Mendelsohn Hello world compared 6 #include using namespace std; int main(int argc, char *argv[]) { string world = "world"; cout << "Hello " << world << endl; } #include int main(int argc, char *argv[]) { char world[] = "world"; printf("Hello %s\n", world); return 0; } C++C Format string allows substitution

7 © 2010 Noah Mendelsohn Hello world compared 7 #include using namespace std; int main(int argc, char *argv[]) { string world = "world"; cout << "Hello " << world << endl; } #include int main(int argc, char *argv[]) { char world[] = "world"; printf("Hello %s\n", world); return 0; } C++C Format strings: %s – string %d – integer (decimal, signed) %u – integer (decimal, unsigned) %ld – integer (decimal, long) %x – integer (base 16 hex) %c – single ASCII character %5d – integer (5 chars wide) Etc, etc.

8 © 2010 Noah Mendelsohn Hello world compared 8 #include using namespace std; int main(int argc, char *argv[]) { string world = "world"; cout << "Hello " << world << endl; } #include int main(int argc, char *argv[]) { char world[] = "world"; printf("Hello %s\n", world); return 0; } C++C \n = new line char \t = tab char \\ = \ char Etc.

9 © 2010 Noah Mendelsohn Basic Datatypes

10 © 2010 Noah Mendelsohn C and C++ mostly share basic data types 10 char a single byte, capable of holding one character in the local character set. int an integer, typically reflecting the natural size of integers on the host machine. short int an integer, possibly smaller than int long int an integer, possibly longer than int long long int an integer, possibly longer than long float single-precision floating point. double double-precision floating point. Abbreviations: “ short ” is same as “ short int ”; “ long ” same as “ long int ” Examples: int x; short int s; short s; double gpa;

11 © 2010 Noah Mendelsohn Pointers 11 char c; /* a single byte character */ char *cp; /* a pointer to a single byte character */ A pointer variable holds a reference to some other variable.

12 © 2010 Noah Mendelsohn What does this code do? 12 int x; /* variable x holds an integer */ int y; /* variable y holds an integer */ int z; /* variable z holds an integer */ int *iptr; /* variable iptr holds pointer to an integer */ x = 2; y = 3; ip = &z; *ip = x + y; printf(“First answer is %d\n”, z); If you’re not sure, try this code yourself. Try changing it!

13 © 2010 Noah Mendelsohn What does this code do? 13 int x; /* variable x holds an integer */ int y; /* variable y holds an integer */ int z; /* variable z holds an integer */ int *ip; /* variable ip holds pointer to an integer */ x = 2; y = 3; ip = &z; *ip = x + y; printf(“First answer is %d\n”, z); *ip = *ip + z; printf(“Seconed answer is %d\n”, z); If you’re not sure, try this code yourself. Try changing it!

14 © 2010 Noah Mendelsohn Structured Data

15 © 2010 Noah Mendelsohn Some structured data 15 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned int i; (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++

16 © 2010 Noah Mendelsohn Some structured data 16 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[3] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned int i; (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; }

17 © 2010 Noah Mendelsohn Some structured data 17 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[3] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned int i; (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 structs have data only…no methods!

18 © 2010 Noah Mendelsohn Some structured data 18 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[3] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned int i; (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; } Unlike C++: keyword struct required when naming a structured type

19 © 2010 Noah Mendelsohn Some structured data 19 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[3] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned int i; (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++

20 © 2010 Noah Mendelsohn Some structured data 20 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned int i; (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++

21 © 2010 Noah Mendelsohn You saw printf and format strings earlier! 21 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned int i; (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 and fprintf format specifications

22 © 2010 Noah Mendelsohn Good Practice: Single Point of Truth

23 © 2010 Noah Mendelsohn Single point of truth 23 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned int i; (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?

24 © 2010 Noah Mendelsohn Single point of truth 24 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, {“albert", 22}, } ; unsigned int i; (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?

25 © 2010 Noah Mendelsohn Single point of truth 25 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, {“albert", 22}, } ; unsigned int i; (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 have single 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!

26 © 2010 Noah Mendelsohn …BTW: this is the same as C++ 26 #include int main(int argc, char *argv[]) { struct student { char *name; int age; }; struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, } ; unsigned int i; (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++

27 © 2010 Noah Mendelsohn Why we use Hanson in COMP 40

28 © 2010 Noah Mendelsohn What about abstract data types like list, or table?  Many modern languages have them built into the standard –Python, Ruby, etc., etc. –C++ Standard Template Library (STL)  C does not standardize implementation of types like these  Hanson gives us C based ADT implementations that: –Are useful in our programs –Teach us what’s going on in many of those higher level language implementations! –Teach us many good techniques for building modular structures in C 28

29 © 2010 Noah Mendelsohn C vs C++ File I/O

30 © 2010 Noah Mendelsohn Access to files and input FeatureCC++ Pre-opened streams stdin, stdout, stderr cin, cout, cerr Open a file FILE *fp fopen(filename,”r”) ifstream myfile(“filename”); Typical use fprintf(stderr, “error\n”); fprintf(fp,”Hi!”); cerr << “error” << endl; myfile << “Hi!”; 30 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

31 © 2010 Noah Mendelsohn More about C Strings

32 © 2010 Noah Mendelsohn C characters 32 #include int main(int argc, char *argv[]) { (void)argc; (void)argv; unsigned char var1 = 'N'; unsigned char var2 = 'O'; unsigned char var3 = 'A'; unsigned char var4 = 'H'; /* %c prints as character %u prints unsigned integer */ printf("The number for %c is %u\n", var1, var1); printf("The number for %c is %u\n", var2, var2); printf("The number for %c is %u\n", var3, var3); printf("The number for %c is %u\n", var4, var4); exit(EXIT_SUCCESS); } Printing each one twice… …in different formats!

33 © 2010 Noah Mendelsohn C characters 33 #include int main(int argc, char *argv[]) { (void)argc; (void)argv; unsigned char var1 = 'N'; unsigned char var2 = 'O'; unsigned char var3 = 'A'; unsigned char var4 = 'H'; /* %c prints as character %u prints unsigned integer */ printf("The number for %c is %u\n", var1, var1); printf("The number for %c is %u\n", var2, var2); printf("The number for %c is %u\n", var3, var3); printf("The number for %c is %u\n", var4, var4); exit(EXIT_SUCCESS); } This program prints: The number for N is 78 The number for O is 79 The number for A is 65 The number for H is 72

34 © 2010 Noah Mendelsohn C characters are integers! 34 #include int main(int argc, char *argv[]) { (void)argc; (void)argv; unsigned char var1 = 'N'; unsigned char var2 = 'O'; unsigned char var3 = 'A'; unsigned char var4 = 'H'; /* %c prints as character %u prints unsigned integer */ printf("The number for %c is %u\n", var1, var1); printf("The number for %c is %u\n", var2, var2); printf("The number for %c is %u\n", var3, var3); printf("The number for %c is %u\n", var4, var4); exit(EXIT_SUCCESS); } This program prints: The number for N is 78 The number for O is 79 The number for A is 65 The number for H is 72 Interesting… Characters are also numbers!

35 © 2010 Noah Mendelsohn Hello world compared 35 C++: string type #include using namespace std; int main(int argc, char *argv[]) { string world = "world"; cout << "Hello " << world << endl; } C: arrays of characters #include int main(int argc, char *argv[]) { char world[] = "world"; printf("Hello %s\n", world); return 0; } C++C

36 © 2010 Noah Mendelsohn Our first hello world 36 #include int main(int argc, char *argv[]) { char world[] = "world"; printf("Hello %s\n", world); return 0; } String literal gives array of characters

37 © 2010 Noah Mendelsohn C arrays addressed by pointer to first element 37 #include int main(int argc, char *argv[]) { char *world = "world"; char universe[] = “universe”; printf("Hello %s\n", world); printf("Hello %s\n", universe); return 0; } These do almost the same thing The relationship between arrays and pointers is subtle & important! This one you need to research using K&R or Harbison & Steele

38 © 2010 Noah Mendelsohn A trickier hello 38 #include int main(int argc, 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?

39 © 2010 Noah Mendelsohn If you understand this, you’re well on your way! 39 #include int main(int argc, 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?

40 © 2010 Noah Mendelsohn If you understand this, you’re well on your way! 40 #include int main(int argc, 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

41 © 2010 Noah Mendelsohn Memory allocation

42 © 2010 Noah Mendelsohn 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 42

43 © 2010 Noah Mendelsohn 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(sizeof struct 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 */ 43

44 © 2010 Noah Mendelsohn A Bit of History

45 © 2010 Noah Mendelsohn 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 45


Download ppt "A Quick Look at C for C++ Programmers Noah Mendelsohn Tufts University Web: COMP."

Similar presentations


Ads by Google