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

Post on 12-Jan-2016

224 views 1 download

Transcript of 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?

#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

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

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

main(){

char p[10];gets(p);

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

return 0;}

2

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

#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

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

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

main(){

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

strcat(p, str1);

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

return 0;}

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

%

The Output4

C++ Examples

How does this code behave?

#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

The OutputOutput of the program:

%a.outsamedifferentint

%

1

#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

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

What are the differences?

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

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

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

Pascal Enumeration Oddities

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

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!

Brought to you by

Gina Abd-El-Razik

Bhavna Sharma

&

C Programming: A Modern Approachby K.N. King

References

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