9

From the manpage man limits.h:

       {WORD_BIT}
             Number of bits in an object of type int.
             Minimum Acceptable Value: 32

However if I run a simple program:

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

int main() {
  printf("%d\n", WORD_BIT);
  return 0;
}

However when trying to compile with gcc (gcc file.c -o file) I get the following:

error: ‘WORD_BIT’ undeclared (first use in this function)

How come this is not defined on my system, and where else can I find this information (in a C program)?

My system:

  • Fedora 36 (Silverblue)
  • gcc version 12.2.1 20220819 (Red Hat 12.2.1-1) (GCC)
  • ldd (GNU libc) 2.35
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
rahmu
  • 19,673
  • 28
  • 87
  • 128

2 Answers2

16

I’m not sure this is documented, but with the GNU C library you need to set _GNU_SOURCE to get WORD_BIT:

$ gcc -include limits.h -E - <<<"WORD_BIT" | tail -n1
WORD_BIT

$ gcc -D_GNU_SOURCE -include limits.h -E - <<<"WORD_BIT" | tail -n1
32

You should really use sysconf:

#include <unistd.h>
#include <stdio.h>

int main(int argc, char **argv) {
  printf("%ld\n", sysconf(_SC_WORD_BIT));
}

or, as recommended in the GNU C library documentation:

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

int main(int argc, char **argv) {
#ifdef WORD_BIT
  printf("%d\n", WORD_BIT);
#else
  printf("%ld\n", sysconf(_SC_WORD_BIT));
#endif
}

(As for why this appears in man limits.h, this man page is the POSIX reference for limits.h, and doesn’t necessarily document the C library on your system exactly. You can see this by looking at the section of the man page — the “P” suffix indicates that it’s POSIX documentation.)

You could use sizeof(int) * CHAR_BIT instead; CHAR_BIT is always defined. What’s more, POSIX extends the C standard and specifies that CHAR_BIT is 8, so if you assume POSIX, you could use sizeof(int) * 8.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
4

The gcc has a set of builtin defines. The one you looking for is __SIZEOF_INT__

Full list of defines is in the documentation:
https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html

The pro-con of using these instead of those defined in limits.h is that the defines built into compiler are built into compiler - they are absolutely correct for this particular platform but are tied to this particular brand of compilers.

But for size of the int it is much easier to use sizeof(int).

White Owl
  • 4,511
  • 1
  • 4
  • 15
  • 5
    But `sizeof` (and ``__SIZEOF_x__``) values are numbers of bytes,  and the OP seems to want number of *bits.* – G-Man Says 'Reinstate Monica' Sep 01 '22 at 21:31
  • 2
    `sizeof(int) * CHAR_BIT` then. – zwol Sep 02 '22 at 01:34
  • There's also `__INT_WIDTH__`. – iBug Sep 02 '22 at 07:25
  • 3
    Regarding `__INT_WIDTH__`, note the following from the documentation: “Defined to the bit widths of the corresponding types. They exist to make the implementations of `limits.h` and `stdint.h` behave correctly. You should not use these macros directly; instead, include the appropriate headers. Some of these macros may not be defined on particular systems if GCC does not provide a `stdint.h` header on those systems.” – Stephen Kitt Sep 02 '22 at 08:21