C

From Chessprogramming wiki
Jump to: navigation, search

Home * Programming * Languages * C

The C Programming Language [1]

C,
a pragmatical, general purpose, block structured, procedural, imperative programming language. C was developed in 1972 by Dennis Ritchie at the Bell Laboratories. It was first intended as a system programming language for the Unix operating system, but has spread to many other platforms and application programming as well. C and its derivations are likely the most often used languages so far for computer chess programming.

Revisions

Due to explicit pointers (address of a variable or function), C might be considered as a high level assembly language, and has some weak spots in its initial design and implementation, which are addressed in

See also

Data

Data Types

C data types are declarations for memory locations for basic and compound data.

Basic Data Types

The C language provides the four basic arithmetic type specifiers char, int, float and double, and the modifiers signed, unsigned, short and long. C99 added a boolean (true/false) type _Bool.

To be aware of the scalar 64-bit origin of bitboards in computer chess, we use so far a type defined unsigned integer U64 in our C and C++ source snippets. The macro C64 will append a suffix to 64-bit constants as required by some compilers:

typedef unsigned __int64 U64;    // for the old microsoft compilers
typedef unsigned long long  U64; // supported by MSC 13.00+ and C99
#define C64(constantU64) constantU64##ULL

Pointer

A pointer is a data type that contains the address of a storage location of a variable (or function). They are declared with the asterisk (*) type declarator following the basic storage type and preceding the variable name of the pointer.

int * ptr2int;

Array

An array is a collection of values, all of the same type, stored contiguously in memory. An array of size N is indexed by integers from 0 up to and including N−1.

unsigned int moves[256];
char *pc[10];  /* array of 10 elements of 'pointer to char' */
char (*pa)[10]; /* pointer to a 10-element array of char */

In C, array indexing is formally defined in terms of pointer arithmetic; that is, the language specification requires that array[i] be equivalent to *(array + i).

Struct

A structure in C refers to Object composition to encapsulate related scalar datatypes inside one structured item. The size of the structure is the sum of its element sizes. To access the structure elements the dot-operator separates the element from the variable or reference. Pointers require arrow operator.

struct MOVE
{
  char from;
  char to;
};

...
MOVE m, a, *b;
m.from = square;
...
if ( a.from == b->to )

Bitfield

So called Bitfields might be implemented as structure where integer members are declared with explicit bit length specifier from 1 .. 31. However due to portability issues of various C-compilers and platforms concerning bit ordering, padding and eventually the sign, most programmer rely on explicit bitfields to composite and extract sub-items by shift and masks, i.e. in encoding moves.

struct DoubleLayout
{
  unsigned int mantissal : 32;
  unsigned int mantissah : 20;
  unsigned int exponent : 11;
  unsigned int sign : 1;
};

Union

A value that may have any of several representations within the same position in memory.

union BitBoard 
{
  U64 qw;
  U32 dw[2];
};

Variables

Variables are stored either in various memory areas or kept inside processor registers (if not referred by a pointer) as:

Instructions

Expressions

An expression in a programming language is a combination of one or more constants, variables, operators, and functions.

Functions

Operators

Control Flow

Goto

If else

Switch case

Function Pointer

For

While

Do while

send(to, from, count)
  register short *to, *from;
  register count;
  {
    register n=(count+7)/8;
    switch(count%8){
    case 0: do{ *to = *from++;
    case 7:     *to = *from++;
    case 6:     *to = *from++;
    case 5:     *to = *from++;
    case 4:     *to = *from++;
    case 3:     *to = *from++;
    case 2:     *to = *from++;
    case 1:     *to = *from++;
        } while(--n > 0);
    }
  }

Preprocessor

Portabilty

Libraries

C and C++ Compiler

A C-Compiler is used to translate the source program, usually ascii-text files with the extension .C, to so called object files, containing machine instructions. A linker binds all the object files together with libraries containing external functions (and data) to build an executable program.

Publications

Forum Posts

1999

2000 ...

2005 ...

2010 ...

Re: A note for C programmers by Rein Halbersma, CCC, November 28, 2013

2015 ...

External Links

References

  1. The cover to the book, Brian W. Kernighan, Dennis M. Ritchie (1978, 1988). The C Programming Language. First Edition
  2. Edsger Dijkstra (1968). Go To Statement Considered Harmful. Communications of the ACM, Vol. 11, No. 3, pdf
  3. William A. Wulf (1971). Programming Without the GOTO. IFIP, Ljubljana, Yugoslavia, August 1971
  4. William A. Wulf (1972). A Case Against the GOTO. Proceedings of the ACM National Conference, Boston, August 197
  5. Donald Knuth (1974). Structured Programming with go to Statements. ACM Computing Surveys, Vol. 6, No. 4, pdf
  6. Ward Douglas Maurer (1996). Attitudes toward the go-to statement (or, hydrogen considered harmful). Computers & Education, Vol. 26, No. 4
  7. Coding Horror: I'd Consider That Harmful, Too by Jeff Atwood, October 25, 2007
  8. Steve McConnell (1993). Code Complete: A Practical Handbook of Software Construction. Microsoft Press

Up one Level