delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/24/12:27:44

Date: Thu, 24 Jul 1997 09:23:59 -0700 (PDT)
Message-Id: <199707241623.JAA27109@adit.ap.net>
Mime-Version: 1.0
To: crough45 AT amc DOT de
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: sizeof() in #if
Cc: chirayu AT radiolink DOT net, djgpp AT delorie DOT com

You wrote:
>Chirayu Krishnappa wrote:
>> 
>> >However, all is not lost.  There is a very useful header file 
>> >defined in the ANSI standard which contains things about the sizes
>> >of variables.  It's called limits.h.  Among the things it defines
>> 
>> Thanks! I'm using this approach now.
>
>The only problem with it is that it's clumsy and in CS terms "not elegant".
>I would really like some 'elegant' code to do it - as it is, I go with what
>works...
I have seen programs which attempt to be ultraportable work by creating
their own definitions before compiling. Example:
- Have a file sizes.c that goes:

#include <stdio.h>

int main(void)
{
   if (sizeof(long) == 4)
     {
      printf("#define LONGSIZE 4\n");
      printf("typedef long int32\n");
     }
   /* And so on... */
}

- Then, in your makefile:

sizes : sizes.c
        gcc -o sizes sizes.c

sizes.h : sizes
        sizes >sizes.h

- And have everything that needs the sizes #include sizes.h, and put the
appropriate dependencies in the makefile.
You can take care of many machine dependencies this way. I've seen it done
for endian-ness as well.
HTH

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019