473,699 Members | 2,501 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 2866
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
19510
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
2170
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
3161
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
10709
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
8032
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
4894
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
12957
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
7410
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
5729
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
2334
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
8685
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
9032
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
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
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
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.