Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Oddities in Programming Language Constructs (With Focus on C & c++)"— Presentation transcript:

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

2

3 C Examples

4 How does this code behave?

5 #include 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; } 1

6 The Output 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 % 1

7 #include main() { char p[10]; gets(p); printf ("%s\n", p); return 0; } 2

8 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:) % The Output 2

9 #include 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; } 3

10 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 % The Output 3

11 #include main() { char* str1 = "def"; char p[5] = "abc"; strcat(p, str1); printf ("%s\n", p); return 0; } 4

12 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 % The Output 4

13 C++ Examples

14 How does this code behave?

15 #include 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; } 1

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

17 #include class Dbase { private: int fd; public: void write (const char *p) { write(fd, p, strlen(p)); } }; int main () { return 0; } 2

18 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. % 2 The Output

19 What are the differences?

20 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; } 3

21 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 % The Output 3

22 More Oddities

23 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++.. Pascal Enumeration Oddities

24 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

25 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!

26 Brought to you by

27 Gina Abd-El-Razik Bhavna Sharma &

28 C Programming: A Modern Approach by K.N. King References Navigating C++ and Object-Oriented Design by Paul Anderson & Gail Anderson


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

Similar presentations


Ads by Google