Posted by krzaq on 2013-05-12 – 18:27:52
Filed under Programming
Tagged as C, C quiz, C++11
A new C++ quiz website has been created under the address cppquiz.org. It’s work in progress, but the quality of questions so far is excellent. I especially recommend questions number 31 and 15.
Edit: three questions of mine have been approved: 37, 38 and 42.
Posted by krzaq on 2013-04-15 – 16:41:33
Ever since introduction of C++03 the standard committee set out to fix many minor annoyances in the language (auto, ranged for and initializer lists to name a few). Most of those are new things that cannot change the behaviour of existing code, but there are, of course, exceptions. In C++03 the first line of the following code was ill-formed, because >> was parsed as operator>>.
std::vector<std::vector<int>> X; // ill-formed
std::vector<std::vector<int> > Y; // ok |
Read More »
Posted by krzaq on 2013-02-26 – 06:12:44
Filed under Programming
Tagged as C, C++, C++11, trivia
It is well known — and intuitively understood by most — that adding a set of parentheses usually doesn’t change anything; for example, int answer = 42; is equal to int answer = (42); or int answer = ((42));. There are some important exceptions to that rule, however, and I’ll talk about these in this post.
Macros
Although macros are rarely used in good C++ code, it is important to be able to understand what’s happening and why. Using a popular example of MIN macro, the naïve implementation would look like this:
#define MIN(x,y) x < y ? x : y |
To a beginner, this would look like a correct implementation, and indeed, it would work in some cases; for example, answer below would indeed be equal to 42:
Unfortunately, macros are expanded as text, and in the following example, possibly surprisingly, answer would hold the value of 41 instead:
int answer = 2 + MIN(40,41); |
Read More »
Posted by krzaq on 2013-02-18 – 08:15:42
Filed under Programming
Tagged as C, C++, C++11, trivia
Consider the following code:
1
2
3
4
5
6
7
8
9
10
11
| #include <iostream>
using namespace std;
int main()
{
int n = 10;
while(n --> 0)
cout << "n: " << n << endl;
} |
Should it compile? If your answer is yes, what should be its output?
Spoiler warning: if you want to try to answer it, don’t look below the image following this paragraph.
Picture 1. Placeholder.
Read More »
Posted by krzaq on 2013-02-11 – 19:02:03
Filed under Programming
Tagged as C, C quiz, C++, C++ quiz
Some men just want to watch the world burn. Some of them are teacher assistants. And some of them give tests like this to an intro class. Sure, it’s not very difficult (except for 1e, which requires deeper understanding of the language), but it’s really, really tricky.
Well, check for yourself.
Easily findable link: [link]
Warning: spoilers ahead.
Read More »