paranoid_android
paranoid_android

Reputation: 3

C - ternary operator in initialization statement of a for-loop

I am quite new to C and preparing for a test by working through some sample questions. Given the code below, I don't understand the initialization of int j by using the ternary operator.

for (int j = i ? foo : 2; j < 10; j++)
#include <stdio.h>
int main (void) {

    int foo = -1;
    do {
        int i = foo++;

        if (foo > 8)
            break;
        else if (foo < 5)
            continue;
        for (int j = i ? foo : 2; j < 10; j++) {
            printf("foo=%d, i=%d, j=%d;", foo, i, j);
        }
        i = foo++;
    } while (1);
    return foo;

    return 0;
}

The results I got from debugging:

So my questions would be:

Upvotes: 0

Views: 120

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 223843

ternary operator in initialization statement of a for-loop

In C, each subexpression is evaluated by itself. A ternary operator is evaluated according to the rules for a ternary operator. The fact it is used in an initialization does not affect it.

… j=0 (according to the debugger, although it was never initialized with the value 0?)…

Once the program is compiled and you are running it, some memory has been reserved for j, and the debugger knows where this memory is.1 When you ask the debugger for the value of j, the debugger shows you the contents of this memory. While your C source code never initialized j, the memory was initialized by the operating system or program loader and may also have been used by other parts of your program before reaching this code. So there is some value in memory, and the debugger shows you that.

… At this time the values are: i=4, foo=5…

Since i has the value 4, the ternary expression i ? foo : 2 selects the second operand, foo. Then, since foo has the value 5, the value of the ternary expression is 5.

When stepping over j changes from 0 to 5 (=foo) - and this change, I don't understand.

Since the ternary expression has the value 5 and it was used to initialize j, j is given the value 5.

Why is int j = 0 without being initialized before? Shouldn't it be some random numbers?

No, the C standard does not specify what value an uninitialized object has. It does not specify that it is random.

As noted above, once a program is compiled and is executing, there is specific memory reserved for an object, and that memory will have some value in its contents. That value is not actually random; it is the result of processes beyond those specified by the C standard. During compilation, the semantics can be more complicated. Per the C standard, an uninitialized object is not required to have a fixed value, and this can cause the compiler not to tie the object to any particular memory until the point in the program where it is given a value.

Note

1 It is possible the memory used for j differs at different times, and the debugger may show you the contents of different parts of memory at different points in your program. For example, a local variable might be cached in a processor register at one point in the code and stored in the stack at another. While the abstract model of computing used by the C standard has one fixed portion of memory for each object, the actual implementation may use different memory at different times.

Upvotes: 0

dbush
dbush

Reputation: 224972

How does this "ternary initialization work"?

j is being initialized with the value of the expression i ? foo : 2. Since i has a non-zero value (4), the second part of the ternary is evaluated, i.e. foo which is 5, and that value is what j is initialized with.

Why is int j = 0 without being initialized before? Shouldn't it be some random numbers?

0 is a random number. There's no particular reason it had to be this value.

Upvotes: 1

Related Questions