#include using namespace std; void fun( int num ) { cout << num << endl; } /* // Warns, but goes through int returnTypeCheck() { double local = 5.8; return local; } // No warning, works fine double returnTypeCheck() { int local = 5; return local; } // No warning, works fine char returnTypeCheck() { int local = 5; return local; } // Works fine bool returnTypeCheck() { double local = 5.8; return local; } // Warns, but runs char returnTypeCheck() { double local = 100.8; return local; } */ // No problem char returnTypeCheck() { bool local = true; return local; } int main() { /* // Testing prefix expressions and where they can be used int count = 5; -- ++ count; cout << "After incrementing twice " << count << endl; ++ count += 3; cout << "After compound assignment " << count << endl; ++ count = 2; cout << "After simple assignment " << count << endl; (++ count) ++; cout << "After parenthesized pre and post assignment " << count << endl; (count) = 5; */ /* // Testing postfix operation on boolean variables bool count = false; cout << count++ << endl; cout << count++ << endl; cout << count++ << endl; cout << count++ << endl; */ // Testing whether assignment is possible between different data types /* double var; var = 'a'; // Can assign char to double cout << var << endl; var = true; // Can assign boolean to double cout << var << endl; char cvar; cvar = true; cout << cvar << endl; cvar = 97; cout << cvar << endl; // Can assign positive numbers to char cvar = -97; cout << cvar << endl; // Assigning negative numbers is allowed, but causes semantic error - prints blank cvar = 97.5; // Warns, but assigns 'a' cout << cvar << endl; // All assignments to boolean variable are ok! bool bvar; bvar = 5; cout << bvar << endl; bvar = -5; cout << bvar << endl; bvar = 13.2; cout << bvar << endl; bvar = 'a'; cout << bvar << endl; */ /* unsigned uvar; uvar = -5; cout << uvar << endl; // Warns, compiles, generates semantic error */ /* // Testing type compatibilities for compound assignment operator unsigned uvar = 10; uvar += -50; cout << uvar << endl; // Generates semantic error double var = 2.0; var += 4; cout << var << endl; var += 'a'; // Can assign char to double cout << var << endl; var += true; // Can assign boolean to double cout << var << endl; char cvar = 'a'; cvar += 97; cout << cvar << endl; // A with carat cvar += -970; cout << cvar << endl; // lower case phi printed cvar += 97.5; cout << cvar << endl; // A with carat cvar += true; cout << cvar << endl; // A with tilde // All assignments to boolean variable are ok! bool bvar = true; bvar += 5; cout << bvar << endl; // 1 bvar += -5; cout << bvar << endl; // 1 bvar += 13.2; cout << bvar << endl; // 1 bvar += 'a'; cout << bvar << endl; // 1 */ /* // Testing the effect of break statement not in loop/switch // Answer - Compilation error if break not in loop or switch // So, we cannot just ignore it int index = 5; if( index <= 10 ) break; else index++; cout << index; */ /* // Testing bitwise operators cout << (3 & 5) << endl; // 1 cout << (3 | 5) << endl; // 7 cout << (3 ^ 5) << endl; // 6 cout << (~ 3 ) << endl; // -4 cout << "----------------------" << endl; cout << (3 << 14) << endl; // 49152 in C++ cout << (3 << 2) << endl; // 12 cout << (3 << -2) << endl; // -1073741824 cout << (3 << 200) << endl; // 0 in C++, 768 in C#, Java cout << (3 << -200) << endl; // 50331648 in C++, Java, C# cout << (3 << 0) << endl; // 3 in C++ cout << (3 << -1) << endl; // -2147483648 in C++ cout << (3 << -2) << endl; // -1073741824 in C++ cout << (3 << -4) << endl; // 805306368 in C++ cout << (3 << -8) << endl; // 50331648 in C++ cout << (3 << -16) << endl; // 196608 in C++ cout << (3 << -18) << endl; // 49152 in C++ cout << (3 << -24) << endl; // 768 in C++ cout << (3 << -30) << endl; // 12 in C++ cout << (3 << -62) << endl; // 12 in C++ cout << (3 << -94) << endl; // 12 in C++ cout << (3 << -126) << endl; // 12 in C++ cout << (3 << -32) << endl; // 3 in C++ cout << (3 << -34) << endl; // -1073741824 in C++ cout << (3 << -48) << endl; // same as -16 C++ cout << (3 << -50) << endl; // same as -18 C++ cout << (3 << -82) << endl; // same as -18 C++ cout << (3 << -2147483648) << endl; // 3 in C++ cout << "----------------------" << endl; cout << (1 << -1) << endl; // -2147483648 in C++ cout << (1 << -2) << endl; // 1073741824 in C++ cout << (1 << -4) << endl; // 268435456 in C++ cout << (1 << -8) << endl; // 16777216 in C++ cout << (1 << -16) << endl; // 65536 in C++ cout << (1 << -24) << endl; // 256 in C++ cout << (1 << -30) << endl; // 4 in C++ cout << (1 << -32) << endl; // 1 in C++ cout << (1 << -64) << endl; // 1 in C++ cout << (1 << -128) << endl; // 1 in C++ cout << (1 << -256) << endl; // 1 in C++ cout << (1 << -2147483648) << endl; // 1 in C++ cout << "----------------------" << endl; cout << (16 >> 2) << endl; // 4 cout << (-16 >> 2) << endl; // -4 cout << (3 >> -2) << endl; // 0 cout << (768 >> 200) << endl; // 0 in C++, 3 in C#, Java cout << (-768 >> 200) << endl; // -1 in C++, -3 in C#, Java cout << (-10 >> 200) << endl; // -1 in C++ cout << "----------------------" << endl; */ /* // Bit wise operators int number, index; // number << (index % 32) is returned cout << "------------Left Shift Operator, Positive Left, Positive right operand---------" << endl; number = 768; for( index = 0; index < 100; index++ ) cout << "(" << number << " << " << index << " = " << (number << index) << endl; // number << (index % 32) + 32 is returned cout << "------------Left Shift Operator, Positive Left, Negative right operand---------" << endl; number = 768; for( index = 0; index > -100; index-- ) cout << "(" << number << " << " << index << " = " << (number << index) << endl; // number << (index % 32) is returned cout << "------------Left Shift Operator, Negative Left, Positive right operand---------" << endl; number = -768; for( index = 0; index < 100; index++ ) cout << "(" << number << " << " << index << " = " << (number << index) << endl; // number << (index % 32) + 32 is returned cout << "------------Left Shift Operator, Negative Left, Negative right operand---------" << endl; number = -768; for( index = 0; index > -100; index-- ) cout << "(" << number << " << " << index << " = " << (number << index) << endl; // number >> (index % 32) is returned cout << "------------Right Shift Operator, Positive Left, Positive Right operand---------" << endl; number = 768; for( index = 0; index < 100; index++ ) cout << "(" << number << " >> " << index << " = " << (number >> index) << endl; // number >> (index % 32) + 32 is returned cout << "------------Right Shift Operator, Positive Left, Negative right operand---------" << endl; number = 768; for( index = 0; index > -100; index-- ) cout << "(" << number << " >> " << index << " = " << (number >> index) << endl; // number >> (index % 32) is returned cout << "------------Right Shift Operator, Negative Left, Positive right operand---------" << endl; number = -768; for( index = 0; index < 100; index++ ) cout << "(" << number << " >> " << index << " = " << (number >> index) << endl; // number >> (index % 32) + 32 is returned cout << "------------Right Shift Operator, Negative Left, Negative right operand---------" << endl; number = -768; for( index = 0; index > -100; index-- ) cout << "(" << number << " >> " << index << " = " << (number >> index) << endl; */ /* // Testing reference variables int var = 5, var2 = 20; int & refVar = var; // Only variable pointing allowed refVar++; // OK, prints 6 refVar = 13; // OK, prints 13 refVar = var2; // OK, prints 20, because it assigns the value of var2 to var cout << refVar << endl; */ /* double num = 5.2; fun( 5.2 ); // Warns, but executes */ /* // Checking compatibility of return types and return value types cout << returnTypeCheck() << endl; */ /* int index; // Checking whether empty condition is allowed // for loop - WORKS - infinite iteration for( index = 0; ; index++ ) cout << index << endl; // Syntax error - expression needed as condition while() { cout << index << endl; index++; } // Syntax error - expression needed as condition do { cout << index << endl; index++; } while(); // for loop - infinite loop, always prints 5 for( index = 0; index = 5 ; index++ ) cout << index << endl; // while - infinite loop, always prints 5 while( index = 5 ) { cout << index << endl; index++; } // do-while - infinite loop, always prints 5 do { cout << index << endl; index++; } while( index = 5 ); index = 5; // Illegal if( index > 0 ) continue; // Illegal if( index < 10 ) break; // What happens if we declare a variable in condition? - WORKS while( bool flag = true ) { cout << "Testing" << endl; } // What happens if we declare a variable in condition? - SYNTAX ERROR! do { cout << "Testing" << endl; } while( bool flag = true ); */ /* // What happens if we declare a variable in condition? - WORKS for( int index = 0; int count = 5; index++ ) cout << "Testing" << endl; */ /* // Can we initialize a 2D array with 1D list? - Yes, and it works - prints 5! int array[2][3] = {1, 2, 3, 4, 5, 6}; cout << array[1][1]; */ /* // Can we initialize a variable with an earlier variable? - Yes, CAN DO int index = 5; int count = index * index; */ /* // Is boolean constant case-sensitive? - YES bool flag = true; // C++ allows 'and' and 'or' operators - YES bool answer = not flag and false; // OK bool result = true||!false; // OK */ /* // Spacing in expressions int x=5; int y=3; int * z; // cout << x+-y << endl; // OK // cout << x + +; // NOT OK // cout << x++y << endl; // NOT OK // cout << "x+++y is " << x+++y << endl; // OK it is x++ + y, i.e., 8 // cout << x++-y << endl; // OK // cout << x-++y << endl; // OK // cout << x + + y << endl; // OK // x &&= y; // Not OK // x*=-y; // OK // cout << -++x << endl; // OK! // cout << ++-x << endl; // NOT OK, lvalue required for increment operator z = &x; // cout << ++ *z << endl; // OK, prints 6 // cout << ++ ++ * z << endl; // OK, prints 7 // z = ++ & x; // NOT OK, lvalue required // cout << ++ -x << endl; // NOT OK, lvalue required // cout << ++ +x << endl; // NOT OK, lvalue required */ /* // What happens when you apply unary minus to boolean? - Returns -1 bool flag = true; cout << - flag << endl; // What happens when you apply unary minus to char? - returns -110 char mi = 'n'; cout << - mi << endl; */ // Is string treated as true or false: true, even when empty string cout << ("Hello" && true) << endl; cout << ("" && true) << endl; }