../ -- the C programming language

links to programming in the C language.

updated 2006-04-20

David is writing a book (YARMAC) on programming in the C language. (Ask me how it's going !) Here's some links to resources that go beyond the scope of that book.

Contents:

related local pages:

[FIXME: move the C programming stuff from linux.html and book.html to here. ]

consider moving YARMAC, my C book, to http://www.foogirl.de/C/FrontPage (found from a link on worldwidewiki) [offline ?]

Consider also moving this text to http://c2.com/cgi/wiki?CategoryCee and/or copying text there to YARMAC.

Or perhaps merging with Wikibooks: Programming in C http://en.wikibooks.org/wiki/Programming:C

Or perhaps merging with the GCC wiki http://gcc.gnu.org/wiki

latest news

latest news about C programming [FIXME: .... online discussion groups ...]

tutorials

FAQ

FAQ

arrays in C

To iterate over every element of an array in plain ANSI C, use

 #define NUM_ELEM(x) (sizeof (x) / sizeof (*(x)))

 for( i = 0; i < NUM_ELEM(array); i++ )
 {
     /* do something with array[i] */
     ;
 }

pages like this one

pages like this one: information about lots of different aspects of C programming (tutorials, libraries, C++, STL, style guides, etc.)

volatile

The "volatile" keyword is ``only'' needed to refer to non-RAM hardware. (serial ports, video RAM, ...).

But what a boring world this would be if all we had was RAM.

Date: Mon, 6 Aug 2001 16:54:12 -0500


Use of 'const' and 'volatile'

http://www.arm.com/support.ns4/html/sdt_build!OpenDocument&ExpandSection=19#_Section19

Description
The correct placement of the type qualifiers 'const' and 'volatile' can
sometimes be confusing, especially when applied to a pointer or the thing it
points to.

Solution
Here is a simple rule which may help: place 'const' (or 'volatile') *after* the
thing you're saying is constant (or volatile).

    * to declare a constant integer, use 'int const xxx', instead of 'const int
xxx'.
    * to declare a pointer to a constant integer, put 'const' after 'int' to get
 'int const *ptr'.
    * to declare a constant pointer to an integer, put 'const' after '*' and get
 'int * const ptr'.
    * to declare a constant pointer to a constant integer, use 'int const *
const ptr'.
    * to declare a constant pointer to a volatile integer, use 'int volatile *
const ptr'.


The really nice thing about doing it this way is that you can see what the type
is by simply reading *backwards* through the definition:

int const xxx;
   |    |    |
   |    |    +----------> xxx is a
   |    +---------------> constant
   +--------------------> integer

  int const *ptr;
   |    |   | |
   |    |   | +---------> ptr is a
   |    |   +-----------> pointer to a
   |    +---------------> constant
   +--------------------> integer

  int * const ptr;
   |  |   |    |
   |  |   |    +--------> ptr is a
   |  |   +-------------> constant
   |  +-----------------> pointer to an
   +--------------------> integer

  int const * const ptr;
   |    |   |   |    |
   |    |   |   |    +--> ptr is a
   |    |   |   +-------> constant
   |    |   +-----------> pointer to a
   |    +---------------> constant
   +--------------------> integer

  int volatile * const ptr;
   |     |     |   |    |
   |     |     |   |    +--> ptr is a
   |     |     |   +-------> constant
   |     |     +-----------> pointer to a
   |     +-----------------> volatile
   +-----------------------> integer


For a real example of the application of 'const', see Placing (constant) jump
tables in ROM.

For an example of the use of 'volatile', see armcc/tcc: Placing C variables at
specific addresses - memory-mapped peripherals.

In fact, this method works for any type qualifier, including __packed, e.g. int
__packed x and int *__packed x;

More about const: http://c2.com/cgi/wiki?ConstCorrectness

refactoring

refactoring

Refactoring is a key part of XP (eXtreme Programming). It's been a major part of the Forth culture for years; the original reason Forth programmers started refactoring isn't relevant to most desktop or server applications, but they've discovered other benefits that apply to C (and practically every language).

Refactoring can be considered a form of functional program compression data_compression.html#program_compression .

DAV: ... In FORTH and Postscript, any ``block'' (a section of code that is only entered in one location and exited in one location) that is duplicated twice in the source can be factored out fairly easily... ... recognize that one function has been ``in-lined'' many places in the code, then split it out into a stand-alone version and replace all repeats with calls to that function. ... [FIXME: consider writing a program to find the ``longest'' duplicated code block ... or the ``most frequent'' duplicated code block ...] [FIXME: consider writing a ``program compressor'' to try to automatically factor all possible repeated blocks]

Program Coding Standards and Style Guides

Both "UI Style Guides" to make programs easier to use, and "Coding Standards" to make source code easier to understand and improve. Online Style Guides.

(also see HTML Style Guides) (also see user_interface.html)

contents:

general program coding standards and style guides

Up: #style_guides

C program coding standards and style guides

Up: #style_guides

program coding standards and style guides for other languages

Up: #style_guides

(DAV suspects that, since well-written code is written for the audience of other humans, then really good styles for one language probably can be applied to any computer language ... except, of course, when that's working around a flaw in that language)

Books Books on programming style:

Up: #style_guides

development tools: IDEs, documentation tools, etc.

development tools: IDEs, documentation tools, ``lint'', etc.

various programs (tools) used to make writing programs easier / better.

I almost split out a section on just ``documentation and literate programming'' -- but documentation is something that needs to saturate every part of programming.

Here I list stand-alone programs. Elsewhere [FIXME:] I list libraries that you can compile into your own programs.

[IDEs, literate programming and documentation tools, lint, ...]

stl

the standard template library (STL) [FIXME: shouldn't some of these be moved to the style guide section ?]

C dialects

There are all kinds of little things in C that people get annoyed at. Here's some dialects of C that try to fix one or more of them. There's a few C interpreters (useful for testing and tweaking pieces of code, without a long edit-compile-run cycle), ...

tiny C programming language

http://www.nicholson.com/rhn/basic/ has several pointers to C interpreters

BASIC

Beginner's All-purpose Symbolic Instruction Code

[FIXME: I'm not really that interested in BASIC any more ... is there a better place to put this stuff ?]

Tiny Basic

(see also Tiny C tiny_c )

68000 Tiny Basic Web Page by Gordon Brandly http://members.home.net/gbrandly/68ktinyb.html

Chipmunk Basic Home Page http://www.nicholson.com/rhn/basic/ has a list of pointers to other Basic interpreters with source code. Check out "structured Basic".

source code to some programs written in Tiny Basic http://www.code.archive.aisnota.com/

MoonRock compiler and development language http://www.rowan.sensation.net.au/moonrock.html "MoonRock is a BASIC-like language with several extensions. Produces small and tight executables (MS-DOS). 8086, 80186+ or 80386+ code. DOS real mode or DPMI protected mode. " "the compiler is written with MicroSoft's QuickBASIC and Visual BASIC for DOS. A new version of MoonRock is being rewritten from scratch, coded entirely in MoonRock. " also points to "SLC (Simple Little Compiler)" DAV: this seems to have some similarities to my "functional compression" ideas ...

comp.lang.basic.misc FAQ http://www.rahul.net/rhn/basic/basic.faq.txt

boolean operators vs. bitwise operators: with the convention that 0 = false, 1 = true, and boolean operators "<", ">", "=", etc all return 0 or 1, while "if()" accepts any non-zero as true, then:

unsorted


started 2000-12-29:DAV: David Cary.

Return to index // validate // end http://david.carybros.com/html/c_programming.html /* was http://rdrop.com/~cary/html/c_programming.html */