Primitive (built-in) data types in C that are also in Java

Syntax Name Java's counterpart Usage
char character char/byte Usually stores a character (= ASCII code). It can also store a very short integer (−128..127)
short short integer short Store short range of integer values between −32768 and 32767. Uses 2 byte memory.
int (default) integer int Store integer values between −2147483648 and 2147483647. Uses 4 byte memory,
long long integer long Store larger range of integer value between −9223372036854775808 and 9223372036854775807. Uses 8 bytes memory
float single precision float float Store single precision floating point values. Uses 4 byte memory,
double double precision float double Store double precision floating point values. Uses 4 byte memory,
Any number type boolean boolean 0 ≡ false and not 0 ≡ true

How to find the range limit of each data type in C

  • The C library's header file limits.h contains definitions of the limits of the primitive data types:

     CHAR_MIN = smallest value for char   CHAR_MAX = largest value for char 
     SHRT_MIN = smallest value for short  SHRT_MAX = largest value for short 
     INT_MIN  = smallest value for int    INT_MAX  = largest value for int
     and so on (look in /usr/include/limits.h for other constants)
    

  • Example: using limits.h

    #include <stdio.h>
    #include <limits.h>
    
    int main( int argc, char* argv[] )
    {
       printf( "CHAR_MIN = %d, CHAR_MAX = %d\n", CHAR_MIN, CHAR_MAX);
    
       printf( "SHRT_MIN = %d, SHRT_MAX = %d\n", SHRT_MIN, SHRT_MAX);
    
       printf( "INT_MIN = %d, INT_MAX = %d\n", INT_MIN, INT_MAX);
    }  

Data types in C that are not found in Java

Syntax Name Usage
unsigned char Unsigned character Stores very small positive integer value between 0 and 255
unsigned short Unsigned short integer Stores small positive integer value between 0 and 65535
unsigned int Unsigned ordinary integer Stores default positive integer value between 0 and 4294967295
unsigned long Unsigned long integer Stores long positive integer value between 0 and 18446744073709551615
* datatype Reference type Stores a memory reference (= address) address (uses 4 bytes in 32 bits machine and 8 bytes in newer 64 bits machines)

C's unsigned integer (whole) data types

  • Recall that the unsigned integer data type uses the binary number system as coding method:

  • Therefore:

    • The value of a binary number stored in an unsigned integer data type in a C program will be computed using the binary number system

C's unsigned integer (whole) data types

  • Example that shows the difference between a signed (default) char and an unsigned char variable:

    int main( int argc, char* argv[] )
    {
        char x          = 0b11111111;  // 11111111 ---> -1  (2s compl code)
        unsigned char y = 0b11111111;  // 11111111 ---> 255 (Bin Num Sys)
    
        printf("x = %d\n", x);
        printf("y = %d\n", y);
    
        if ( x < 0 )
           printf("x is negative\n");
        else
           printf("x is positive\n");
    
        if ( y < 0 )
           printf("y is negative\n");
        else
           printf("y is positive\n"); 
    
    } 

DEMO: /home/cs255001/demo/C/set1/unsigned1.c