13 Jo P Jan 08


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

13 Jo P Jan 08

  1. 1. The Joy of Programming Some Interesting Programming Puzzles S.G. GANESH Every experienced C programmer knows that if the code looks simple, straightforward and perfectly fine, it doesn’t necessarily mean that it would work fine and not dump-core at runtime! In this month’s column, we’ll look at a few such interesting programming problems. 1) This program had an assertion failure. Why? Avoid looking at the answers immediately; instead, try to struct bitfield { solve them first. signed int b : 1; Solution 1: A signed bit-field size of 1 can store only one } bit; bit, and that value becomes a sign bit. For storing the value 1, the only bit available is used, which is the sign bit. So, 1 int main() { becomes -1 for a bit-field member of size 1. If you use bit.b = 1; “printf(“%d”, bit.b)”, you would see the output -1. assert(bit.b == 1); Solution 2: This condition fails when i is INT_MIN. } Remember that the range of an integer is 2(n-1) to 2(n-1)-1, where n is the size of the integer. INT_MIN is the least possible value 2) This function worked fine for many years and of integral negative numbers and when we negate it, as in -i, it suddenly, for some value of i, the assertion failed. What was is not in the range of positive integers. So, it ‘overflows’ and that value of i for which it failed and why? becomes negative and INT_MIN again! So the assertion fails. int my_abs(int i) { Solution 3: For NaN (Not a Number), the condition (d == if (i < 0) { d) is not true! The IEEE 754 floating point arithmetic standard i = -i; has a special number called NaN for representing invalid } values like (0.0/0.0) instead of just throwing an error/exception assert(i >= 0); like Divide-by-Zero. To detect this special number return i; programmatically, the special condition of (NaN != NaN) is } provided. Solution 4: The prototype for the sqrt library function is 3) The following function was written with an intention missing in this program. For those functions that are not that the printf should never be executed. But for some value declared (but used), the compiler assumes the return type to of d, the message got printed! What is that value of ‘d’? be int. Since <math.h> is not included in this program (which void oops(double d) { has the prototype for sqrt), sqrt is assumed to return int; the if(d != d) actual double value that is returned from sqrt(2) is converted printf(“Oops! Something went wrong”); to an int value and assigned to x after casting it to double. } This results in the wrong output. The solution is to either use #include <math.h> or provide the prototype of sqrt before 4) The following program, when run, gave the output: its use. sqrt of 2=1557547102.000000 instead of: sqrt of 2=1.414214. Solution 5: The character ‘hi’ is a multi-byte character, What could be the problem? and the programmer has typed ‘hi’ instead of “hi” (typing ‘, int main() { instead of “ , by mistake). The printf function tries to double x = sqrt(2); interpret msg as if it is pointing to a string literal, and reads printf(quot;sqrt of 2=%lfquot;, x); that address, which is grossly incorrect; hence the program } core-dumped... That’s it for now. And here’s wishing you a happy 5) This program core-dumped at runtime instead of new year! printing “hi”; can you find out why? int main() { S G Ganesh is a research engineer in Siemens (Corporate Technology). His latest book is “60 Tips on Object Oriented char * msg = ‘hi’; Programming”, published by Tata-McGraw Hill in printf(msg); December, 2007. You can reach him at } sgganesh@gmail.com. www.linuxforu.com | LINUX FOR YOU | JANUARY 2008 127 CMYK