Hi i'm trying to study for c programming course midterm and there are few questions i have no idea why the answer is. could anyone describe why the answer comes out like these?

#include <stdio.h>
int main(){
    int i = 3;
    if (((i+=3)&&(i++>6))||((++i>8)&&(i+=3)))
        printf("Yes. i is %d",i);
    else
        printf("No. i is %d",i);
    return 0;
}

the answer for this code is "No. i is 8"

the second code is:

#include <stdio.h>
int main(){
    int i, a[6];
    for(i=0;i++<5;) a[i] = i*10;
    for(--i;--i>0;)
        printf("%d - %d \n",i,a[i]);
    return 0;
}

and the answer to this code is:
4 - 40
3 - 30
2 - 20
1 - 10

the third code is:

#include<stdio.h>
#define printmax(x,y) printf("the max of %d and %d is",x,y); printf("%d\n",(x > y ? x: y))
int main(){
    int i = 5, j = 10;
    printmax((i++),(j++));
    printf("i = %d, j = %d",i,j);
    return 0;
}

and the answer to this code is:
the max of 5 and 10 is 12
i = 7, j = 13

i'm not very experienced with C programming and so i'm trying to understand but i just can't :(((

Recommended Answers

All 4 Replies

These examples are hideous. But let's break it down.

(((i+=3)&&(i++>6))||((++i>8)&&(i+=3))

i starts as 3. Then it's immediately incremented by 3 (making it 6) and tested against 0. 6 is not equal to 0, so the first half of the && clause succeeds. i is not greater than 6 so the second half fails, but i still gets incremented to 7 afterward as the postfix increment is used.

Both sides of the || are tested because one might be true, so i is then incremented before testing the left clause of the right hand &&. This fails because i is now 8, and the test is asking if i is greater than 8. This is an && test, so the right hand clause doesn't need to be checked if the left hand clause fails, so i is not incremented further by 3.

Both && statements failed, and i remains 8, so your output is the body of the else clause.

for(i=0;i++<5;) a[i] = i*10;
for(--i;--i>0;)
    printf("%d - %d \n",i,a[i]);

This one is relatively easy. In the first loop i is always incremented, but only after testing against 5.

The end result is that a[0] is never modified, but a[1] through a[4] are. At the end of the loop, i is 6, so the second loop decrements it immediately to 5. Then the condition decrements it again before testing against 0. The result is that you have a perfect reversal of the first loop.

printmax((i++),(j++));

This one is also relatively easy, but you need to recognize that macros are textual replacement. That means your printmax is replaced with this:

printf("the max of %d and %d is",(j++),(j++));
printf("%d\n",((i++) > (j++) ? (i++): (j++)));

i and j are incremented far too many times, which easily explains your output.

On a side note, I'm utterly amazed that none of your examples exhibit undefined behavior. It seems like your teacher actually knows something about C.

commented: weird examples, but helpfull explanations +15
commented: Helpful, but these are TERRIBLE examples of C code. Why not try to teach proper coding practices! :rolleyes: +12

If this professor worked for me, I'd fire him! This is a perfect example of "Those who can, do. Those who can't, teach."... He is trying to be overly cleaver and illustrate how you can twist meaning in programming logic. 1) it violates the KISS principal. 2) it doesn't really teach the student proper programming practices. 3) has this person EVER written a significant program that has had any social or other impact?

thank you :(( really helped me understand the concept of postfixes and prefixes :))

If this professor worked for me, I'd fire him!

I'm okay with these examples as thought experiments, provided it's very clearly stated that this is not how you should write code, and by working through the examples you can see why.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.