Much of the programming fundamentals learned in your Java software development classes apply directly to programming in the C language.
Language History
- The C language was developed at AT & T in the early 1970s.
- C is still very popular in a number of domains including operating system development and embedded systems.
- Compiler generates machine code that runs on specific microcontrollers.
- The C++ language was developed at Bell Labs in 1979 (originally named "C with Classes").
- Attempts to be backwards compatible with C.
- Adds a number of features including object oriented constructs like user defined classes.
- Compiler generates machine code that runs on specific microcontrollers.
- Java was developed at Sun Microsystems and released in 1995.
- Was designed so that C/C++ developers could quickly learn.
- Uses very similar syntax as C/C++.
- Compiler produces byte code that runs on a Java Virtual Machine.
- Includes a number of language "improvements" over C++.
- C# was developed at Microsoft beginning in 1999.
- Similar to Java but compiler produces byte code (termed Common Intermediate Language) that runs on the .NET runtime.
- Includes a number of language "improvements" over Java.
Similarities Between C and Java
Our discussion will be based a specific variant of C know as C99 (as implemented by the GNU GCC compiler).
Numeric Data Types
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 |
Other Data Types
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:
- C++ has support for a Boolean type (called
bool
) but C does not. - C99 has support for a Boolean type (called
_Bool
). - C99 adds additional types like
_Complex
and_Imaginary
; however we likely won't use these. - C/C++ has the
char
data type:- Similar to Java in that it stores a single character.
- Differs from Java in that it is 8-bits big and just stores the ASCII value of the character.
- Although not a primitive type, Java supports the
string
class. - Storage of multiple character strings in C is typically done with null-terminated character arrays.
- Such a character array is referred to as a C-style string.
- The following code allocates space for eight bytes (the seven characters and a null character that is used to terminate the string).
char word[] = "funness";
Variable Declaration and Initialization
- When a variable is declared, memory is allocated for the variable.
- Typically a local variable will be placed on the stack; however the compiler may choose to dedicate a specific register to store the variable.
- If the variable is not given a value when it is declared...
- In Java, the variable is initialized to 0.
- In C/C++, no initialization is performed. Make sure you initialize variables when you declare them. Otherwise your program may behave unpredictablly.
- Exceptions:
- Uninitialized global and static variables are given initial values of 0.
- Some debuggers may initialize variables that are not explicitly initialized (which can be even more frustrating to debug because you don't see the problem when debugging).
Iteration and Selection
- Looping constructs in C are identical to those in Java:
- for() loops.
- while() loops.
- do {} while() loops.
- Conditional constructs in C are identical to those in Java:
- if() statements.
- if() {} else {} statements.
- switch() statements.
Mathematic Operations
- C/C++ and Java share the same arithmetic operators.
- C/C++ and Java share the same operator precedence rules.
- C/C++ and Java share the same arithmetic promotion rules:
- For unary operations, if the operand is
byte
orshort
, it gets converted toint
. - For binary operations:
- If either operand is a
double
, then the other operand is converted to adouble
, - Otherwise, if either operand is a
float
, then the other operand is converted to afloat
, - Otherwise, if either operand is a
long
, then the other operand is converted to along
, - Otherwise, both operands are converted to an
int
.
- If either operand is a
- For unary operations, if the operand is