Johan Bezem
Johan Bezem

Reputation: 2594

What C program behaves differently in run-time when compiled with C89 and C99?

I found the following snippet (I think in Wikipedia) that creates a different run-time when C++ comments are recognized than when not:

int a = 4 //* This is a comment, but where does it end? */ 2
  ;

But until now that's been the only one (variants excluded).

I'm not interested in differentiating using __STDC__ and the like, and not in programs that C89 will not compile.

Are there other programs/snippets producing a different run-time with C89 than C99?

Upvotes: 15

Views: 548

Answers (3)

caf
caf

Reputation: 239341

This program will print 0.000000 on a conforming C89 implementation and 1.000000 on a conforming C99 implementation:

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

int main()
{
    double d = strtod("0x1", NULL);
    printf("%f\n", d);
    return 0;
}

Upvotes: 6

Joseph Quinsey
Joseph Quinsey

Reputation: 9972

Two examples:

  • C99 has -3/2 as Defined Behaviour (namely, to truncate to zero).

  • C99 has -1<<1 as Undefined Behaviour (but not C89).

Also, in the past I've run into problems with 64-bit enums, such as enum {mask = 1ULL << 32}, but I don't recall if the compiler was silent, or just quietly did the wrong thing.

Upvotes: 3

Windows programmer
Windows programmer

Reputation: 8075

Integer division can produce a different result, depending on which c89 implementation you used.

Does either ANSI C or ISO C specify what -5 % 10 should be?

Upvotes: 2

Related Questions