简体   繁体   中英

Arithmetic operations in IF loop

What does the below code do? I'm very confused with its working. Because I thought that the if loop runs till the range of int. But I'm confused when I try to print the value of i . Please help me out with this.

#include<stdio.h>

void main()
{
    static int i;
    for (;;)
        if (i+++”Apple”)
            printf(“Banana”);
        else
            break;
}

It is interpreted as i++ + "Apple" . Since i is static and does not have an initializer, i++ yields 0. So the whole expression is 0 + some address or equivalent to if ("Apple") .

EDIT

As Jonathan Leffler correctly notes in the comments, what I said above only applies to the first iteration. After that it will keep incrementing i and will keep printing "Banana".

I think at some point, due to overflows (if it doesn't crash) "Apple" + i will yield 0 and the loop will break. Again, I don't really know what a well-meaning compiler should do when one adds a pointer and a large number.

As Eric Postpischil commented, you can only advance the pointer until it points to one-past the allocated space. In your exxample adding 7 will advance the pointer one-past the allocated space ("Apples\\0"). Adding more is undefined behavior and technically strange things can happen.

Use int main(void) instead of void main() .

The expression i+++"Apple" is parsed as (i++) + "Apple" ; the string literal "Apple" is converted from an expression of type "6-element array of char " to "pointer to char ", and its value is the address of the first element of the array. The expression i++ evaluates to the current value of i , and as a side effect , the value in i is incremented by 1.

So, we're adding the result of the integer expression i++ to the pointer value resulting from the expression "Apple" ; this gives us a new pointer value that's equal or greater than the address of "Apple" . So assuming the address of the string literal "Apple" is 0x80123450, then basically we're evaluating the values

0x80123450 + 0
0x80123450 + 1
0x80123450 + 2
...

all of which should evaluate to non-zero, which causes the printf statement to be executed. The question is what happens when i++ results in an integer overflow (the behavior of which is not well defined) or the value of i+++"Apple" results in an overflow for a pointer value. It's not clear that i+++"Apple" will ever result in a 0-valued expression.

This code SHOULD Have been written like this:

char *apple = "Apple";
for(i = 0; apple[i++];)
    printf("Banana");

Not only is it clearer than the code posted in the original, it is also clearer to see what it does. But I guess this came from "Look how bizarre we can write things in C". There are lots of things that are possible in C that isn't a great idea.

It is also possible to learn to balance a plate of hot food on your head for the purpose of serving yourself dinner. It doesn't make it a particularly great idea - unless you don't have hands and feet, I suppose... ;)

Edit: Except this is wrong... The equivalent is:

char *apple = "Apple";
for(i = 0; apple+i++ != NULL;)
    printf("Banana");

On a 64-bit machine, that will take a while. If it finishes in reasonable time (sending output to /dev/null), I will update. It takes approximitely three minutes on my machine (AMD 3.4GHz Phenom II).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM