C (programming language): Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Pat Palmer
(changing order of info in introduction)
imported>Pat Palmer
(→‎Pros & Cons of C: softening claim of platform independence)
Line 52: Line 52:
*C is very flexible. An indication of this feature is the fact that many low level software packages that have close interaction with hardware are written in C. For example, most of [[Unix]] is written in C. Most [[compiler]]s and [[interpreter]]s are written in C. C gives programmers access to hardware, enabling them to manipulate [[memory]]. This feature is advantageous but can prove problematic if misused.
*C is very flexible. An indication of this feature is the fact that many low level software packages that have close interaction with hardware are written in C. For example, most of [[Unix]] is written in C. Most [[compiler]]s and [[interpreter]]s are written in C. C gives programmers access to hardware, enabling them to manipulate [[memory]]. This feature is advantageous but can prove problematic if misused.


*C is portable. Most C programs written on one system can be run on another system without much modification. C [[compiler]]s are available for most systems.
*C has been standardized and is portable with some diligence, since some internal storage formats are platform dependent. Today, many C programs written on one system can be run on another system without much modification. C [[compiler]]s are available for most systems.


===Cons===
===Cons===

Revision as of 08:26, 27 July 2008

This article is developing and not approved.
Main Article
Discussion
Related Articles  [?]
Bibliography  [?]
External Links  [?]
Citable Version  [?]
Tutorials [?]
 
This editable Main Article is under development and subject to a disclaimer.
For other uses, see C (disambiguation).

C is a general-purpose, procedural computer programming language which is still in use more than thirty years after its creation. C was developed in 1972 by Dennis Ritchie and Brian Kernighan (then of Bell Laboratories) for use with the Unix operating system, and, before it entered the standards process, in a reference book.[1] . C's name derives from those of two of its predecessors, Basic Computer Programming Language (BCPL)[2], and a BCPL variant,B[3]. The language was later implemented for many different computer platforms and has been standardized by ANSI and ISO. As of 2007, versions of C are still used, especially for writing operating system software and embedded programs (for gadgets such as smart phones). The syntax and scope rules of C were also adopted by several later programming languages, including C++, Java, and C#. Javascript uses similar syntax but has different scope rules.

Syntax of the C family of programming languages

Many basic characteristics of C syntax, along with its stack-based scope behavior, have been adopted by several subsequent programming languages, including C++, Java, Javascript, C# and (to some extent) Ruby. The adopted characteristics tend to include case sensitivity, ending of statements with a semi-colon (;), use of { } to enclose blocks of code, enclosing procedure parameters inside ( ) pairs, and allowing temporary variables to be declared inside blocks or procedures (which are semantically destroyed subsequent to that procedure's execution, and reinitialized from scratch during subsequent procedure executions).

Hello World

#include <stdio.h>

int main(void) {
   printf("Hello, world!\n");
   return 0;
}

Analysis of the example

The Hello World program (see above) appears in many programming languages books and articles as a cursory introduction into a language's syntax. It was introduced in the book The C Programming Language[1].

#include <stdio.h> tells the precompiler to include the contents of the header file stdio.h, which declares standard input and output functions into the program before compiling.

int main(void) { tells the compiler that there is a function named main which expects no parameters (void) and will return an integer number to the caller (int). Due to a standard convention of the language, main is the first function called after the execution environment of the program has been set up. The opening curly brace following int main(void) denotes the beginning of the function.

printf("Hello, world!\n"); will make the program output Hello, world! and a new line (\n) on the screen. printf is itself a function similar to main but predefined in a library (libc) and linked into the program at compile time or runtime. The trailing semicolon is the end of statement marker in C.

return 0; defines the value to be returned from main and leaves the function back to its caller, some standard C startup code. After some additional cleanup that code will pass the 0 on to the operating system, to which it means 'success'.

} signals the end of the function definition to the compiler.


Pros & Cons of C

Pros

  • C is very efficient. The flexibility it affords programmers can be harnessed to make C programs very compact and consequently run quickly.
  • C is very flexible. An indication of this feature is the fact that many low level software packages that have close interaction with hardware are written in C. For example, most of Unix is written in C. Most compilers and interpreters are written in C. C gives programmers access to hardware, enabling them to manipulate memory. This feature is advantageous but can prove problematic if misused.
  • C has been standardized and is portable with some diligence, since some internal storage formats are platform dependent. Today, many C programs written on one system can be run on another system without much modification. C compilers are available for most systems.

Cons

  • C, though flexible, gives the programmer many tools which if misused can be problematic. For example, pointers can make C code very hard to debug, and errors very hard to trace. See the buffer overflow article for a longer discussion.
  • The low level nature of C makes it relatively harder to learn than other programming languages.

See also

References

  1. 1.0 1.1 Kernighan, B. & D. Ritchie (1978), The C Programming Language, Prentice Hall
  2. Richards, Martin (21 July 1967), Richards's BCPL Reference Manual, Memorandum M-352 of MIT Project MAC
  3. Thompson, Ken (7 January 1972), Users' Reference to B