Difference between revisions of "C datatypes"

From Teknologisk videncenter
Jump to: navigation, search
m
m (On a 64 bit system)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=Int and long int=
 
=Int and long int=
 +
See the file limits.h for details - Properly at /usr/include/limits.h
 
<source lang=c>
 
<source lang=c>
 
#include <stdio.h>
 
#include <stdio.h>
Line 28: Line 29:
 
</source>
 
</source>
 
==Compiling and running==
 
==Compiling and running==
 +
===On a 64 bit system===
 
<source lang=bash>
 
<source lang=bash>
heth@mars:~/bin$ gcc -v
 
gcc version 4.4.5 (Ubuntu/Linaro 4.4.5-15ubuntu1)
 
heth@mars:~/bin$ uname -a
 
Linux mars 2.6.35-24-generic #42-Ubuntu SMP Thu Dec 2 02:41:37 UTC 2010 x86_64 x86_64 x86_64 GNU/Linux
 
 
heth@mars:~/bin$ gcc types.c -o types
 
heth@mars:~/bin$ gcc types.c -o types
 
heth@mars:~/bin$ ./types
 
heth@mars:~/bin$ ./types
 +
Wordsize us 64
 +
 
The number of bits in a byte 8
 
The number of bits in a byte 8
 
The minimum value of SIGNED CHAR = -128
 
The minimum value of SIGNED CHAR = -128
Line 47: Line 47:
 
The minimum value of LONG = -9223372036854775808
 
The minimum value of LONG = -9223372036854775808
 
The maximum value of LONG = 9223372036854775807
 
The maximum value of LONG = 9223372036854775807
heth@mars:~/bin$
 
 
</source>
 
</source>
  
 +
===On a 32 bit system===
 +
<source lang=bash>
 +
heth@mars:~/bin$ gcc types.c -o types
 +
heth@mars:~/bin$ ./types
 +
Wordsize is 32
 +
 +
The number of bits in a byte 8
 +
The minimum value of SIGNED CHAR = -128
 +
The maximum value of SIGNED CHAR = 127
 +
The maximum value of UNSIGNED CHAR = 255
 +
The minimum value of SHORT INT = -32768
 +
The maximum value of SHORT INT = 32767
 +
The minimum value of INT = -2147483648
 +
The maximum value of INT = 2147483647
 +
The minimum value of CHAR = 0
 +
The maximum value of CHAR = 255
 +
The minimum value of LONG = -2147483648
 +
The maximum value of LONG = 2147483647
 +
The minimum value of LONG = -2147483648
 +
The maximum value of UNSIGNED LONG = 2147483647
 +
</source>
 
[[Category:C]]
 
[[Category:C]]

Latest revision as of 10:17, 27 November 2022

Int and long int

See the file limits.h for details - Properly at /usr/include/limits.h

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

int main() {
   printf("Wordsize is %d\n\n", __WORDSIZE);
   printf("The number of bits in a byte %d\n", CHAR_BIT);

   printf("The minimum value of SIGNED CHAR = %d\n", SCHAR_MIN);
   printf("The maximum value of SIGNED CHAR = %d\n", SCHAR_MAX);
   printf("The maximum value of UNSIGNED CHAR = %d\n", UCHAR_MAX);

   printf("The minimum value of SHORT INT = %d\n", SHRT_MIN);
   printf("The maximum value of SHORT INT = %d\n", SHRT_MAX); 

   printf("The minimum value of INT = %d\n", INT_MIN);
   printf("The maximum value of INT = %d\n", INT_MAX);

   printf("The minimum value of CHAR = %d\n", CHAR_MIN);
   printf("The maximum value of CHAR = %d\n", CHAR_MAX);

   printf("The minimum value of LONG = %ld\n", LONG_MIN);
   printf("The maximum value of LONG = %ld\n", LONG_MAX);
  
   return(0);
}

Compiling and running

On a 64 bit system

heth@mars:~/bin$ gcc types.c -o types
heth@mars:~/bin$ ./types
Wordsize us 64

The number of bits in a byte 8
The minimum value of SIGNED CHAR = -128
The maximum value of SIGNED CHAR = 127
The maximum value of UNSIGNED CHAR = 255
The minimum value of SHORT INT = -32768
The maximum value of SHORT INT = 32767
The minimum value of INT = -2147483648
The maximum value of INT = 2147483647
The minimum value of CHAR = -128
The maximum value of CHAR = 127
The minimum value of LONG = -9223372036854775808
The maximum value of LONG = 9223372036854775807

On a 32 bit system

heth@mars:~/bin$ gcc types.c -o types
heth@mars:~/bin$ ./types
Wordsize is 32

The number of bits in a byte 8
The minimum value of SIGNED CHAR = -128
The maximum value of SIGNED CHAR = 127
The maximum value of UNSIGNED CHAR = 255
The minimum value of SHORT INT = -32768
The maximum value of SHORT INT = 32767
The minimum value of INT = -2147483648
The maximum value of INT = 2147483647
The minimum value of CHAR = 0
The maximum value of CHAR = 255
The minimum value of LONG = -2147483648
The maximum value of LONG = 2147483647
The minimum value of LONG = -2147483648
The maximum value of UNSIGNED LONG = 2147483647