473,692 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#if typedef()

Is there any way to know if there is a typedef of a given name?

Specifically, I need to know if the compiler has a 64-bit integer type,
and need to know if "int64_t" exists. Something like this pseudo-code:

#if typedef(int64_t )
typedef int64_t MY_BIG_INT
#elif typedef(long long)
typedef long long MY_BIG_INT
#else
typedef long MY_BIG_INT
#endif

(Yes, this program needs to work on systems which don't have a 64-bit
integers, and it needs to take advantage of them if they are there.)

Also, what is the standard include file which would be needed to have
the int64_t typedef included? I see it in <stdint.h> on one compiler
I have, and <native.h> on another. I don't see these headers being
included by other standard headers on these systems.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 15 '05 #1
10 12210
In article <42************ ***@spamcop.net >,
Kenneth Brody <ke******@spamc op.net> wrote:
Is there any way to know if there is a typedef of a given name? Specifically , I need to know if the compiler has a 64-bit integer type,
and need to know if "int64_t" exists.

There is no portable way to test that in C89 (I'm not familiar enough
with C99 to know if it were extended in that regard, but I suspect not.)
The usual way to handle this is to have a meta level that tests
for the existance of appropriate types or functions and sets
preprocessing tokens appropriately in a constructed include file
that the rest of the code imports.
--
Look out, there are llamas!
Nov 15 '05 #2
Kenneth Brody <ke******@spamc op.net> writes:
Is there any way to know if there is a typedef of a given name?
No.

[snip]
Also, what is the standard include file which would be needed to have
the int64_t typedef included? I see it in <stdint.h> on one compiler
I have, and <native.h> on another. I don't see these headers being
included by other standard headers on these systems.


In C99, int64_t is defined in <stdint.h>, but the C99 standard is not
universally supported. Strictly speaking, int64_t is optional; it
will be defined only if the implementation has a two's complement
integer type with a width of exactly 64 bits and no padding. In
practice, since C99 requires long long to be at least 64 bits, I'd be
surprised to see a C99 implementation that doesn't have int64_t.

Given a C99 implementation, the macro INT64_MAX will be defined in
<stdint.h> if and only if int64_t is defined, so you can use
#ifdef INT64_MAX

You can check whether you have a C99 implementation with

#if __STDC_VERSION_ _ >= 199901L

A pre-C99 compiler is allowed, but not required, to define int64_t in
an implementation-specific header -- or it can call it int_64_t, or
anything it likes. There's no way in the preprocessor to test whether
a given header exists. You pretty much have to track down the details
for all the implementations you need to support, and write custom code
for each.

Perhaps the best approach is to use your own <stdint.h> (or, more
properly, "stdint.h") header to be used with pre-C99 compilers. See
<http://www.lysator.liu .se/c/q8/> for a public domain implementation.

Note that __STDC_VERSION_ _ >= 199901L doesn't necessarily imply that
the <stdint.h> header *doesn't* exist. Some compilers might provide
partial C99 support.

--
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.
Nov 15 '05 #3
Walter Roberson wrote:

In article <42************ ***@spamcop.net >,
Kenneth Brody <ke******@spamc op.net> wrote:
Is there any way to know if there is a typedef of a given name?
Specifically , I need to know if the compiler has a 64-bit integer type,
and need to know if "int64_t" exists.


There is no portable way to test that in C89 (I'm not familiar enough
with C99 to know if it were extended in that regard, but I suspect not.)


I can't depend on C99 being available, so that's not really relevent.
The usual way to handle this is to have a meta level that tests
for the existance of appropriate types or functions and sets
preprocessing tokens appropriately in a constructed include file
that the rest of the code imports.


Well, we already have a bunch of config.h files for different platforms
to define such things native byte order (yes, some parts of the code need
to know that), whether "void" is available (we used to run on platforms
that had no "void" type), which type of varargs are needed, and so on. I
was just hoping to avoid yet another entry.

