473,748 Members | 4,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size_t and C90/C99

#include <stdio.h>

#ifdef C99
#define FMT_STRING "The value is %zu.\n"
#define CAST_SIZE_T
#else
#define FMT_STRING "The value is %lu.\n"
#define CAST_SIZE_T (unsigned long)
#endif

int main(void) {
size_t val=42;

printf(FMT_STRI NG, CAST_SIZE_T val);
return 0;
}
I don't even know for sure if the C99 macro is defined in my
implementation ... but the code compiled and run without glitches with
gcc

gcc -W -Wall -std=c89 -pedantic c9099.c -oc90
gcc -W -Wall -std=c99 -pedantic c9099.c -oc99
My questions are:

a) Is there a standard way to identify the standard being used to
compile the program (the "C99" predefined macro?)?

b) Is the way I used #ifdef usual accepted practice to code for the two
standards?

--
If you're posting through Google read <http://cfaj.freeshell. org/google>
Feb 11 '06 #1
7 2870
Pedro Graca wrote:
#include <stdio.h>

#ifdef C99
#define FMT_STRING "The value is %zu.\n"
#define CAST_SIZE_T
#else
#define FMT_STRING "The value is %lu.\n"
#define CAST_SIZE_T (unsigned long)
#endif
[...]

My questions are:

a) Is there a standard way to identify the standard being used to
compile the program (the "C99" predefined macro?)?
#if __STDC_VERSION >= 199901
b) Is the way I used #ifdef usual accepted practice to code for the two
standards?


Nothing wrong with it that I can see.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 11 '06 #2
Eric Sosman wrote:

#if __STDC_VERSION >= 199901
[...]


Oh, drat. __STDC_VERSION_ _, of course.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 11 '06 #3
Eric Sosman wrote:
Pedro Graca wrote:
a) Is there a standard way to identify the standard being used to
compile the program (the "C99" predefined macro?)?


#if __STDC_VERSION_ _ >= 199901
b) Is the way I used #ifdef usual accepted practice to code for the two
standards?


Nothing wrong with it that I can see.


Thank you Eric.

--
If you're posting through Google read <http://cfaj.freeshell. org/google>
Feb 11 '06 #4
Eric Sosman wrote:
Pedro Graca wrote:
#include <stdio.h>

#ifdef C99
#define FMT_STRING "The value is %zu.\n"
#define CAST_SIZE_T
#else
#define FMT_STRING "The value is %lu.\n"
#define CAST_SIZE_T (unsigned long)
In terms of style, a function macro would be better IMO.
#endif
[...]

My questions are:

a) Is there a standard way to identify the standard being used to
compile the program (the "C99" predefined macro?)?


#if __STDC_VERSION_ _ >= 199901 [corrected]
b) Is the way I used #ifdef usual accepted practice to code for the two
standards?


Nothing wrong with it that I can see.


__STDC_VERSION_ _ may be set to something larger than 199901 on a C90
implementation. Using it is therefore potentially unreliable.

My alternative would be...

#include <stdio.h>
#include <limits.h>

#ifdef SIZE_MAX /* C99? */
#define FMT_PRI_SIZE_T "z"
#define CAST_PRI_SIZE_T (sz) ((size_t) (sz))
#else
#define FMT_PRI_SIZE_T "l"
#define CAST_PRI_SIZE_T (sz) ((unsigned long)(size_t) (sz))
#endif

int main(void)
{
printf("Maximum size_t value is %" FMT_PRI_SIZE_T "u.\n",
CAST_PRI_SIZE_T (-1));
return 0;
}

--
Peter

Feb 12 '06 #5
"Peter Nilsson" <ai***@acay.com .au> writes:
[...]
__STDC_VERSION_ _ may be set to something larger than 199901 on a C90
implementation. Using it is therefore potentially unreliable.


Theoretically, yes, but I don't think that's a realistic concern.
Since the C90 standard doesn't mention __STDC_VERSION_ _, a C90
compiler can define it any way it likes -- but an implementation that
sets it to something larger than 199901L would be deliberately lying,
and you wouldn't be able to depend on *anything* it tells you.

If the compiler lies to me, I will get my revenge.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 12 '06 #6
Keith Thompson wrote:
"Peter Nilsson" <ai***@acay.com .au> writes:
[...]
__STDC_VERSION_ _ may be set to something larger than 199901 on a C90
implementation. Using it is therefore potentially unreliable.
Theoretically, yes, but I don't think that's a realistic concern.


I don't think so either, but not for the same reason that you do.

I don't think it's a concern because I think the few C90
implementations that bother to specify __STDC_VERSION_ _
will be those that came in after the normative additions to C90.
Since the C90 standard doesn't mention __STDC_VERSION_ _, a C90
compiler can define it any way it likes -- but an implementation that
sets it to something larger than 199901L would be deliberately lying,
Why would it be a _deliberate_ lie? For decades, I've been using
YYYYMMDD style in version constants of my own, even when the
gap between versions is likely to be months (even years).

