Перейти к содержимому
Обложка сообщества Разное

C++: Tricky codes

This are my collections of c++ codes which are very tricky.

Some of them I found by making mistakes, so recorded them to avoid making the same bug twice.

Try to guess the output just by reading, please don't use any compiler or IDE.

Do you know other tricky codes?

 
1: A - B
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int a = 020;
  5. short b = 2;
  6. cout << a - b << endl;
  7. return 0;
  8. }
 
2. Condition
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. bool ok = true;
  5. // Try to guess with if condition \
  6. if (!ok && true)
  7. cout << "I am ok" << endl;
  8. return 0;
  9. }
 
3. Comparison
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. unsigned a = 0;
  5. int b = 2;
  6. if (a + b >= -2)
  7. cout << a + b << ">=" << -2 << endl;
  8. else
  9. cout << a + b << "<" << -2 << endl;
  10. return 0;
  11. }
 
4. Binary operation
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int a = 0, b = 1;
  5. if (a&b==0) {
  6. cout << (a&b) << "=" << 0 << endl;
  7. } else {
  8. cout << (a&b) << "!=" << 0 << endl;
  9. }
  10. return 0;
  11. }
 
5. Float casting
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. long long a = 1e18 + 1;
  5. cout << a << endl;
  6. return 0;
  7. }
 
6. String size
  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. string s ="a";
  5. for(int i =0; i <= s.size()-2;++i)
  6. cout << s[i]<<" ";
  7. return0;
  8. }
 

Upd.

Answers:

1. Output: "14". Because 020 is in octal base, therefore 16 - 2 = 14

2. Output: "I am ok". When you use "\", it means that you split one long line to several lines.

So, for a compiler the code looks like this:

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. bool ok = true;
  5. // Try to guess with if condition if (!ok && true)
  6. cout << "I am ok" << endl;
  7. return 0;
  8. }

3. Output: "2<-2". The reason is: unsigned int + int = unsigned int,

i.e unsigned type has higher priority than int when it comes to casting.

Thus, a + b is unsigned.

  1. if(2>=-2)// i.e unsigned >= int

In a result compiler casts both parts to unsigned. As a consequence, the line turns into:

  1. if (2 >= 4294967294)

but wait, why 4294967294? Numbers are stored in binary form. Negative number are stored in what's called two's complement. The two's complement of an n-bit number x is 2^n-x.

Thus, 2^32 - 2 = 4294967294.

4. Output: "0!=0". Because "==" has higher priority. Advice : look into priority table or always put brackets.

5. Output: "1000000000000000000" instead of "1000000000000000001". Because of the representation of float.

6. Output: Runtime Error. Because, s.size() returns "size_t" type which is unsigned integer type. So, s.size() - 2 is unsigned, so it turns into a huge number. In a result, runtime error because it tries to access non-existing index.


3
3
634

Еще по теме

C++: Tricky codes - Yvision.kz