#use wml::tmpl::main title="C -- A good compromise between Assembler and high-level programming languages" PAGE=programming SUBPAGE=c

<H1>The C Programming Language</H1>
Read more about the <a href="http://cm.bell-labs.com/cm/cs/who/dmr/chist.html">History of the C Language</A>.

<H2>Language Idioms</H2>
<H3><A name="truth">Truth</A></H3>
C does not have a dedicated data type for boolean values.  Instead,
ordinary integer values are normally used to indicate truth.
When a numeric value is interpreted in a boolean context (in the
test expression of a 'if' or 'while' statement for isntance) it is
False when it is zero (0), otherwise it is
taken to be True.
<P>
The following code snippet illustrates this behaviour:
<TABLE>
<TR><TH>Code</TH><TH>Result</TH></TR>
<TR>
<TD>
<PRE>
\#include <stdio.h>
void truth(int bound)
{
  int i;
  for (i = bound*-1; i <= bound; i++) {
    printf("%2d is %s\n", i, i ? "TRUE" : "FALSE");
  }
}

int main () { truth(2); return 0; }
</PRE>
</TD>
<TD>
<PRE>
-2 is TRUE
-1 is TRUE
 0 is FALSE
 1 is TRUE
 2 is TRUE
</PRE>
</TD>
</TR>
</TABLE>
<P>There are cases in which relying on this convention creates quite
confusing code.  A well known example is the memcmp function,
which return 0 if the compared memory areas are equivalent.
This leads to code like
<PRE>
  if (!memcmp(p1, p2, 4)) { ...
</PRE>
which is confusing to read since the ! operator is normally
read as "not".
It is recommended to avoid this and use a more clear
way to express the same thing:
<PRE>
  if (memcmp(p1, p2, 4) == 0) { ...
</PRE>

<H2>Web resources</H2>
<UL>
<LI><A href="http://cm.bell-labs.com/cm/cs/who/dmr/">Dennis Ritchie Home Page</a></li>
<LI><A href="http://www.easysw.com/~mike/serial/serial.html">
    Serial Programming Guide for POSIX Operating Systems</A></li>
<LI><A href="http://www.ecst.csuchico.edu/~beej/guide/net/">
     Beej's Guide to Network Programming</a></li>
<LI><A href="http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html">A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux</a></li>
<LI><A href="http://www2.linuxjournal.com/lj-issues/issue17/1136.html">
     Writing mouse-sensitive program (gpm)</A></LI>
</UL>
