15

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?

Community
  • 1
  • 1
Johan Bezem
  • 2,582
  • 1
  • 20
  • 47
  • I think the rules for types of integer literals changed slightly, so you can probably make a program where an expression has the wrong signedness and thus different behavior depending on C89 vs C99... – R.. GitHub STOP HELPING ICE Nov 17 '11 at 06:02
  • @R.. Any pointers to a source? I'll figure it out myself, but a pointer to a source would be helpful. – Johan Bezem Nov 17 '11 at 06:11
  • 3
    Sorry, that's why I wrote it as a comment not an answer. :-) – R.. GitHub STOP HELPING ICE Nov 17 '11 at 06:27
  • I think the standard committee spends a lot of effort to have backwards compatibility. I you find another than the one you cite, you should file a defect report. – Jens Gustedt Nov 17 '11 at 07:09
  • To whoever voted to close this question: Imagine what will happen when you have to debug someone else's code, and you don't know which compiler they originally depended on. – Windows programmer Nov 17 '11 at 07:12
  • @JamesAnderson (and two others) I don't want to be picky, but what is the necessity to keep editing my title? A feature is a property of a programming language and show behavior only when implemented. A program shows behavior since it is an implementation. Shees, three edits back and forth... – Johan Bezem Nov 17 '11 at 08:34
  • @Johan. For clarity really. The title as it stands is about particular programs, whereas, you question is about how features of the programming language are implemented differently according to different standards. – James Anderson Nov 18 '11 at 01:41
  • @Windowsprogrammer: You don't need to know the compiler. The example code above is a bug (unless the intent is exactly to find the difference between compilers) on any compiler, because its behaviour relies on unexpected and non-obvious behaviour, so if you encounter that kind of code, you change it. – gnasher729 Oct 07 '14 at 16:01

3 Answers3

6

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;
}
caf
  • 233,326
  • 40
  • 323
  • 462
  • +1 Wonderful! C89 stops at the `x` and converts only zero (since hex number support is new in C99), whereas C99 converts the hex number 1. Thanks, this is what I'm looking for! – Johan Bezem Nov 17 '11 at 10:39
  • @JohanBezem: Be aware that as a practical matter, pure C89 standard libraries seem to be getting thin on the ground. – caf Nov 17 '11 at 11:15
  • I don't intend to use this in live code. And I'm in embedded, pretty ancient compilers survive there occasionally. – Johan Bezem Nov 17 '11 at 12:01
3

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.

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • AFAIK `long long` is not in the C89 Standard. I'll evaluate the other two examples, thanks! – Johan Bezem Nov 17 '11 at 08:24
  • 1
    even in C99 (as well as C1x), enumerations are restricted to values in range of `int` – Christoph Nov 17 '11 at 09:12
  • @Christoph True, but many compilers will support some deviations like this. – Johan Bezem Nov 17 '11 at 09:33
  • @JosephQuinsey Your two examples are correct, but conforming C89 compilers may interpret the expressions identical to C99 conforming compilers AFAICT. That would make it an unreliable test, albeit it a valid one. Thanks anyway! +1 – Johan Bezem Nov 17 '11 at 10:43
2

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?

Community
  • 1
  • 1
Windows programmer
  • 7,871
  • 1
  • 22
  • 23