Interview Q&A

A collection of 8 Articles

What does the C ??!??! operator do?

??! is a trigraph that translates to |. So it says: if(a || b){ ... }else{ ... } So, what is digraphs and trigraphs? In computer programming, digraphs and trigraphs are sequences of two and three characters, respectively, that appear in source code and, according to a programming language specification, should be treated as if ...

Why in C language is it the case that a[5] == 5[a]?

The C standard defines the Array Subscript Operator [] as follows: a[b] == *(a + b) Therefore a[5] will evaluate to: *(a + 5) and 5[a] will evaluate to: *(5 + a) and from elementary math we know those are equal. This is the direct artifact of arrays behaving as pointers, "a" ...

Why isn't sizeof for a struct equal to the sum of sizeof of each member?

This is because of padding added to satisfy alignment constraints. Data structure alignment impacts both performance and correctness of programs. Here's an example using typical settings for an x86 processor (all used 32 and 64 bit modes): struct X { short s; /* 2 bytes */ /* 2 padding bytes */ int i; /* 4 bytes ...

Why is one loop so much slower than two loops?

Suppose a1, b1, c1, and d1 point to heap memory and code has the following loop. const int n=100000; for(int j=0;j<n;j++){ a1[j] += b1[j]; c1[j] += d1[j]; } This loop is executed 10,000 times via another outer for loop. To speed ...

Why is processing a sorted array faster than an unsorted array?

Here is a piece of Visual C++ code that seems very peculiar. For some strange reason, sorting the data miraculously makes the code almost six times faster. #include <algorithm> #include <ctime> #include <iostream> int main() { // Generate data const unsigned arraySize = 32768; int data[arraySize] ...

What is the name of the "-->" operator?

Here's an example: #include <stdio.h> int main() { int x = 10; while( x --> 0 ) // x goes to 0 { printf("%d ", x); } } Answer --> is not an operator. It is in fact two separate operators, -- and >. The conditional's code decrements x, while returning x's ...