-8

// example

#include<stdio.h>
    int main(){
    int i,j;
    for(i<4; j=3; j=0){
        printf("%d",i);
    } 
}

// why the output is an infinite for loop with i=1

Xanley
  • 1
  • 2
  • 4
    Why do you ask? What possible reason could anyone have to do that? – StoryTeller - Unslander Monica May 03 '18 at 06:44
  • 1
    What happens when the letters in `for` are interchanged as `rof`? The same happens here. It makes no sense. – Ajay Brahmakshatriya May 03 '18 at 06:46
  • Maybe you should lookup the `for` loop on you r preferred search engine. – CannedMoose May 03 '18 at 06:47
  • 1
    In your case, undefined behaviour. `i` is uninitialised, and the first thing done in the loop is to evaluate `i < 4` (which accesses the value of `i`, so behaviour is undefined). – Peter May 03 '18 at 06:50
  • What happens? Try it! Step through the code line by line in a debugger to see what happens. – Some programmer dude May 03 '18 at 06:50
  • @Peter Unfortunately the OP have use both C and C++ as tags, which means it's not so clear-cut. In C++ it's definite undefined behavior. In C it *might* be UB if the indeterminate value of `i` is a *trap value* (which doesn't exist for integers really). – Some programmer dude May 03 '18 at 06:51
  • @Someprogrammerdude why is it definitely UB in C++? – Jabberwocky May 03 '18 at 06:52
  • @Shiva In the future please refrain from using both C and C++ tags. They are two very different languages and the behavior in many situations can be very different. – Some programmer dude May 03 '18 at 06:53
  • @Someprogrammerdude - if the value is indeterminate, the result of accessing it is undefined. – Peter May 03 '18 at 06:53
  • @MichaelWalz In C++ it's UB to use indeterminate values. In C it depends of the actual indeterminate value. The specifications differ on this. – Some programmer dude May 03 '18 at 06:54
  • Actually, no. In C++, the act of accessing the value of an uninitialised variable is undefined behaviour. The value, determinate or not, is irrelevant. – Peter May 03 '18 at 06:54
  • @Someprogrammerdude `i` doesn't have it's address taken. So it is not indeterminate but undefined in C too. On some targets the registers could be in a "uninitialized state" and the behavior is not defined when these registers are accessed for a read. Anyway, this discussion is off-topic. I am happy with just telling OP that nothing meaningful can be expected from the statement. – Ajay Brahmakshatriya May 03 '18 at 06:56
  • The C standard does say that the value of an uninitialised variable is indeterminate, but also says (I can't remember the section number offhand) that accessing an indeterminate value gives undefined behaviour. The C++ standards simply says that the act of accessing an uninitialised variable gives undefined behaviour. – Peter May 03 '18 at 07:03
  • In the C11 specification, §6.2.6.1/5 specifies "trap representation" values, and that those are UB. *But* the paragraph starts with saying "Certain object representations need not represent a value of the object type." On any machine running today there are no such possible "trap representation" values for integer types. All possible values of an integer are representable. Therefore in C using an `int` and its value (without casting to any other non-integer type) will never lead to UB. – Some programmer dude May 03 '18 at 07:14

3 Answers3

3

Since for loop of form

for (initialization; condition; increment) {/*body*/}

Can be transformed to the while loop, like so:

{
initialization;
while (condition)
    {
    /*body*/
    increment;
    }
}

Your program, can be, effectively, transformed to:

int i,j;
{
i<4;
while (j=3)
    {
    printf("%d",i);
    j=0;
    }
}

Since assignment operator returns the value, that is assigned (in this case: 3), and any non-zero integer value is evaluated to true, you get an infinite loop.

As to what output you get.. It is undefined behavior, due to you using uninitialized variable i.

Algirdas Preidžius
  • 1,769
  • 3
  • 14
  • 17
  • This is undefined behavior in C and C++ both, since `i` is an uninitialized variable with automatic storage duration, which does not have its address taken. [See this](https://stackoverflow.com/a/40674888/584518). – Lundin May 03 '18 at 07:44
  • @Lundin Thank you, I edited the answer. – Algirdas Preidžius May 03 '18 at 07:52
1

in for the loop condition part, j=3 become always true. that is why you got an infinite result.

msc
  • 33,420
  • 29
  • 119
  • 214
0

As only the middle condition will be compared and there will always be something not 0 it will continue on and on!