Changes

Jump to: navigation, search

C

17,688 bytes added, 15:46, 28 February 2019
Created page with "'''Home * Programming * Languages * C''' FILE:The C Programming Language, First Edition Cover (2).svg|border|right|thumb| The C Programming Language <..."
'''[[Main Page|Home]] * [[Programming]] * [[Languages]] * C'''

[[FILE:The C Programming Language, First Edition Cover (2).svg|border|right|thumb| The C Programming Language <ref>The cover to the book, [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</ref> ]]

'''C''',<br/>
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.

=Revisions=
Due to explicit [https://en.wikipedia.org/wiki/Pointer_(computer_programming) 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
* [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/C18_(C_standard_revision) C18 (C standard revision) from Wikipedia]

=See also=
* [[Cpp|C++]]
* [[C sharp|C#]]
* [[D (Programming Language)]]
* [[Go (Programming Language)]]
* [[Java]]

=Data=
==Data Types==
[https://en.wikipedia.org/wiki/C_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|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'':
<pre>
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
</pre>
===Pointer===
A [https://en.wikipedia.org/wiki/C_data_types#Pointers 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.
<pre>
int * ptr2int;
</pre>
===Array===
An [[Array|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.
<pre>
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 */
</pre>

In C, array indexing is formally defined in terms of pointer arithmetic; that is, the language specification requires that <code>array[i]</code> be equivalent to <code>*(array + i)</code>.
===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.
<pre>
struct MOVE
{
char from;
char to;
};

...
MOVE m, a, *b;
m.from = square;
...
if ( a.from == b->to )
</pre>
===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]].
<pre>
struct DoubleLayout
{
unsigned int mantissal : 32;
unsigned int mantissah : 20;
unsigned int exponent : 11;
unsigned int sign : 1;
};
</pre>
===Union===
A [https://en.wikipedia.org/wiki/Union_type#C/C++ value] that may have any of several representations within the same position in memory.
<pre>
union BitBoard
{
U64 qw;
U32 dw[2];
};
</pre>

==Variables==
[https://en.wikipedia.org/wiki/Variable_(computer_science)#Scope_and_extent Variables] are stored either in various memory areas or kept inside processor registers (if not referred by a pointer) as:
* [https://en.wikipedia.org/wiki/Global_variable Global] or [https://en.wikipedia.org/wiki/Static_variable Static Variable] in static memory
* [https://en.wikipedia.org/wiki/Local_variable Local Variable] as [https://en.wikipedia.org/wiki/Automatic_variable Automatic Variable] on the [https://en.wikipedia.org/wiki/Stack-based_memory_allocation Stack]]
* Local Variable as Automatic Variable inside a [https://en.wikipedia.org/wiki/Processor_register Processor Register]
* [https://en.wikipedia.org/wiki/C_dynamic_memory_allocation#Heap-based Dynamic memory allocation on the Heap]

=Instructions=
==Expressions==
An [https://en.wikipedia.org/wiki/Expression_(computer_science) expression] in a programming language is a combination of one or more constants, variables, operators, and functions.
==Functions==
* [https://en.wikipedia.org/wiki/C_mathematical_functions C mathematical functions from Wikipedia]
==Operators==
* [https://en.wikipedia.org/wiki/Assignment_operator_(C%2B%2B) Assignment] =
* [https://en.wikipedia.org/wiki/Arithmetic#Arithmetic_operations Arithmetical] +, -, *, /, %, ++, --
* [https://en.wikipedia.org/wiki/Bitwise_operations_in_C Bitwise] &, |, ^, ~, [https://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts >>, <<]
* [https://en.wikipedia.org/wiki/Augmented_assignment Augmented assignment] +=, -=. *=, /=, %=, &=, |=, ^=, <<=, >>=
* [https://en.wikipedia.org/wiki/Relational_operator#B_and_C Relational] ==, <, >, <=, >=, !=
* [https://en.wikipedia.org/wiki/Boolean_algebra Boolean] !, &&, ||
* [https://en.wikipedia.org/wiki/%3F: Conditional] ?:
* [https://en.wikipedia.org/wiki/Comma_operator Comma] ,
* [https://en.wikipedia.org/wiki/Sizeof sizeof]

==Control Flow==
* [https://en.wikipedia.org/wiki/Control_flow Control flow from Wikipedia]
<span id="Goto"></span>
===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]
<span id="Switch"></span>
===Switch case===
* [https://en.wikipedia.org/wiki/Switch_statement Switch statement from Wikipedia]
<span id="FunctionPointer"></span>
===Function Pointer===
* [https://en.wikipedia.org/wiki/Function_pointer Function pointer from Wikipedia]
<span id="For"></span>
===For===
* [https://en.wikipedia.org/wiki/For_loop For loop from Wikipedia]
<span id="While"></span>
===While===
* [https://en.wikipedia.org/wiki/While_loop While loop from Wikipedia]
<span id="Do"></span>
===Do while===
* [https://en.wikipedia.org/wiki/Do_while_loop Do while loop from Wikipedia]
* <span id="Duff"></span>[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]
<pre>
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);
}
}
</pre>
<span id="Preprocessor"></span>
=Preprocessor=
* [https://en.wikipedia.org/wiki/C_preprocessor C preprocessor from Wikipedia]

=Portabilty=
* [https://en.wikipedia.org/wiki/Two%27s_complement Two's Complement]
* [https://en.wikipedia.org/wiki/Sizeof sizeof]
* [https://en.wikipedia.org/wiki/Arithmetic_shift shift]
* [[Endianness]]

=Libraries=
* [https://en.wikipedia.org/wiki/C_standard_library C standard library] (libc)

=C and C++ Compiler=
A [https://en.wikipedia.org/wiki/List_of_compilers#C_compilers 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.
* [https://en.wikipedia.org/wiki/AMD_Optimizing_C/C%2B%2B_Compiler AMD Optimizing C/C++ Compiler]
* [https://en.wikipedia.org/wiki/Clang Clang]
* [https://en.wikipedia.org/wiki/CompCert CompCert]
* [[Free Software Foundation#GCC|GNU C Compiler]]
* [[Intel]] [https://en.wikipedia.org/wiki/Intel_C%2B%2B_Compiler C++]
* [https://en.wikipedia.org/wiki/Interactive_C Interactive C]
* [https://en.wikipedia.org/wiki/LCC_(compiler) LCC (compiler)]
* [[Microsoft]] [https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B Visual C++]
* [http://www.smorgasbordet.com/pellesc/ Pelles C]
* [https://en.wikipedia.org/wiki/Tiny_C_Compiler Tiny C Compiler]

=Publications=
* [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
* [[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], [http://literateprogramming.com/ctraps.pdf pdf preprint]
* [[Patrick Winston]] ('''1994'''). ''[http://people.csail.mit.edu/phw/Books/index.html#OnToC On To C]''. [http://people.csail.mit.edu/phw/OnToC/ On-Line]
* [[Andrew Appel]], [https://dblp.uni-trier.de/pers/hd/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]
* [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://pubweb.eng.utah.edu/~cs5785/slides-f10/Dangerous+Optimizations.pdf slides as pdf]
* [https://pdos.csail.mit.edu/~xi/ Xi Wang], [https://pdos.csail.mit.edu/~hchen/ Haogang Chen], [https://homes.cs.washington.edu/~akcheung/ Alvin Cheung], [http://zhihaojia.com/ Zhihao Jia], [http://people.csail.mit.edu/nickolai/ Nickolai Zeldovich], [https://pdos.csail.mit.edu/~kaashoek/ M. Frans Kaashoek] ('''2012'''). ''Undefined Behavior: What Happened to My Code''? [https://pdos.csail.mit.edu/papers/ub:apsys12.pdf pdf]
* [https://wdtz.org/ Will Dietz], [https://lipeng28.github.io/ Peng Li], [http://www.cs.utah.edu/~regehr/ John Regehr], [https://en.wikipedia.org/wiki/Vikram_Adve Vikram Adve] ('''2012'''). ''Understanding Integer Overflow in C/C++''. [https://www.cs.utah.edu/~regehr/papers/overflow12.pdf pdf]

=Forum Posts=
==1999==
* [https://www.stmintz.com/ccc/index.php?id=74219 C or C++ for chess programming: speed] by [[Marc-Philippe Huget]], [[CCC]], October 20, 1999
==2000 ...==
* [https://www.stmintz.com/ccc/index.php?id=211975 One (silly) question about "C"] by Antonio Senatore, [[CCC]], February 05, 2002
==2005 ...==
* [https://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]
* [https://en.wikibooks.org/wiki/C_Programming C Programming - Wikibooks]
* [https://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://www.azillionmonkeys.com/qed/programming.html Programming Bits] by [[Paul Hsieh]]
* [https://groups.google.com/forum/#!forum/comp.lang.c comp.lang.c] Discussion about C
* [http://c-faq.com/index.html comp.lang.c Frequently Asked Questions]
* [https://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]]'''

Navigation menu