C for the C++ programmer

NB: I have only begun this, but even as it is it may prove useful to give you a first glance at the what you need to do differently when writing in C rather than C++. I will [try to] work to improve this throughout the semester.

Basic Overview

To begin to discuss what a C++ programmer needs to know to program in C, first we should note the most fundamental differences:

What's the same (or close enough that you won't likely notice any differences)? Loops, if/then/else, switch, the built-in types and function definitions (for the most part).

What is your best resource for C? Kernighan and Ritchie's "The C Programming Language", aka K&R. This is widely considered to be one of the best books written about any programming language. I will not try to duplicate this excellent book. You should own it and you should read it. When I say RTFM (Read The Friendly Manual), K&R will often be one of the better choices. Also check out the relevant man pages.

You can also make use of the current ANSII standard, though that is not written for casual reading.

Now, on to learning how to be functional in C. The topics addressed are:

I/O

C does not use the i/o facilities of C++. There aren't any classes called istream, ostream, etc. The operators << and >> do not have anything to do with i/o.

C provides a number of functions for doing i/o. We will start with output. Here is the classic "Hello World" in C:

int main() {
  printf("Hello World!\n");
}

The function printf is the simplest way of printing to standard output. Later we will see the very similar function fprintf for printing to files.

Our example above shows printf taking only one argument. The first argument to printf is always a string, called the format string. In simple cases like ours, this is all that's needed. We will show some more interesting examples later, but RTFM.

File Pointers

To use C's standard I/O of course you need to open a file and have a way to refer to the open file.

File Descriptors

File pointers are a C thing. Actual low level Unix I/O is done through file descriptors. File descriptors are small integers that act as indices into a table of information that, naturally enough, describes open files.

Definining Types

Struct

Structs hold member variables. They do not have member functions. Access is always public, not just by default. There are no such thing as access permissions and the symbols public, private or protected are not keywords in C.

So structs in C are simply "less" than what they and classes are in C++.

One very noticeable point is the way that you use them.

Union

The purpose if a union is to define two (or more) different ways to access the same memory.

union FOO {
    int a;
    char b[4];
};
int main() {
    union FOO x;
    int i;
    x.a = 0x12345678;  // The 0x at the beginning means this is a hexadecimal value.
    for (i = 0; i < 4; ++i) {
        // %x means to print in hexidecimal
        printf("%x\n", x.b[i]);
    }
}
On my machine, the above code printed out

78
56
34
12

Typedef

C-String Utilities

Functions

The Heap


Maintained by John Sterling (http://cis.poly.edu/jsterling) Last updated: October 26, 2009
Please send me any comments or corrections so that I may improve the value of this resource.