C

Home * Programming * Languages * C



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
 * ANSI C from Wikipedia
 * C99 from Wikipedia
 * C11 (C standard revision) from Wikipedia
 * C18 (C standard revision) from Wikipedia

=See also=
 * C++
 * C#
 * D (Programming Language)
 * Go (Programming Language)
 * Java

=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
 * 1) 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  be equivalent to.

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:
 * Global or Static Variable in static memory
 * Local Variable as Automatic Variable on the Stack]
 * Local Variable as Automatic Variable inside a Processor Register
 * Dynamic memory allocation on the Heap

=Instructions=

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

Functions

 * C mathematical functions from Wikipedia

Operators

 * Assignment =
 * Arithmetical +, -, *, /, %, ++, --
 * Bitwise &, |, ^, ~, >>, <<
 * Augmented assignment +=, -=. *=, /=, %=, &=, |=, ^=, <<=, >>=
 * Relational ==, <, >, <=, >=, !=
 * Boolean !, &&, ||
 * Conditional ?:
 * Comma ,
 * sizeof

Control Flow

 * Control flow from Wikipedia

Goto

 * Goto from Wikipedia
 * Considered harmful from Wikipedia
 * Using gotos by Steve McConnell

If else

 * Conditional (computer programming) from Wikipedia

Switch case

 * Switch statement from Wikipedia

Function Pointer

 * Function pointer from Wikipedia

For

 * For loop from Wikipedia

While

 * While loop from Wikipedia

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=
 * Do while loop from Wikipedia
 * The amazing Duff's Device by Tom Duff
 * Duff's Device from Wikipedia
 * C preprocessor from Wikipedia

=Portabilty=
 * Two's Complement
 * sizeof
 * shift
 * Endianness

=Libraries=
 * C standard library (libc)

=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.
 * AMD Optimizing C/C++ Compiler
 * Clang
 * CompCert
 * GNU C Compiler
 * Intel C++
 * Interactive C
 * LCC (compiler)
 * Microsoft Visual C++
 * Pelles C
 * Tiny C Compiler

=Publications=
 * Brian W. Kernighan, Dennis M. Ritchie (1978, 1988). The C Programming Language. First Edition ISBN 0-13-110163-3, Second Edition ISBN 0-13-110362-8
 * Andrew Koenig (1989). C Traps and Pitfalls. Addison-Wesley, pdf preprint
 * Patrick Winston (1994). On To C. On-Line
 * Andrew Appel, Maia Ginsburg (1998). Modern Compiler Implementation in C. Cambridge University Press
 * Robert C. Seacord (2010). Dangerous Optimizations and the Loss of Causality. CS 15-392 © 2010 Carnegie Mellon University, slides as pdf
 * Xi Wang, Haogang Chen, Alvin Cheung, Zhihao Jia, Nickolai Zeldovich, M. Frans Kaashoek (2012). Undefined Behavior: What Happened to My Code? pdf
 * Will Dietz, Peng Li, John Regehr, Vikram Adve (2012). Understanding Integer Overflow in C/C++. pdf

=Forum Posts=

1999

 * C or C++ for chess programming: speed by Marc-Philippe Huget, CCC, October 20, 1999

2000 ...

 * One (silly) question about "C" by Antonio Senatore, CCC, February 05, 2002

2005 ...

 * Re: chess engines writen in C by Dann Corbit, CCC, January 13, 2005
 * ansi-C question by Vincent Diepeveen, CCC, June 08, 2008
 * setjmp - another one by Chris Whittington, CCC, August 27, 2008
 * kbhit taking huge CPU?? by John Merlino, CCC, April 01, 2009 » Thread
 * Critter: Pascal vs C by Richard Vida, CCC, August 27, 2009 » Pascal

2010 ...

 * MSVC calloc question by Harm Geert Muller, CCC, March 17, 2011
 * My experience with Linux/GCC by Richard Vida, CCC, March 23, 2011 » Linux
 * a cautionary tale about simple-looking macros by Wylie Garvin, CCC, July 03, 2011
 * c or c++ ? by ethan ara, CCC, July 10, 2011
 * VisualStudio - __fastcall instead of __cdecl? by Martin Sedlak, CCC, June 18, 2012
 * C vs ASM by Ed Schröder, CCC, March 05, 2013 » Assembly
 * Re: goto thread (split) by Steven Edwards, CCC, August 01, 2013 » Iterative Search, Symbolic
 * A note for C programmers by Robert Hyatt, CCC, November 23, 2013
 * Re: A note for C programmers by Rein Halbersma, CCC, November 28, 2013


 * A note on strcpy by User923005, OpenChess Forum, November 26, 2013
 * strcpy revisited by Robert Hyatt, CCC, December 08, 2013

2015 ...

 * Using more than 1 thread in C beginner question by Uri Blass, CCC, January 11, 2016 » Thread
 * C programming style question by Michael Sherwin, CCC, January 19, 2016
 * Crafty c questions by J. Wesley Cleveland, CCC, March 10, 2016 » Crafty
 * I'm not very happy with the do {} while statement in C by Michael Sherwin, CCC, February 18, 2018

=External Links=
 * C from Wikipedia
 * C Programming - Wikibooks
 * The C Book - Table of Contents, an online version of the popular introduction and reference on the ANSI Standard C programming language
 * Programming Bits by Paul Hsieh
 * comp.lang.c Discussion about C
 * comp.lang.c Frequently Asked Questions
 * Chess Engine In C - YouTube Videos by BlueFeverSoft » Vice
 * How to C in 2016

=References= Up one Level