I don't see why an implementor of C90 in the late 80s, who didn't
have the luxury of a crystal ball, would think that 19891101 (say)
would be unusual, let alone a _deliberate_ attempt to usurp
future C programmers!
and you wouldn't be able to depend on *anything* it tells you.
Even gcc ports can be made to set __STDC__ to 1 in _non_ conforming
mode. Do you still trust gcc? I do, for the most part.
If the compiler lies to me, I will get my revenge.


I just ask the author to modify future updates, or I hack the
compiler (or more likely a default header) myself. I don't call
it revenge, I just call it fixing a problem. But in reality, in this
case, it would be fixing a problem of my own making.

--
Peter

Feb 12 '06 #7
"Peter Nilsson" <ai***@acay.com .au> writes:
Keith Thompson wrote:
"Peter Nilsson" <ai***@acay.com .au> writes:
[...]
> __STDC_VERSION_ _ may be set to something larger than 199901 on a C90
> implementation. Using it is therefore potentially unreliable.


Theoretically, yes, but I don't think that's a realistic concern.


I don't think so either, but not for the same reason that you do.

I don't think it's a concern because I think the few C90
implementations that bother to specify __STDC_VERSION_ _
will be those that came in after the normative additions to C90.


Sure, Amendment 1 (1995) introduced __STDC_VERSION_ _ and specified a
value of 199409L.
Since the C90 standard doesn't mention __STDC_VERSION_ _, a C90
compiler can define it any way it likes -- but an implementation that
sets it to something larger than 199901L would be deliberately lying,


Why would it be a _deliberate_ lie? For decades, I've been using
YYYYMMDD style in version constants of my own, even when the
gap between versions is likely to be months (even years).

I don't see why an implementor of C90 in the late 80s, who didn't
have the luxury of a crystal ball, would think that 19891101 (say)
would be unusual, let alone a _deliberate_ attempt to usurp
future C programmers!


Before Amendment 1 was released, such an implementer would have had to
independently invent the name __STDC_VERSION_ _, which seems unlikely
(and I've never heard of an implementation that actually did so).
After Amendment 1, the meaning was established, and it's hard to
imagine that setting it to anything greater than 199901L would be
anything but a deliberate lie.

(This is all hypothetical, of course.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
19522
by: javadesigner | last post by:
Hi: I am a bit new to C programming and am trying to write a wrapper around malloc. Since malloc takes size_t as it's parameter, I want to be able to see if my function recieved an argument less than or equal to the max size_t type. So:
18
2178
by: rayw | last post by:
I used to believe that size_t was something to do with integral types, and the std. Something along the lines of .. a char is 8 bits, a int >= a char a long >= int
5
3168
by: edware | last post by:
Hello, I have some questions about the size_t type. First, what do we know about size_t? From what I have read I believe that it is an unsigned integer, but not necessarily an int. Am I correct? Does this mean that if I need to compare two variables, one of the size_t type, should the other also be a size_t variable? There could be problems if I compare it with an int, if those have different sizes, right?
12
10717
by: Alex Vinokur | last post by:
Why was the size_t type defined in compilers in addition to unsigned int/long? When/why should one use size_t? Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
39
8044
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using it otherwise is confusing. I also thought that size_t could be signed but it seems I was wrong on that one. So if you were to see code iterating through a table of Foo objects using an index of size_t type, would it be confusing? Should I have...
23
4912
by: bwaichu | last post by:
To avoid padding in structures, where is the best place to put size_t variables? According the faq question 2.12 (http://c-faq.com/struct/padding.html), it says: "If you're worried about wasted space, you can minimize the effects of padding by ordering the members of a structure based on their base types, from largest to smallest."
318
13010
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people started to try to find possible errors, a harmless passtime they seem to enjoy. One of their remarks was that I used "int" instead of "size_t" for the input of my allocator function.
73
7436
by: Yevgen Muntyan | last post by:
Hey, I was reading C99 Rationale, and it has the following two QUIET CHANGE paragraphs: 6.5.3.4: "With the introduction of the long long and extended integer types, the sizeof operator may yield a value that exceeds the range of an unsigned long." 6.5.6: "With the introduction of the long long and extended integer
89
5752
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
27
2348
by: mike3 | last post by:
Hi. I can't believe I may have to use an array here. I've got this bignum package I was making in C++ for a fractal generator, and tried an approach that was suggested to me here a while back, about using a "stack of vectors" instead of a static array. Anyway, I put the suggestion into effect, and it seems the routine that uses the stack of vectors (an in-place multiplication routine) is
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.