I guess I'll probably end up going with "assume there's an int64_t type
unless the config file defines NO_INT64_T", or something like that.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 15 '05 #4
Kenneth Brody <ke******@spamc op.net> writes:
[...]
Well, we already have a bunch of config.h files for different platforms
to define such things native byte order (yes, some parts of the code need
to know that), whether "void" is available (we used to run on platforms
that had no "void" type), which type of varargs are needed, and so on. I
was just hoping to avoid yet another entry.
Looks like you'll need yet another entry.
I guess I'll probably end up going with "assume there's an int64_t type
unless the config file defines NO_INT64_T", or something like that.


You may find that giving a flag a negative name will cause confusion.
I find thinks like
#ifdef NO_INT64_T
or
#if !defined(NO_INT _64_T)
more difficult to read than
#ifdef HAS_INT64_T
or
#if defined(HAS_INT _64_T)

--
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.
Nov 15 '05 #5


Kenneth Brody wrote:
Is there any way to know if there is a typedef of a given name?
Not directly, no.
Specifically, I need to know if the compiler has a 64-bit integer type,
and need to know if "int64_t" exists. Something like this pseudo-code:

#if typedef(int64_t )
typedef int64_t MY_BIG_INT
#elif typedef(long long)
typedef long long MY_BIG_INT
#else
typedef long MY_BIG_INT
#endif

(Yes, this program needs to work on systems which don't have a 64-bit
integers, and it needs to take advantage of them if they are there.)
`#if __STDC_VERSION_ _ >= 199901L' means "C99 or later"
and implies the existence of the <stdint.h> header. Having
included this header, you can then check for the presence
of macros like INT64_MAX or UINT_LEAST64_MA X (depending on
whether you want signed or unsigned, exactly or at least 64
bits).

If you don't have C99, you still might be lucky: Some C90
implementations provide a 64-bit `long'. You can check by
including <limits.h> and inspecting the values of LONG_MAX
and/or ULONG_MAX (with care; see below).

Finally, some pre-C99 implementations provide `long long'
if invoked in a non-conforming mode. If yours does so and if
it supports `long long' in the C99 style, <limits.h> will
define LLONG_MAX and ULLONG_MAX for you to check.

