CPP BIT FALLS JUST FOR FUN | |
I mention only a limited no of bitfalls that are not available in any books. For more bitfalls please refer Yasvanth Kanithkar's books. |
PROBLEM | ANSWER |
void main() { int a=5; int &b= a; char& c= ' \n' ; char * d= "\n" ; cout << a ++ << c << ++ b << d << a; }
| 6 6 5 // precedence of insertion not defined |
void main() { int i=4;j=7; int k = ( i++ , i+ 2 , ++i , j+2 , i+2); cout<< i<< '\n' << j << '\n\ << k; }
| 6 7 8 // sequence operator ignores all the ng statements except unary |
void main() { int i=4, j=5; if ( ( i=100 ) || ( j=200) && (i=200) { cout<< i <<j; } } | 200 5 // statements are always true |
void main() { int i= 1, j = 2, k = 3, d =0; int a = j = i = k =d+1; cout << j << char (10) << a << '\ n ' << d; } | 1 1 0 // Associative ? |
vois main() { cout<< 0xffffffffffffffffffffff; } | 4294967295 // in my computer |
void main() { int a[] = {1,2,3,4,5}; int *p =a; int * q = "mcmillan "; cout<< sizeof (a ) <<endl<< sizeof (p) << endl <<sizeof (q ); }
| 10 // array name not hold address on size of and references 2 // 16 bit address (may also be 4) 2 |
static union mc{ int y; long double t ; char* o;}; void main() { struct mac {}; char a[] = "mcmillan "; cout << sizeof( mac) << endl << sizeof (a) <<'\n' << sizeof (mc); }
| 1 // Default size of is 1 9 // +1 for null character 10 // depend on long double's size |
void main() { int x = 5, y =6; int * p = &x , *q = &y; cout<< p-q; }
| 1 // always one for proceeding local variables even for any data types |
void main() { int x=10; cout<<x+++x++; }
| 30 // Increment having higher precedence |
void main() { int x=10; if(x++<x)cout<<"1"; else cout<<"2"; }
| 1 // Increment on seeing clone |
void main() { cout<<(7<<1) <<endl<< (7>>1); }
| 14 3 // Just shifting |
void main() { int x=4; if(x++<5 && x++ == 5) cout<<x; else cout<<x; }
| 6 // Increment on seeing clone |
void main() { int x=5; int y=x & y; cout<<y; }
| 4 // undefined y, but result always previous even until 9 |
void main() { int endl = 5; cout<endl<<endl<<endl; }
| 555 // this is also overloading |
void main() { int i = 3 = = 0 = = 0 ; cout<<i; }
| 1 // relational's associative ? |
void main() { enum mac { a, b=-2,c,d ,e = c}; mac m1 = d, m2 = e; cout<< m1 << endl <<m2; }
| 0 // No matter of negative -1 // good, no problem |
void main() { for (static int i = 0 ; i <= 2 ; i++) cout << i; cout << i; }
| 0123 // forget the scope in for loop |
void main() { int a[5] = {0,1,2,3}; cout<< a[5]; }
| 0 // Missed trailer is always zero |
int exchange ( int x, int y) { int t = x; x = y; y =t; return y; } void main ( ) { int x=4, y=5; exchange (x, y); cout << x << endl << y ; } | 4 5 // sorry this is not passed as reference / pointer / address . Just only by value |
void main() { float i =5; if( i <= 5) cout << "1"; else cout << " 2"; }
| 2 // not for ANSI C++ |
TO BE CONTINUED ...>