Older Version Newer Version

GerdIsenberg GerdIsenberg Feb 19, 2018

[[toc]]
**[[Home]] * [[Programming]] * [[Languages]] * C**

[[https://en.wikipedia.org/wiki/C_%28programming_language|C]] is a pragmatical, general purpose, [[https://en.wikipedia.org/wiki/Statement_block|block structured]], [[https://en.wikipedia.org/wiki/Procedural_programming|procedural]], [[https://en.wikipedia.org/wiki/Imperative_programming|imperative]] [[https://en.wikipedia.org/wiki/Programming_language|programming language]]. C was developed in **1972** by [[https://en.wikipedia.org/wiki/Dennis_Ritchie|Dennis Ritchie]] at the [[Bell Laboratories]]. It was first intended as a [[https://en.wikipedia.org/wiki/System_software|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.

Due to explicit pointers (address of a variable or function), C can be considered as a high level assembly language, but has some weak spots in its initial design and implementation, which are addressed in
* [[https://en.wikipedia.org/wiki/ANSI_C|ANSI C from Wikipedia]]
* [[https://en.wikipedia.org/wiki/C99|C99 from Wikipedia]]
* [[https://en.wikipedia.org/wiki/C11_%28C_standard_revision%29|C11 (C standard revision) from Wikipedia]]
* [[https://en.wikipedia.org/wiki/Clang|Clang from Wikipedia]] <ref>[[https://en.wikipedia.org/wiki/LLVM|LLVM from Wikipedia]]</ref>
* [[Cpp|C++]]

=Data= 
==Data Types== 
===Primitive Data Types=== 
//To be aware of the scalar 64-bit origin of [[Bitboards|bitboards]] in computer chess, we use so far a type defined unsigned integer U64 in our C and [[Cpp|C++]] source snippets. The macro C64 will append a suffix to 64-bit constants as required by some compilers//:
[[code]]
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
[[code]]

===Pointer=== 
===Array=== 
[[#Struct]]
===Struct=== 
A structure in C refers to [[https://en.wikipedia.org/wiki/Object_composition|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.
[[code format="cpp"]]
struct MOVE
{
   char from;
   char to;
};

...
MOVE m, a, *b;
m.from = square;
...
if ( a.from == b->to )
[[code]]
[[#Bitfield]]
===Bitfield===
So called [[https://en.wikipedia.org/wiki/Bit_field|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|encoding moves]].

===Union=== 

==Variables== 
Variables are either stored in various memory areas or kept inside processor registers.
===Globals=== 
===On the Stack=== 
===On the Heap=== 
===Register===

=Instructions= 
==Operations== 
===Arithmetical=== 
===Bitwise boolean=== 
===Relational=== 
===Logical=== 
==Functions== 
* [[https://en.wikipedia.org/wiki/C_mathematical_functions|C mathematical functions from Wikipedia]]
==Expressions== 

==Control Flow== 
* [[https://en.wikipedia.org/wiki/Control_flow|Control flow from Wikipedia]]
[[#Goto]]
===Goto=== 
* [[https://en.wikipedia.org/wiki/Goto|Goto from Wikipedia]] 
* [[https://en.wikipedia.org/wiki/Considered_harmful|Considered harmful from Wikipedia]]<ref>[[https://en.wikipedia.org/wiki/Edsger_Dijkstra|Edsger Dijkstra]] (**1968**). //Go To Statement Considered Harmful//. [[ACM#Communications|Communications of the ACM]], Vol. 11, No. 3, [[http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF|pdf]]</ref> <ref>[[Mathematician#WiWulf|William A. Wulf]] (**1971**). //Programming Without the GOTO//. [[IFIP]], Ljubljana, Yugoslavia, August 1971</ref> <ref>[[Mathematician#WiWulf|William A. Wulf]] (**1972**). //A Case Against the GOTO//. Proceedings of the [[ACM]] National Conference, Boston, August 197</ref> <ref>[[Donald Knuth]] (**1974**). //Structured Programming with go to Statements//. [[ACM #Surveys|ACM Computing Surveys]], Vol. 6, No. 4, [[http://cs.sjsu.edu/~mak/CS185C/KnuthStructuredProgrammingGoTo.pdf|pdf]]</ref> <ref>[[Ward Douglas Maurer]] (**1996**). //[[http://dl.acm.org/citation.cfm?id=238417|Attitudes toward the go-to statement (or, hydrogen considered harmful)]]//. [[http://www.journals.elsevier.com/computers-and-education/|Computers & Education]], Vol. 26, No. 4</ref> <ref>[[http://www.codinghorror.com/blog/2007/10/id-consider-that-harmful-too.html|Coding Horror: I'd Consider That Harmful, Too]] by [[https://en.wikipedia.org/wiki/Jeff_Atwood|Jeff Atwood]], October 25, 2007</ref>
* [[http://stevemcconnell.com/ccgoto.htm|Using gotos]] by [[http://www.stevemcconnell.com/aboutme.htm|Steve McConnell]] <ref>[[http://www.stevemcconnell.com/aboutme.htm|Steve McConnell]] (**1993**). //[[http://www.stevemcconnell.com/cc1.htm|Code Complete: A Practical Handbook of Software Construction]]//. [[https://en.wikipedia.org/wiki/Microsoft_Press|Microsoft Press]]</ref>

===If else===
* [[https://en.wikipedia.org/wiki/Conditional_%28computer_programming%29|Conditional (computer programming) from Wikipedia]] 
[[#Switch]]
===Switch case=== 
* [[https://en.wikipedia.org/wiki/Switch_statement|Switch statement from Wikipedia]] 
[[#FunctionPointer]]
===Function Pointer=== 
* [[https://en.wikipedia.org/wiki/Function_pointer|Function pointer from Wikipedia]] 
[[#For]]
===For=== 
* [[https://en.wikipedia.org/wiki/For_loop|For loop from Wikipedia]]
[[#While]]
===While===
* [[https://en.wikipedia.org/wiki/While_loop|While loop from Wikipedia]]
[[#Do]]
===Do while=== 
* [[https://en.wikipedia.org/wiki/Do_while_loop|Do while loop from Wikipedia]]
* [[#Duff]][[http://doc.cat-v.org/bell_labs/duffs_device|The amazing Duff's Device]] by [[https://en.wikipedia.org/wiki/Tom_Duff|Tom Duff]]
* [[https://en.wikipedia.org/wiki/Duff%27s_device|Duff's Device from Wikipedia]]
[[#Preprocessor]]
=Preprocessor= 
* [[https://en.wikipedia.org/wiki/C_preprocessor|C preprocessor from Wikipedia]]

=Portabilty= 
==Twos' Complement== 
==sizeof== 
==shift== 
==Endianness== 
//Main article// [[Endianness]]

=Libraries= 
* [[https://en.wikipedia.org/wiki/C_standard_library|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.
* [[Microsoft]] [[https://en.wikipedia.org/wiki/Visual_C%2B%2B|Visual C++]]
* [[https://en.wikipedia.org/wiki/Intel_C%2B%2B_Compiler|Intel C++]]
* [[Free Software Foundation#GCC|GNU C Compiler]]

=Books= 
* [[https://en.wikipedia.org/wiki/Brian_Kernighan|Brian W. Kernighan]], [[https://en.wikipedia.org/wiki/Dennis_Ritchie|Dennis M. Ritchie]] (**1978, 1988**). //[[https://en.wikipedia.org/wiki/The_C_Programming_Language|The C Programming Language]]//. First Edition ISBN 0-13-110163-3, Second Edition ISBN 0-13-110362-8
> [[image:The_C_Programming_Language_1st_edition_cover.jpg link="https://en.wikipedia.org/wiki/File:The_C_Programming_Language_1st_edition_cover.jpg"]]
* [[Andrew Koenig]] (**1989**). //[[https://en.wikipedia.org/wiki/C_Traps_and_Pitfalls|C Traps and Pitfalls]]//. [[https://en.wikipedia.org/wiki/Addison-Wesley|Addison-Wesley]], ISBN 0-201-17928-8, [[http://literateprogramming.com/ctraps.pdf|pdf preprint]]
* [[Patrick Winston]] (**1994**). //[[http://people.csail.mit.edu/phw/Books/index.html#OnToC|On To C]]//. ISBN-13: 978-0201580426
* [[Andrew Appel]], [[http://dblp.uni-trier.de/pers/hc/g/Ginsburg:Maia|Maia Ginsburg]] (**1998**). //[[https://www.cs.princeton.edu/~appel/modern/c/|Modern Compiler Implementation in C]]//. [[https://en.wikipedia.org/wiki/Cambridge_University_Press|Cambridge University Press]]

=Publications=
* [[https://en.wikipedia.org/wiki/Robert_C._Seacord|Robert C. Seacord]] (**2010**). //Dangerous Optimizations and the Loss of Causality//. CS 15-392 © 2010 [[Carnegie Mellon University]], [[https://www.securecoding.cert.org/confluence/download/attachments/40402999/Dangerous+Optimizations.pdf|slides as pdf]]
* [[http://pdos.csail.mit.edu/~xi/|Xi Wang]], [[http://pdos.csail.mit.edu/~hchen/|Haogang Chen]], [[http://people.csail.mit.edu/akcheung/|Alvin Cheung]], [[http://zhihaojia.com/|Zhihao Jia]], [[http://people.csail.mit.edu/nickolai/|Nickolai Zeldovich]], [[http://pdos.csail.mit.edu/~kaashoek/|M. Frans Kaashoek]] (**2012**). //Undefined Behavior: What Happened to My Code//? [[http://pdos.csail.mit.edu/papers/ub:apsys12.pdf|pdf]]
* [[http://wdtz.org/|Will Dietz]], [[http://www.cs.utah.edu/~peterlee/|Peng Li]], [[http://www.cs.utah.edu/~regehr/|John Regehr]], [[http://llvm.cs.uiuc.edu/~vadve/Home.html|Vikram Adve]] (**2012**). //Understanding Integer Overflow in C/C++//.  [[http://www.cs.utah.edu/~regehr/papers/overflow12.pdf|pdf]]

=Forum Posts=
==1999==
* [[http://www.stmintz.com/ccc/index.php?id=74219|C or C++ for chess programming: speed]] by [[Marc-Philippe Huget]], [[CCC]], October 20, 1999
==2000 ...==
* [[http://www.stmintz.com/ccc/index.php?id=211975|One (silly) question about "C"]] by Antonio Senatore, [[CCC]], February 05, 2002
==2005 ...==
* [[http://www.stmintz.com/ccc/index.php?id=405552|Re: chess engines writen in C]] by [[Dann Corbit]], [[CCC]], January 13, 2005
* [[http://www.talkchess.com/forum/viewtopic.php?t=21673|ansi-C question]] by [[Vincent Diepeveen]], [[CCC]], June 08, 2008
* [[http://www.talkchess.com/forum/viewtopic.php?t=23292|setjmp() - another one]] by [[Chris Whittington]], [[CCC]], August 27, 2008
* [[http://www.talkchess.com/forum/viewtopic.php?t=27279|kbhit() taking huge CPU??]] by [[John Merlino]], [[CCC]], April 01, 2009 » [[Thread]]
* [[http://www.talkchess.com/forum/viewtopic.php?t=29562|Critter: Pascal vs C]] by [[Richard Vida]], [[CCC]], August 27, 2009 » [[Pascal]]
==2010 ...==
* [[http://www.talkchess.com/forum/viewtopic.php?t=38441|MSVC calloc question]] by [[Harm Geert Muller]], [[CCC]], March 17, 2011
* [[http://www.talkchess.com/forum/viewtopic.php?t=38523|My experience with Linux/GCC]] by [[Richard Vida]], [[CCC]], March 23, 2011 » [[Linux]]
* [[http://www.talkchess.com/forum/viewtopic.php?t=39587|a cautionary tale about simple-looking macros]] by [[Wylie Garvin]], [[CCC]], July 03, 2011
* [[http://talkchess.com/forum/viewtopic.php?t=39683|c or c++ ?]] by ethan ara, [[CCC]], July 10, 2011
* [[http://www.talkchess.com/forum/viewtopic.php?t=44111|VisualStudio - __fastcall instead of __cdecl?]] by [[Martin Sedlak]], [[CCC]], June 18, 2012
* [[http://www.talkchess.com/forum/viewtopic.php?t=47414|C vs ASM]] by [[Ed Schroder|Ed Schröder]], [[CCC]], March 05, 2013 » [[Assembly]]
* [[http://www.talkchess.com/forum/viewtopic.php?t=48812&start=6|Re: goto thread (split)]] by [[Steven Edwards]], [[CCC]], August 01, 2013 » [[Iterative Search]], [[Symbolic]]
* [[http://www.talkchess.com/forum/viewtopic.php?t=50186|A note for C programmers]] by [[Robert Hyatt]], [[CCC]], November 23, 2013
> [[http://www.talkchess.com/forum/viewtopic.php?t=50186&start=80|Re: A note for C programmers]] by [[Rein Halbersma]], [[CCC]], November 28, 2013
* [[http://www.open-chess.org/viewtopic.php?f=5&t=2519|A note on strcpy]] by [[Dann Corbit|User923005]], [[Computer Chess Forums|OpenChess Forum]], November 26, 2013
* [[http://www.talkchess.com/forum/viewtopic.php?t=50387|strcpy() revisited]] by [[Robert Hyatt]], [[CCC]], December 08, 2013
==2015 ...==
* [[http://www.talkchess.com/forum/viewtopic.php?t=58882|Using more than 1 thread in C beginner question]] by [[Uri Blass]], [[CCC]], January 11, 2016 » [[Thread]]
* [[http://www.talkchess.com/forum/viewtopic.php?t=58967|C programming style question]] by [[Michael Sherwin]], [[CCC]], January 19, 2016
* [[http://www.talkchess.com/forum/viewtopic.php?t=59464|Crafty c questions]] by [[J. Wesley Cleveland]], [[CCC]], March 10, 2016 » [[Crafty]]
* [[http://www.talkchess.com/forum/viewtopic.php?t=66624|I'm not very happy with the do {} while() statement in C]] by [[Michael Sherwin]], [[CCC]], February 18, 2018

=External Links= 
* [[https://en.wikipedia.org/wiki/C_%28programming_language|C from Wikipedia]]
* [[http://publications.gbdirect.co.uk/c_book/|The C Book - Table of Contents]], an online version of the popular introduction and reference on the ANSI Standard C programming language
* [[http://gwiesenekker.dyndns.org/page7/page7.html|C utilities]] by [[Gijsbert Wiesenekker]]
* [[http://www.azillionmonkeys.com/qed/programming.html|Programming Bits]] by [[Paul Hsieh]]
* [[http://groups.google.com/group/comp.lang.c/topics|comp.lang.c]] Discussion about C
* [[http://c-faq.com/index.html|comp.lang.c Frequently Asked Questions]]
* [[http://www.youtube.com/playlist?list=PLZ1QII7yudbc-Ky058TEaOstZHVbT-2hg|Chess Engine In C]] - [[https://en.wikipedia.org/wiki/YouTube|YouTube]] Videos by [[BlueFeverSoft]] » [[Vice]]
* [[https://matt.sh/howto-c|How to C in 2016]]

=References=
<references />
**[[Languages|Up one Level]]**