Putting it all together, you'd get something like this
(the indirect value tests cater to C90 preprocessors, which
might not be able to parse longer-than-`long' numbers):

#if __STDC_VERSION_ _ >= 199901L
#include <stdint.h>
#ifdef INT_LEAST64_MAX
typedef int_least64_t MyBigInt;
#define MYBIGBITS 64
#endif
#endif

#ifndef MYBIGBITS
#include <limits.h>
#if (LONG_MAX >> 31) >> 31 >= 1
typedef long MyBigInt;
#define MYBIGBITS 64
#elif (LLONG_MAX >> 31) >> 31 >= 1
/* non-conforming but helpful C90 */
typedef long long MyBigInt;
#define MYBIGBITS 64
#else
/* oh, well -- better luck next time */
typedef long MyBigInt;
#define MYBIGBITS 32
#endif
#endif
Also, what is the standard include file which would be needed to have
the int64_t typedef included? I see it in <stdint.h> on one compiler
I have, and <native.h> on another. I don't see these headers being
included by other standard headers on these systems.


C99 specifies <stdint.h> but not <native.h>. C90 specifies
neither, so don't try to #include it until you've established
that you've got a C99 implementation.

--
Er*********@sun .com

Nov 15 '05 #6
Eric Sosman wrote:
[...]
`#if __STDC_VERSION_ _ >= 199901L' means "C99 or later"
and implies the existence of the <stdint.h> header. Having
included this header, you can then check for the presence
of macros like INT64_MAX or UINT_LEAST64_MA X (depending on
whether you want signed or unsigned, exactly or at least 64
bits).

[...]

Thanks for the pointers.

However, what happens when __STDC_VERSION_ _ isn't defined at all? (I
suppose this obviously means "not C99".) Neither "cc" under SCO Unix,
nor MSVC 6.0 define this. (I haven't tried any other platforms yet.)

That's 2 underscores, "STDC", one underscore, "VERSION", two underscores.
Correct?

In any case, I'm going to be stuck with "yet another config item", as it
doesn't look like all the systems we run on have enough to automatically
determine it.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 15 '05 #7
Kenneth Brody wrote:
Is there any way to know if there is a typedef of a given name?
No.
Specifically, I need to know if the compiler has a 64-bit integer type,
and need to know if "int64_t" exists. Something like this pseudo-code:

#if typedef(int64_t )
typedef int64_t MY_BIG_INT
#elif typedef(long long)
"long long", on systems which have it, won't *be* a type def.
typedef long long MY_BIG_INT
#else
typedef long MY_BIG_INT
#endif

(Yes, this program needs to work on systems which don't have a 64-bit
integers, and it needs to take advantage of them if they are there.)
For that you obviously need rather more work than merely using the right
type.
Also, what is the standard include file which would be needed to have
the int64_t typedef included? I see it in <stdint.h> on one compiler
I have, and <native.h> on another. I don't see these headers being
included by other standard headers on these systems.


stdint.h is part of the C99 standard. Unfortunately most implementations
do not fully meet C99. However, it is simple to write an stdint.h for
any systems that don't have it.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #8


Kenneth Brody wrote:
Eric Sosman wrote:
[...]
`#if __STDC_VERSION_ _ >= 199901L' means "C99 or later"
and implies the existence of the <stdint.h> header. Having
included this header, you can then check for the presence
of macros like INT64_MAX or UINT_LEAST64_MA X (depending on
whether you want signed or unsigned, exactly or at least 64
bits).
[...]

Thanks for the pointers.

However, what happens when __STDC_VERSION_ _ isn't defined at all? (I
suppose this obviously means "not C99".) Neither "cc" under SCO Unix,
nor MSVC 6.0 define this. (I haven't tried any other platforms yet.)


An identifier that isn't defined as a macro evaluates
as zero for the purposes of #if. 6.10.1/2:

"[...] After all replacements due to macro expansion
and the defined unary operator have been performed,
all remaining identifiers are replaced with the pp-
number 0, [...]"

(Personally, I wish the ANSI committee had not defined the
language this way; the "anything unrecognized is zero" rule
makes spellnig errors potentially more damaging. However,
that particular horse is no longer proximal to the barn.)
That's 2 underscores, "STDC", one underscore, "VERSION", two underscores.
Correct?


Correct. See 6.10.8/1.

--
Er*********@sun .com

Nov 15 '05 #9
Eric Sosman wrote:

Kenneth Brody wrote:
Eric Sosman wrote:
[...]
`#if __STDC_VERSION_ _ >= 199901L' means "C99 or later"
[...] However, what happens when __STDC_VERSION_ _ isn't defined at all? (I
suppose this obviously means "not C99".) Neither "cc" under SCO Unix,
nor MSVC 6.0 define this. (I haven't tried any other platforms yet.)


An identifier that isn't defined as a macro evaluates
as zero for the purposes of #if. 6.10.1/2:


Well, that helps for "#if __STDC_VERSION_ _ >= 199901L", but it doesn't
help my test of printf("%ld\n", __STDC_VERSION_ _);. ;-)

[...]
That's 2 underscores, "STDC", one underscore, "VERSION", two underscores.
Correct?


Correct. See 6.10.8/1.


Thanks. I just wanted to make sure I didn't have a typo.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 15 '05 #10

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

Similar topics

2
3629
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
14
4631
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further explanations, so I wonder is there any good using typedef ? and I also know that when a data type being typedefed become an abstract data type, so what exactly is an abstract data type, is it any good ? -- Posted via http://dbforums.com
4
3051
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum { DOUBLE_LIST, INT_LIST } DATA_TYPE; typedef struct { DATA_TYPE type;
15
5654
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing everything in a /*...*/ comment and appending the 'hail world' code. Is there an easier, and, let's say, more
16
3827
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
12
15635
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
6
7318
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if (condition) typedef char T; #else typedef short T;
15
2561
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! ) what is it supposed to do ?
12
4642
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int (*(*(*ptr2d_fptr)()))();
16
2770
by: mdh | last post by:
A quick ? :-) question about Typedefs. There is a very brief discussion about this in K&R ( p146). Googling this group, there is a surprising dearth of questions about these. From one of the threads, there is sound advice ( to me at any rate) not to hide pointers behind typedefs. So, may I ask the group when the use of typedefs really makes sense? Sorry if this is somewhat general, but there are no exercises ( not that I am asking for...
0
8546
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
9090
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
7635
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
6462
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
5822
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
4327
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
4563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2981
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
3
1962
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.