Much of the programming fundamentals learned in your Java software development classes apply directly to programming in the C language.
Our discussion will be based a specific variant of C know as C99 (as implemented by the GNU GCC compiler).
C provides numeric data types that are similar to those in Java.
In Java we have the following numeric data primitives:
| Datatype | Content | Space Required | Min Value | Max Value |
|---|---|---|---|---|
| byte | integer | 8 bits | -128 | 127 |
| short | integer | 16 bits | -32,768 | 32,767 |
| int | integer | 32 bits | -2,147,483,648 | 2,147,483,647 |
| long | integer | 64 bits | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,808 |
| float | real | 32 bits | -3.4E38 | 3.4E38 |
| double | real | 64 bits | -1.8E308 | 1.8E308 |
In C, the space required by the int and long types depends on the target microcontroller. Also, we have some additional numeric data types:
| Datatype | Content | Space Required | Min Value | Max Value |
|---|---|---|---|---|
| unsigned byte | integer | 8 bits | 0 | 255 |
| unsigned short | integer | 16 bits | 0 | 65,535 |
| unsigned int | integer | target dependent | 0 | ?? |
| unsigned long | integer | target dependent | 0 | ?? |
The C99 standard introduces the following types which have a fixed space requirement. You should use these types in your code:
| Datatype | Content | Space Required | Min Value | Max Value |
|---|---|---|---|---|
| uint8_t | integer | 8 bits | 0 | 255 |
| uint16_t | integer | 16 bits | 0 | 65,535 |
| uint32_t | integer | 32 bits | 0 | 4,294,967,295 |
| uint64_t | integer | 64 bits | 0 | 18,446,744,073,709,551,615 |
| int8_t | integer | 8 bits | -128 | 127 |
| int16_t | integer | 16 bits | -32,768 | 32,767 |
| int32_t | integer | 32 bits | -2,147,483,648 | 2,147,483,647 |
| int64_t | integer | 64 bits | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,808 |
Java supports a few other primitive data types:
boolean – Represents true or false.char – holds a single character, a multi-byte Unicode character.C language differences:
bool) but C does not._Bool)._Complex and _Imaginary; however we likely won't use these.char data type:string class.char word[] = "funness";
byte or short, it gets converted to int.double, then the other operand is converted to a double,float, then the other operand is converted to a float,long, then the other operand is converted to a long,int.