P
Pedro Graca
Is there a way (preprocessor magic, perhaps ???) to use a type with
twice as many bits as `unsigned` (or `int`) with no changes to the
source code?
====
#define SINGLE_TYPE unsigned
#define DOUBLE_TYPE ???
====
I tried the following, and it worked in 2 different computers, with 2
different results.
#include <limits.h>
#include <stdio.h>
#define SINGLE_TYPE unsigned
#if UINT_MAX < ULONG_MAX / UINT_MAX
# define DOUBLE_TYPE unsigned long
# define DOUBLE_TYPE_STRING "unsigned long"
#else
# if UINT_MAX < ULLONG_MAX / UINT_MAX
# define DOUBLE_TYPE unsigned long long
# define DOUBLE_TYPE_STRING "unsigned long long"
# else
# error No available "DOUBLE_TYPE"
# endif
#endif
int main(void) {
printf("sizeof(SINGLE_TYPE) is %d; sizeof(DOUBLE_TYPE) is %d\n",
(int)sizeof(SINGLE_TYPE), (int)sizeof(DOUBLE_TYPE));
printf("DOUBLE_TYPE is \"%s\"\n", DOUBLE_TYPE_STRING);
return 0;
}
Is the above guaranteed to work, provided one of `unsigned long` or
`unsigned long long` is at least twice as large as `unsigned`?
It worked for me in two different compilers, but I'm not confident on
the preprocessing calculations.
(And, yes!, I realize `unsigned long long` is not defined by C89, but
I couldn't find the option to use only C89 in the compiler where I
needed that type)
twice as many bits as `unsigned` (or `int`) with no changes to the
source code?
====
#define SINGLE_TYPE unsigned
#define DOUBLE_TYPE ???
====
I tried the following, and it worked in 2 different computers, with 2
different results.
#include <limits.h>
#include <stdio.h>
#define SINGLE_TYPE unsigned
#if UINT_MAX < ULONG_MAX / UINT_MAX
# define DOUBLE_TYPE unsigned long
# define DOUBLE_TYPE_STRING "unsigned long"
#else
# if UINT_MAX < ULLONG_MAX / UINT_MAX
# define DOUBLE_TYPE unsigned long long
# define DOUBLE_TYPE_STRING "unsigned long long"
# else
# error No available "DOUBLE_TYPE"
# endif
#endif
int main(void) {
printf("sizeof(SINGLE_TYPE) is %d; sizeof(DOUBLE_TYPE) is %d\n",
(int)sizeof(SINGLE_TYPE), (int)sizeof(DOUBLE_TYPE));
printf("DOUBLE_TYPE is \"%s\"\n", DOUBLE_TYPE_STRING);
return 0;
}
Is the above guaranteed to work, provided one of `unsigned long` or
`unsigned long long` is at least twice as large as `unsigned`?
It worked for me in two different compilers, but I'm not confident on
the preprocessing calculations.
(And, yes!, I realize `unsigned long long` is not defined by C89, but
I couldn't find the option to use only C89 in the compiler where I
needed that type)