Article View: comp.std.c
Article #4565Re: sizeof(1L) in preprocesor;How to tell sizeof(double)>sizeof(long) ?
From: Mark Brader
Date: Fri, 05 Jan 1996 09:41
Date: Fri, 05 Jan 1996 09:41
53 lines
2477 bytes
2477 bytes
Nobuhiko Katayama (nk@lns598.lns.cornell.edu) writes: > Dear experts, > I understand that sizeof(double) cannot be used with #if as > double is a type name. This is not the reason. The reason is that the standard requires a C program to be processed in specific sequence of 8 logical phases of translation. The #if directive is evaluated during translation phase 4, but the identifiers "sizeof" and "double" are not understood to be keywords until translation phase 7. > Could you tell me where in the standard it says > that I cannot use sizeof(1.0) or sizeof(1L)... ? The same reason applies. Section 5.1.1.2 of the standard (2.1.1.2 in the original ANSI version) defines the translation phases, and section 6.8.1 (3.8.1) describes the #if directive. In particular, note the footnote where it points out that in the expression inside a #if, there are only identifiers and not keywords. > is there any portable way to tell whether > long is longer(shorter) than double in preprocessor stage ? I can't think of any way. You *can* use the various characteristics defined in <limits.h> and <float.h>, as explained in section 5.2.4.2 (2.2.4.2), to find out various information about the types. In particular, if what you really want is to choose whichever of the two types can hold the larger exact integer value, then in practice you need only do: #include <limits.h> #include <float.h> #if LONG_MAX * DBL_EPSILON > 1 typedef long mytype; #define mytype_format "%ld" /* for printf */ #else typedef double mytype; #define mytype_format "%f" #endif This may not work if the number of bits in a long is close to the number of bits in the significand of a double -- I don't want to think about the exact criteria -- but in practice that won't happen on any likely machine. -- Mark Brader | "As penance, I suppose I should read the standard msb@sq.com | again, but I've already lost as much hair as SoftQuad Inc., Toronto | I can afford." -- Tom Kelly My text in this article is in the public domain.
Technical Details
Message-ID:
<1996Jan5.094122.16151@sq.com>Path:
arc1-ca.pugleaf.net!howland.reston.ans.net!torn!sq!msbReferences:
<sc3f9vb6gk.fsf@lns101.lns.cornell.edu>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!howland.reston.ans.net!torn!sq!msb From: msb@sq.com (Mark Brader) Subject: Re: sizeof(1L) in preprocesor;How to tell sizeof(double)>sizeof(long) ? Message-ID: <1996Jan5.094122.16151@sq.com> Organization: SoftQuad Inc., Toronto, Canada References: <sc3f9vb6gk.fsf@lns101.lns.cornell.edu> Date: Fri, 5 Jan 1996 09:41:22 GMT