# CSCI 255 Data Types vol. 1

## Integer data types of Java

• `byte`
• 8-bit signed two's complement
• Range: -128 to 127
• `short`
• 16-bit signed two's complement
• Range: -32768 to 32767
• `int`
• 32-bit signed two's complement
• Range: -2147483648 to 2147483647
• `long`
• 64-bit signed two's complement
• Range: -9223372036854775808 to 9223372036854775807

The `char` data type is designed for Unicode characters.

## Integer data types of C

Quotes are from The C Programming Language by Kernighan and Ritchie.

## ANSI C

• `char`
• Can store "any member of the execution character set"
• Can be `signed` or `unsigined`
• The "plain" `char` may be either `signed` or `unsigined`
• `int`
• Has the "natural size suggested by the host machine architecture"
• Can be `short` or `long`
• Can be `signed` or `unsigined`
• The "plain" `int` is `signed`

Arithmetic with unsigned integers "obey the laws of arithmetic modulo 2n". Because signed integers are not required to be two's complement, many of their operations do not have a tight formal definition. For example, -5/2 could be either -2 or -3. If -5/2 is -2 then -5%2 must be -1, and if -5/2 is -3 then -5%2 must be 1; that is (-5/2)*2+5%2 must be 5.

The ANSI C standard requires that both the `short int` and the plain `int` be able to hold integers from -32767 to 32767 when `signed` and from 0 to 65535 when `unsigned`. The `long int` must be able to hold integers from -2147483647 to 2147483647 when `signed` and from 0 to 4294967295 when `unsigned`.

## C99

The C99 standard includes a `long long` integer which is required to hold at least 64 bits.

## Solutions through includes

• `<limits.h>`: defines constants, e.g., `INT_MAX` for minimum and maximum values
• A program to print limits
```#include <limits.h>
#include <stdio.h>

int main(void) {
printf("Largest char  is %d\n",  SCHAR_MAX) ;
printf("Largest short is %d\n",  SHRT_MAX) ;
printf("Largest int   is %d\n",  INT_MAX) ;
printf("Largest long  is %ld\n", LONG_MAX) ;
return 0 ;
}
```
• Run on a 32-bit computer
```Largest char  is 127
Largest short is 32767
Largest int   is 2147483647
Largest long  is 2147483647
```
• Run on a 64-bit computer
```Largest char  is 127
Largest short is 32767
Largest int   is 2147483647
Largest long  is 9223372036854775807
```
• `<stdint.h>` in C99 and most recent C compilers: provides `typedef`'s with size specific names, e.g., `int32_t` and `uint16_t`
• `<pic24_generic.h>` of the textbook: provides `typedef`'s with size specific names, e.g., `int32` and `uint16`