oddities in programming language constructs (with focus on c & c++)

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

Upload: tabitha-craig

Post on 12-Jan-2016

226 views

Category:

Documents


1 download

TRANSCRIPT

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

Oddities in Programming

Language Constructs

(With Focus on C & c++)

Page 2: Oddities in Programming Language Constructs (With Focus on C & c++)
Page 3: Oddities in Programming Language Constructs (With Focus on C & c++)

C Examples

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

How does this code behave?

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

#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;}

1

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

The OutputThe 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

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

#include <stdio.h>#include <string.h>

main(){

char p[10];gets(p);

printf ("%s\n", p);

return 0;}

2

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

The gets function should have read only 9 characters into thearray p but instead it read whatever came before the newline character.

Output of the program:

%a.outI 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:)%

The Output2

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

#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");

elseprintf ("%s\n", "10 is less than -10");

return 0;}

3

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

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.out10 is less than –10%

The Output3

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

#include <stdio.h>#include <string.h>

main(){

char* str1 = "def";char p[5] = "abc";

strcat(p, str1);

printf ("%s\n", p);

return 0;}

4

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

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 Output4

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

C++ Examples

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

How does this code behave?

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

#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;}

1

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

The OutputOutput of the program:

%a.outsamedifferentint

%

1

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

#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;}

2

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

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

2The Output

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

What are the differences?

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

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

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

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.out1 21 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.out1 21 2%

The Output3

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

More Oddities

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

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

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

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

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

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!

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

Brought to you by

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

Gina Abd-El-Razik

Bhavna Sharma

&

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

C Programming: A Modern Approachby K.N. King

References

Navigating C++ and Object-Oriented Designby Paul Anderson & Gail Anderson