- size of basic data types in C
- 5 Answers 5
- C++ Data Types Sizes/Ranges Display on Linux
- C++ Data Types Sizes/Ranges Display on Linux
- Data Types in C with their size and range
- Data types ranges and their macros in C++
- Example
- Example
- Output
- Data types size in c
- how to print sizes of various data types of C
- Why are the long and long double type sizes different between Linux and Windows?
- Possible to find out the sizes of data types (int, float, double, . ) on a system, without writing a C program?
- 7 Answers 7
size of basic data types in C
The size of float and double must be at least 4 and 8 respectively. 2
@SecurityMatt @Hot Licks No. char can have more than 8 bits, so sizeof(int) == 1 is possible, e.g. on TI 28Cxx where both have 16 bits.
5 Answers 5
My question is if I were to run the same program on a 32-bit machine will I see different results
Or in other words does the size of the basic data types depend on 1) processor 2) Operating System 3) anything else
- Yes, 2. yes, 3. yes, for example if you run a 32-bit app in 32-bit compatibility mode on a 64-bit OS, then it most likely will use a 32-bit word size (of course, it was compiled like that). Oh, and yes, it may depend on the compiler too.
«And your compiler flags. « (Thanks, Kay!)
Subject to having to install some libraries [probably just glibc] in it’s 32-bit variant, you should be able to try this yourself by using gcc -m32 myprog.c [or clang -m32 myprog.c ].
However, the only thing of your items that have been listed that will change if you move from a 64-bit x86 linux system to 32-bit x86 linux system, using gcc-based compilers, is the size of long . Note the heavy qualification of x86, gcc, etc — compilers have a lot of freedom. Someone could write a compiler for Linux that uses 16-bit int and 64-bit long on a 32-bit system with no huge amount of difficulty. Using that compiler to compile the Linux kernel and many of the Linux tools would probably fail [most likely including compiling gcc with that compiler]. But you can’t really say «on this architecture» or «in this OS» or «with this compiler» . without also qualifying what the OTHER parameters are.
Case in point: A Microsoft C/C++ compiler has a long that is 32 bit even on 64-bit systems. Why, I hear you ask? Because a large number of Windows API functions use long as a 32-bit value as legacy from when Windows was a 16-bit OS on Intel 286/386 processors. Since (some of) the system calls are backwards compatible a very long way in Windows, code that is written for 16-bit systems will still work on 64-bit Windows [unless the code is using some really unusual system calls, and of course, the STYLE will look a bit ancient]. Changing long to a 64-bit value would have broken some of that functioanilty, so the compiler guys at MS decided to stick with long = 32 bit. If you want 64-bit integers, you have to use long long or int64_t or something else, not long . Of course, this breaks some code that assumes that sizeof(long) == sizeof(void *) . Hopefully, most such code has already been fixed.
C++ Data Types Sizes/Ranges Display on Linux
Solution 3: If c++ take a look at std class numeric_limits Given the task is to find the memory range of the different data types, that what range of value a data type can store the value from minimum value to maximum value. Example ‘char’(signed) is character data type and has a range of -128 to +128 and macro for minimum value, macro to find the range values of char data type is CHAR_MIN and CHAR_MAX.
C++ Data Types Sizes/Ranges Display on Linux
I’m running Linux and frequently find myself wondering what the storage sizes and numeric ranges are for the basic data types (signed/unsigned char, signed/unsigned long, signed/unsigned long double, et cetera).
I’m hoping there’s a little command line program which prints all this, though, if not, I realize that I could build it. (The ascii command, for instance, is very useful for a similar purpose.)
You could just vim through limits.h . For instance, on my machine it starts with:
/* Number of bits in a `char'. */ # define CHAR_BIT 8 /* Minimum and maximum values a `signed char' can hold. */ # define SCHAR_MIN (-128) # define SCHAR_MAX 127
Or if you feel you need a separate program, build one that simply prints things like: CHAR_BIT , SCHAR_MIN etc.
sizeof(type) returns the size of the type , basic or not.
If c++ take a look at std class numeric_limits
C data types and si Code Example, C queries related to “c data types and si” unsigned nteger range in c; c char byte size; memory sizes of variables c; int long; size of all data types in c; nteger in c; …
Data Types in C with their size and range
Data Types in C with their sizes and rangesWatch LATEST FULL POINTER SERIES PLAYLIST in easiest …
Data types ranges and their macros in C++
Given the task is to find the memory range of the different data types, that what range of value a data type can store the value from minimum value to maximum value. There is memory range of data type from in which value of data can be stored. It is difficult to remember the large range of value so C++ has macros for represent these numbers, from macros these large numbers can be directly assigned to variable without typing the whole Number range.
Example
‘char’(signed) is character data type and has a range of -128 to +128 and macro for minimum value, macro to find the range values of char data type is CHAR_MIN and CHAR_MAX.
Likewise we can find range of any data type by these MIN and MAX macros like for ‘int’ we can use INT_MIN and INT_MAX.
Range of char
Range of int
Example
C++ code to demonstrate the macros of data types
#include #include #include Using namespace std; int main( )
Output
If we run the above code it will generate the following output −
Range of char: -128 to 127 Range of int: -2147483648 to 2147483648 Range of float: 1.17549e-38 to 3.40282e+38 Range of double: 2.22507e-308 to 1.79769e+308 Range of Short char: -128 to 127 Range of Unsigned Char: 0 to 255 Range of long int: -922337203685477580 to 922337203685477580 Range of Unsigned int: 0 to 42944967295 Range of Short int: -32768 to 32767 Range of float negative: -1.17549e-38 to -3.40282e+38 Range of double negative: 2.22507e-308 to 1.79769e+308
C Data Types, In C programming, data types are declarations for variables. This determines the type and size of data associated with variables. For example, int myVar; Here, …
Data types size in c
how to print sizes of various data types of C
#include #include int main(void)
Java Data Types, Data types are divided into two groups: Primitive data types — includes byte, short, int, long, float, double, boolean and char; Non-primitive data types — such as …
Why are the long and long double type sizes different between Linux and Windows?
I am learning about computer architecture now. Why the data type sizes are different between Linux and Windows operating systems?? This is my sample C code.
when i compiled and run this code on my windwos 10 64bit with visual studio 10, it show like: then, when i compiled and run this code on my ubuntu 18.04 64bit. it shows like: The Windows system shows me the size of long double is 8-byte and long is 4-byte. But Ubuntu shows me the size of long double is 16-byte and long is 8-byte. Why are the two sizes different? Both have 64-bit environments, and I wonder why they have different values. I’d appreciate it if you could explain.
An aside, did you know that you can simply use «%zu» as a format specifier for size_t ? (To avoid the cast to int )
Sizes are determined by the C implementation, not by the operating system or the hardware. General-purpose C implementations are largely influenced by the operating system and the hardware—key types, such as int , are normally chosen to be good for performance with the hardware and for interfacing with operating system features. But some types are flexible. E.g., one C implementation might choose to implement an extended-precision floating-point type for long double while another might choose not to make that effort and leave long double the same as double …
… Or one C implementation might choose to use the old Intel 80-bit floating-point features for long double while another decides the performance of that is not worth it, especially if it interferes with modern features. And special-purpose C implementations, such as one designed to facilitate running old code for a particular environment, might largely ignore the operating system and hardware and make the sizes what they need to be to match the old code. You can write compiler software to make the types any sizes, so choosing sizes is a matter of satisfying your goals for the compiler.
Possible to find out the sizes of data types (int, float, double, . ) on a system, without writing a C program?
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?
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?
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.
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 .
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 — 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.
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.
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 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:
Yes. You could scan /usr/include//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 */