Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it possible to find out the sizes of data types (int, float, double, ...) on a Linux system, without writing a C program?

Would the results for C same as for C++, and other programming languages in the same Linux system?

share|improve this question
3  
I am curious -- if you don't intend to write a C program, what difference does it make if C data types are one size rather than another? –  David Cary Feb 14 at 17:31
3  
@DavidCary For calling ABI's from a language other than C! –  Kaz Feb 14 at 22:47

7 Answers 7

If you know the definition of the data type you want you can use getconf to find these values out on most Unix systems.

$ getconf CHAR_BIT
8

The list of variables is defined in the man page man limits.h as well as here, man sysconf, in addition to being on disk. You can use locate limits.h to find it, it's often here: /usr/include/linux/limits.h.

share|improve this answer
4  
With the caveat that this applies only to the platform's official C compiler. There may be alternative compilers, or alternative configurations (typically through command line options) of the official compiler, that lead to different sizes. –  Gilles Feb 14 at 17:26
    
@Gilles - have you ever seen a way to actually list these variables out? I've been looking and cannot for the life of me find a tool that can do this. Seems like there would be. Also I was under the impression that getting these values through getconf was the safest way, so long as you say, I'm hitting "the" official compiler on the box. –  slm Feb 14 at 17:52
2  
The reliable way — and the way people use when they care, which is by and large when they want to compile a C program — is to compile a small C program. See how autoconf operates. getconf isn't so safe, unless you're calling the C compiler as c89 or c99 with (almost) no option. –  Gilles Feb 14 at 19:17

Yes. You could scan /usr/include/<arch>/limits.h

For example, on my NetBSD amd64, /usr/include/amd64/limits.h would show:

#define CHAR_BIT        8               /* number of bits in a char */

#define SCHAR_MAX       0x7f            /* max value for a signed char */
#define SCHAR_MIN       (-0x7f-1)       /* min value for a signed char */

#define UCHAR_MAX       0xff            /* max value for an unsigned char */
#define CHAR_MAX        0x7f            /* max value for a char */
#define CHAR_MIN        (-0x7f-1)       /* min value for a char */

#define USHRT_MAX       0xffff          /* max value for an unsigned short */
#define SHRT_MAX        0x7fff          /* max value for a short */
#define SHRT_MIN        (-0x7fff-1)     /* min value for a short */

#define UINT_MAX        0xffffffffU     /* max value for an unsigned int */
#define INT_MAX         0x7fffffff      /* max value for an int */
#define INT_MIN         (-0x7fffffff-1) /* min value for an int */

#define ULONG_MAX       0xffffffffffffffffUL    /* max value for an unsigned long */
#define LONG_MAX        0x7fffffffffffffffL     /* max value for a long */
#define LONG_MIN        (-0x7fffffffffffffffL-1)        /* min value for a long */
share|improve this answer
3  
This often works, but sometimes different compilers or compiler settings will lead to different sizes. –  Gilles Feb 14 at 17:27

Kind of.

With gcc at least, this works:

$ cpp -dD /dev/null | grep __SIZEOF_LONG__

Anyway, why don't you want to write a C program to do it? You could send a tiny C program to your compiler from the shell something like this:

binary=$(mktemp)
cat <<\EOF | cc -o $binary -x c -
#include <stdio.h>
int main() {
    printf("int=%lu bytes\n", sizeof(int));
    printf("long=%lu bytes\n", sizeof(long));
}
EOF
$binary
rm $binary

The -x c tells the compiler the language is C, and the - means read from standard input.

On my system, the above prints:

int=4 bytes
long=8 bytes

Tested in gcc and clang.

share|improve this answer

If you have perl installed you can get this from perl -V:

intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=8, prototype=define
share|improve this answer

Sizes of data types are a property of a compiler (or ABI), not of the system. You can have multiple compilers using different sizes for data types on the same system.

share|improve this answer

No... it is possible to run binaries with different ideas of the sizes of basic types, particularly on 64 bit architectures. Recent Linux kernels on x86_64 can run native 32 bit binaries, and there is the x32 ABI with 32 bit types.

The data type sizes are partly what the compiler uses. But it is clearly advantageous to (1) use types that the machine supports efficiently and (2) use types consistently from the low-level libraries through user applications. Having to handle several variants is just a mess.

share|improve this answer

Try this to parse and output the lines containing the strings referencing the data types:

{ shopt -s globstar; for i in /usr/include/**/*.h; do grep -HE '\b(([UL])|(UL)|())LONG|\bFLOAT|\bDOUBLE|\bINT' $i; done; }

This catches of course the definitions in /usr/include/limits.h so you'll get that plus more, sometimes with values, but mostly referencing what is set in limits.h which you can conveniently look at with the getconf -a and ulimit -a commands.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.