Sunday, 27 May 2012

C language, for system programming

C has known success thanks to the liberty if offers to programmers. Its drawback is the difficulty to debug programs. It is fast, and a huge collection of APIs is available.
The C language was originally designed from 1969 to 1972, by Dennis Ritchie to program the Unix operating system. See The development of the C language for a short history.
The goal of C was to be portable.
The tutorial of the language by Dennis Ritchie and Brian Kernighan is considered one of the best ever written.
C language, Dennis Ritchie He inspired many other languages ​​including C++, Java, C# from Microsoft and more recently Go and Dart  from Google.

Description of the C language

A C program is a set of functions that return or not a value, and global variables. Everything can be evaluated as an expression, and this is not necessarily an advantage for security.
Functions and variables have prototypes in header files for shared use of the source file.
The programmer must manage itself the memory, using pointers and functions to allocate of free fields of memory.
It is portable with some restrictions, additional variable types for exemple, are specific to compilers.
The ISO C11 standard defined in 2011 adds new features to the C language: multi-threading, Unicode, lambda functions, anonymous structures and unions when nested.

Data structures:

Scalars: int, long, char, char *.
struct creates an object type made of of several variables and was the forerunner of objects.
union can give several identifiers to a variable and is used primarily with struct.
typedef to define new data types ..

Control structures:

  • if ... else
  • switch ... case ... default
  • while
  • for
  • do ... while
  • break, continue, return
  • goto

Symbols:

// and /* */ comments.
& | && || logical operators.
# directive to the pre-processor.
Compound symbols allows to perform several operations on a same variable. For example, a += b adds b to a and assigns the result toa.

The while loop:

while(x < y)
{
   ... statements ...
}

Defining a function:

int name(char *x, int y)
{
   ... statements ...

   return(z);
}

Sample code

Displaying the list of characters in a string:
char *s = "demo";
int l = strlen(s);
for(int i = 0; i < l; i++)
{
   char c = s[i];
   printf("%c\n", c);
} 

C sites and tools