C/C++ Interview Questions




  1. Notice the line that reads

    i = (j++, k++);

    This line actually performs three actions at once. These are the three actions, in order:

    1. Assigns the value of k to i. This happens because the left value (lvalue) always evaluates to the rightmost argument. In this case, it evaluates to k. Notice that it does not evaluate to k++, because k++ is a postfix incremental expression, and k is not incremented until the assignment of k to i is made. If the expression had read ++k, the value of ++k would be assigned to i because it is a prefix incremental expression, and it is incremented before the assignment is made.

    2. Increments j.

    3. Increments k.

  2. Is left-to-right or right-to-left order guaranteed for operator precedence?

    The simple answer to this question is neither. The C language does not always evaluate left-to-right or right-to-left. Generally, function calls are evaluated first, followed by complex expressions and then simple expressions.

    Additionally, most of today’s popular C compilers often rearrange the order in which the expression is evaluated in order to get better optimized code. You therefore should always implicitly define your operator precedence by using parentheses.

    For example, consider the following expression:

    a = b + c/d / function_call() * 5

    The way this expression is to be evaluated is totally ambiguous, and you probably will not get the results you want. Instead, try writing it by using implicit operator precedence:

    a = b + (((c/d) / function_call()) * 5)

    Using this method, you can be assured that your expression will be evaluated properly and that the compiler will not rearrange operators for optimization purposes.


  3. What is an lvalue?

    An lvalue is an expression to which a value can be assigned. The lvalue expression is located on the left side of an assignment statement, whereas an rvalue is located on the right side of an assignment statement. Each assignment statement must have an lvalue and an rvalue. The lvalue expression must reference a storable variable in memory. It cannot be a constant. For instance, the following lines show a few examples of lvalues:

    int x;
    int* p_int;
    x = 1;
    *p_int = 5;

    The variable x is an integer, which is a storable location in memory. Therefore, the statement x = 1 qualifies x to be an lvalue. Notice the second assignment statement, *p_int = 5. By using the * modifier to reference the area of memory that p_int points to, *p_int is qualified as an lvalue. In contrast, here are a few examples of what would not be considered lvalues:

    #define CONST_VAL 10
    int x;
    /* example 1 */
    1 = x;
    /* example 2 */
    CONST_VAL = 5;

    In both statements, the left side of the statement evaluates to a constant value that cannot be changed because constants do not represent storable locations in memory. Therefore, these two assignment statements do not contain lvalues and will be flagged by your compiler as errors.


  4. Can an array be an lvalue?

    Is an array an expression to which we can assign a value? The answer to this question is no, because an array is composed of several separate array elements that cannot be treated as a whole for assignment purposes. The following statement is therefore illegal:

    int x[5], y[5];

    x = y;

    You could, however, use a for loop to iterate through each element of the array and assign values individually, such as in this example:

    int i;
    int x[5];
    int y[5];
    for (i=0; i<5; i++)
         x[i] = y[i]

    Additionally, you might want to copy the whole array all at once. You can do so using a library function such as the memcpy() function, which is shown here:

    memcpy(x, y, sizeof(y));

    It should be noted here that unlike arrays, structures can be treated as lvalues. Thus, you can assign one structure variable to another structure variable of the same type, such as this:

    typedef struct t_name
         char last_name[25];
         char first_name[15];
         char middle_init[2];
    } NAME;
    NAME my_name, your_name;
    your_name = my_name;

    In the preceding example, the entire contents of the my_name structure were copied into the your_name structure. This is essentially the same as the following line:

    memcpy(your_name, my_name, sizeof(your_name));



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s