1

I see it is legal to use the variable __PTRDIFF_TYPE__ with no header inclusion.

I tried to look for this variable name in ISO/IEC 9899 but it does not appear. I expected to see its definition in the 7th part, C library.

Why is it legal ?

I am using the gcc under Linux/GNU.

alinsoar
  • 15,386
  • 4
  • 57
  • 74
  • Which compiler are you talking about? – Cornstalks Jun 19 '16 at 22:47
  • oh, I forgot to mention the system -- linux/gnu, so gcc. – alinsoar Jun 19 '16 at 22:48
  • To see predefined macros from gcc (and clang), you can use the command: [`gcc -dM -E - < /dev/null`](http://stackoverflow.com/questions/2224334/gcc-dump-preprocessor-defines) – Cornstalks Jun 19 '16 at 22:56
  • @Cornstalks: interesting – alinsoar Jun 19 '16 at 22:58
  • That is not standard. And even if implemented, it is **not a variable**, not even a constant, but a macro! What's your problem using the standard type? Relying on such implementation-macros is a bad idea in general. I doubt it is legal for an application to use it. It clearly is not from the standard. Where did you get that impression from? – too honest for this site Jun 19 '16 at 23:03
  • As requested by the OP I have closed this as a duplicate of the newer question that, they say, more precisely states what they want to know. – zwol Jun 26 '16 at 11:40

1 Answers1

2

This is a predefined macro in gcc (a GNU C extension); see https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html.

As for why this is legal: The standard reserves all names starting with two underscores (and all names starting with an underscore followed by an uppercase letter) for use by the implementation, so gcc could predefine __whatever and still follow the standard.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • ah, you mean it's a GCC extension ? I suspect it's used by other compilers. – alinsoar Jun 19 '16 at 22:51
  • @alinsoar: It is an extension to be used for the standard headers. Maybe that's easier than a bunch of `ifdef` to customize `ptrdiff_t` for each platform. As I wrote above, you must not use it in application code. – too honest for this site Jun 19 '16 at 23:04
  • It is legal for an implementation to define that macro. It is not automatically granted to be used by an application. From the standard's view it definitively is not. – too honest for this site Jun 19 '16 at 23:06
  • I suspect, the insertion of such type of variable/object macros are useful for gcc to compile on multiple platforms. So, such a variable is a variable inserted in some ``ARCH.CONF`` file. – alinsoar Jun 19 '16 at 23:32
  • 1
    @alinsoar: I think that's what my comment says – too honest for this site Jun 19 '16 at 23:42
  • Actually my real misunderstanding here is why it's necessary to define both the ``__PTRDIFF_TYPE__`` and in the same time ``ptrdiff_t``? Why only one of them isn't enough? This is what I do not understand in all this bussiness. Can you detail this in your answer ? – alinsoar Jun 24 '16 at 18:54