1 / 28

Oddities in Programming Language Constructs (With Focus on C & c++)

Oddities in Programming Language Constructs (With Focus on C & c++). C Examples. How does this code behave?. 1. #include <stdio.h> #include <string.h> main() { char* str1 = "abcdefghi"; char str2[4]; int size = 0; strcpy(str2, str1);

ninon
Download Presentation

Oddities in Programming Language Constructs (With Focus on C & c++)

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. Oddities in Programming Language Constructs (With Focus on C & c++)

  2. C Examples

  3. How does this code behave?

  4. 1 #include <stdio.h> #include <string.h> main() { char* str1 = "abcdefghi"; char str2[4]; int size = 0; strcpy(str2, str1); size = sizeof(str2); printf ("The value of str2 is: %s \n", str2); printf ("The size of str2 is: %d \n", size); return 0; }

  5. The Output 1 The strcpy function copies one string to another but is unable to copy only the characters that fit into the array that it is copying to. The strcpy method has no way of checking that the string pointed to by str1 will actually fit in the array pointed to by str2. Output of the program: %a.out The value of str2 is: abcdefghi The size of str2 is: 4 %

  6. 2 #include <stdio.h> #include <string.h> main() { char p[10]; gets(p); printf ("%s\n", p); return 0; }

  7. The Output 2 The gets function should have read only 9 characters into the array p but instead it read whatever came before the newline character. Output of the program: %a.out I am printing more than 10 characters..lets see how many it gets:) I am printing more than 10 characters..lets see how many it gets:) %

  8. 3 #include <stdio.h> main() { int signed_int = -10; unsigned int unsigned_int = 10; if (signed_int < unsigned_int) printf ("%s\n", "-10 is less than 10"); else printf ("%s\n", "10 is less than -10"); return 0; }

  9. The Output 3 When a signed operand is combined with an unsigned operand, the signed operand is "concerted" to an unsigned value by treating the sign bit as part of the number's magnitude. This can lead to obscure programming errors. In our previous example, you would expect that -10 is less than 10 but the program output shows otherwise!! %a.out 10 is less than –10 %

  10. 4 #include <stdio.h> #include <string.h> main() { char* str1 = "def"; char p[5] = "abc"; strcat(p, str1); printf ("%s\n", p); return 0; }

  11. The Output 4 strcat will attempt to add the chars d, e, f, \0 (stored in str1) to the end of the array - p - which can hold 5 values (a, b, c, \0, \0) implicitly. So now p when printed out prints ‘abcdef’ instead of what it should have printed 'abcd'. Output of the program: %a.out abcdef %

  12. C++ Examples

  13. How does this code behave?

  14. 1 #include <typeinfo.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main () { char c = 'a'; int d = 13; printf("\n%s\n", (typeid (c) == typeid(char) ? "same" : "different")); printf("%s\n", (typeid (c) != typeid(d) ? "different" : "same")); printf("%s\n", (typeid (c + d) == typeid(int) ? "int" : "char")); return 0; }

  15. The Output 1 Output of the program: %a.out same different int %

  16. 2 #include <typeinfo.h> #include <stdio.h> #include <stdlib.h> #include <string.h> class Dbase { private: int fd; public: void write (const char *p) { write(fd, p, strlen(p)); } }; int main () { return 0; }

  17. The Output 2 Instead of overloading the function name “write” as one would expect, this piece of code attempts to perform recursion and complains about arguments passed to it, even though the signatures are different! % CC blocking.cpp "blocking.cpp", line 15: Error: Formal argument p of type const char* in call to Dbase::write(const char*) is being passed int. "blocking.cpp", line 15: Error: Too many arguments in call to "Dbase::write(const char*)". 2 Error(s) detected. %

  18. What are the differences?

  19. 3 int main () { int i = 1; { int j = 2; printf("%d %d\n", i, j); } printf("%d %d\n", i, j); return 0; } int main () { int i = 1; int j = 3; { int j = 2; printf("%d %d\n", i, j); } printf("%d %d\n", i, j); return 0; } int main () { int i = 1; int j = 3; { j = 2; printf("%d %d\n", i, j); } printf("%d %d\n", i, j); return 0; }

  20. The Output 3 The first example will produce a compile error since j is not defined at the point where we attempt to print it. Declaring a variable inside a block defines it only for that block, and it becomes undefined as soon as you leave the block. % CC scope.cpp "scope.cpp", line 15: Error: j is not defined. 1 Error(s) detected. % For the second piece of code, the declaration of j in the inner block masks the initial declaration, but when we exit this block, it is no longer blocked and thus the output is as follows: % a.out 1 2 1 3 % For the third example, we are not declaring any new variable j, so when we reassign j in the inner block, we are simply modifying the j declared globally and the result will be as follows. % a.out 1 2 1 2 %

  21. More Oddities

  22. Pascal Enumeration Oddities EnumType = (one, two, three, forty := 40, fortyone); As a result, the ordinal number of forty is 40, and not 3, as it would be when the ':= 40' wasn’t present. The ordinal value of fortyone is then 41, and not 4, as it would be when the assignment wasn’t present. After an assignment in an enumerated definition the compiler adds 1 to the assigned value to assign to the next enumerated value. When specifying such an enumeration type, it is important to keep in mind that the enumerated elements should be kept in ascending order. The following will produce a compiler error: EnumType = (one, two, three, forty := 40, thirty := 30); It is necessary to keep forty and thirty in the correct order. This is, however, legal in C and C++..

  23. Fine in C & C++ enum fruits { Pears, Plums = 25, Nectarines }; enum vegetables { Carrots, Potatoes = 20, Eggplants = 7, Beans, Sprouts = 8 }; • In Pascal, the following functions exist, thus, Pascal does not allow an enumeration to have a smaller value than its predecessor. Thus, attempting to declare enumerations as above would result in compilation error! • pred(X) yields the predecessor of object X • succ(X) yields the successor of object X • ord(X) yields the position number of object X

  24. In Singular, the post increment operation has no r-value associated with it!! Thus, the assignment j = i++ is meaningless.. Quite an oddity for a programming language ;) int i = 0; int j = 0; j = i++; //WRONG IN SINGULAR!

  25. Brought to you by

  26. Gina Abd-El-Razik & Bhavna Sharma

  27. References C Programming: A Modern Approachby K.N. King Navigating C++ and Object-Oriented Designby Paul Anderson & Gail Anderson

More Related