0
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    for (i = 0;;)
    {
        printf("%d\n", ((i++) * i) ^ i);
    }
}


Compiling , it runs till the Limit and goes to negative integer.Why this behaviour?
Did it for fun while reading the For statement syntax...

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Omjelo
  • 109
  • 8

2 Answers2

1

Your program invokes undefined behavior twice, integer overflow and unsequenced expression ((i++)*i)^i).

Sequence points ISO/IEC 9899:2017C17 N2176 § B.29:

  • Between the evaluations of the function designator and actual arguments in a function call and the actual call. (6.5.2.2).

  • Between the evaluations of the first and second operands of the following operators: logical AND&&(6.5.13); logical OR||(6.5.14); comma,(6.5.17).

  • Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated (6.5.15).

  • Between the evaluation of a full expression and the next full expression to be evaluated. The following are full expressions: a full declarator for a variably modified type; an initializer that is not part of a compound literal (6.7.9); the expression in an expression statement; the controlling expression of a selection statement (if or switch) (6.8.4); the controlling expression of a while or do statement (6.8.5); each of the (optional) expressions of a for statement (6.8.5.3);the (optional) expression in a return statement (6.8.6.4).

  • Immediately before a library function returns (7.1.4).

  • After the actions associated with each formatted input/output function conversion specifier(7.21.6, 7.29.2).

  • Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call (7.22.5).

Undefined Beahaviour ISO/IEC 9899:2017C17 N2176 § 3.4.3:

1 - undefined behavior - behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements.

2 - Note 1 to entry: Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with orwithout the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnosticmessage).

3 - EXAMPLE: An example of undefined behavior is the behavior on integer overflow.

For GCC, the compile doesn't show none of those warnings by default, if you use gcc file.c -Wall -Wextra the sequence point warning is issued, Integer overflow is not part of these generic flags, I'm sure there is a specialized one, you can look in https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

Community
  • 1
  • 1
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Pardon me for asking, the only error was the Wsequence which indicated the undefined sequence but none about the integer overflow... I also declared the flag - Wstrict-overflow – Omjelo Apr 16 '20 at 14:00
1

Considering int type to be of 32 bits. Since 'i' starts from zero, it will be something like this:

00000 ..... 32 times

After i++:

00000000000000000000000000000001

This will keep on incrementing until it becomes:

0111 .. (31 1's)

After the next increment you go into the negative number:

10000 ...(31 0's)

In 2's complement above is -((2^32)).

Further after many post increments it becomes:

11111111111111111111111111111111 (32 times 1)

Above is -1.

When the next increment occurs it becomes:

0000 ... (all 32 zeroes)

and this process continues.

Syed Waris
  • 1,056
  • 1
  • 11
  • 16