C Programming for Java Programmers
- Mark Allen Weiss, "C++ for Java Programmers"
These notes are prepared to quickly bring CS 385 Operating Systems students up to speed with the basics of C programming they will need for their programming assignments. They are almost entirely restricted to pure C, with only a few of the C++ enhancements included, and no classes or other OO programming.
See also C Plus Course Notes Pages
Some Simple Samples
- C was developed when programs had to be space, time, and instruction efficient. The philosophy is to trust programmers and to give them all the power and flexibility possible. It is like a very powerful chainsaw with no safety guards to get in the way.
- C does not check array indices or memory accesses ( pointers. )
- C does little if any type checking. Typecasts between incompatable types is allowed.
- C does not initialize variables. Uninitialized variables may contain random values ( commonly ) or zeros ( only in certain implementations. )
- C provides much more direct access to memory storage locations, with no checks or restrictions.
- C++ was written to be backwards compatible with C, so ( almost ) any program written in pure C can be compiled and run in C++ with no loss of speed or space efficiency.
1 Basic Types and Control Structures
- Functions in C do not have to be inside of classes. ( Pure C doesn't even support classes. )
- C provides a preprocessor, which processes user code before passing it to the compiler.
- #include <library.h> - Reads the file "library.h" from standard directories and inserts the text into this position of the user's code.
- #include "mylibrary.h" - Reads the file "mylibrary.h" from the current directory, as above.
- #define MAXCOLS 1000 - Initializes a text substitution of every occurance of "MAXCOLS" to "1000"
- #define DEBUG - Sets the preprocessor variable "DEBUG' to 1
- #ifdef, #ifndef, #else, #endif - Used to conditionally include or not include blocks of code passed to the compiler.
- using namespace std; - It's magic, just do it. :-)
- Typecasts - Example: average = ( double ) totalScores / nStudents;
- typedef - defines new types, typically structures
- typedef myIntegerType int;
- Conditional tests work on numbers as well as booleans. Any non-zero number is considered true:
2 Functions, Arrrays, Strings, and Parameter Passing
- Function Prototypes
- Parameter passing mechanisms
- by value
- by reference
- by pointer / address
- Relationship between arrays, pointers, and memory addresses.
- Arrays of pointers - double indirection. ( or more. )
- C-style character strings. ( See 11 below. )
3 Pointers and Reference Variables
- void * - A generic pointer that can be used to point to any memory location. In general void pointers must be typecast to specific pointer types before they can be used.
- char * - The C language did not originally have a void * type, so programmers would use a char * as a generic pointer, to point to any memory location with a "resolution" of one byte. Many old system calls use char * for what we would today use void *.
4.9 The struct Type
- Pointers to structs.
- structs containing pointers.
- Arrays of structs.
- Endless combinations of the above.
10 Collections: The Standard Template Library
11 Primitive Arrays and Strings
12 C-Style C++