C (programming language)

From Bauman National Library
This page was last modified on 8 June 2016, at 21:16.
C
Text in light blue serif capital letters on white background and very large light blue sans-serif letter C.
The C Programming Language (often referred to as "K&R"), the seminal book on C
Paradigm Imperative (procedural), structured
Designed by Dennis Ritchie
Developer Dennis Ritchie & Bell Labs (creators); ANSI X3J11 (ANSI C); ISO/IEC JTC1/SC22/WG14 (ISO C)
Stable release C11 / December 2011; 10 years ago (2011-12)
Typing discipline Static, weak, manifest, nominal
OS Cross-platform
Filename extensions .c, .h
Website {{#property:P856}}
Major implementations
GCC, Clang, Intel C, MSVC, Pelles C, Watcom C
Dialects
Cyclone, Unified Parallel C, Split-C, Cilk, C*
Influenced
Numerous: AMPL, AWK, csh, C++, C--, C#, Objective-C, BitC, D, Go, Rust, Java, JavaScript, Julia, Limbo, LPC, Perl, PHP, Pike, Processing, Python, Seed7, Vala, Verilog (HDL)[1]

C - is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from supercomputers to embedded systems.

C was originally developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs, and used to re-implement the Unix operating system. It has since become one of the most widely used programming languages of all time, with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the American National Standards Institute (ANSI) since 1989 (see ANSI C) and subsequently by the International Organization for Standardization (ISO).

History

K&R C

In 1978, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming Language. This book, known to C programmers as "K&R", served for many years as an informal specification of the language. The version of C that it describes is commonly referred to as K&R C.

K&R introduced several language features:

  • standard I/O library
  • long int data type
  • unsigned int data type
  • compound assignment operators of the form =op (such as =-)

In the years following the publication of K&R C, several features were added to the language:

  • void functions (i.e., functions with no return value)
  • functions returning struct or union types (rather than pointers)
  • assignment for struct data types
  • enumerated types
  • specifier constants (const)
  • float type

ISO C

In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C. X3J11 based the C standard on the Unix implementation; however, the non-portable portion of the Unix C library was handed off to the IEEE working group 1003 to become the basis for the 1988 POSIX standard. In 1989, the C standard was ratified as ANSI X3.159-1989 "Programming Language C". This version of the language is often referred to as ANSI C, Standard C or sometimes C89.

In 1990, the ANSI C standard (with formatting changes) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990, which is sometimes called C90.

One of the aims of the C standardization process was to produce a superset of K&R C, incorporating many of the subsequently introduced unofficial features. The standards committee also included several additional features such as function prototypes (borrowed from C++), void pointers, support for international character sets and locales, and preprocessor enhancements. Although the syntax for parameter declarations was augmented to include the style used in C++, the K&R interface continued to be permitted, for compatibility with existing source code.

After the ANSI/ISO standardization process, the C language specification remained relatively static for several years.

C99

The C standard was further revised in the late 1990s.

New features of C99:

  • inline functions
  • new types: long long int, bool, complex
  • variable-length arrays
  • support for one-line comments beginning with //
  • new library functions such as snprintf
  • new headers such as stdint.h
  • named structures initialization

C11

In 2007, work began on another revision of the C standard.

The C11 standard adds numerous new features to C and the library.

Main changes:

Overview

Like most imperative languages, C has facilities for structured programming and allows lexical variable scope and recursion, while a static type system prevents many unintended operations. In C, all executable code is contained within subroutines. Function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values. C program source text is free-format, using the semicolon as a statement terminator and curly braces for grouping blocks of statements.

C (like Unix OS) was developed by programmers for programmers. Dispite this fact this langugage has wide implementation field.

The main purpose of C development was making program design much more easier with minimum errors.

The C language also exhibits the following characteristics:

  • There is a small, fixed number of keywords, including a full set of flow of control primitives: for, if/else, while, switch, and do/while
  • There are a large number of arithmetical and logical operators, such as +, +=, ++, &, ~
  • More than one assignment may be performed in a single statement
  • Function return values can be ignored when not needed
  • Typing is static, but weakly enforced: all data has a type, but implicit conversions can be performed
  • Declaration syntax mimics usage context.
  • User-defined (typedef) and compound types are possible.
  • Low-level access to computer memory is possible by converting machine addresses to typed pointers.
  • Procedures (subroutines not returning values) are a special case of function, with an untyped return type void
  • Functions may not be defined within the lexical scope of other functions
  • A preprocessor performs macro definition, source code file inclusion, and conditional compilation
  • There is a basic form of modularity: files can be compiled separately and linked together
  • Complex functionality such as I/O, string manipulation, and mathematical functions are consistently delegated to library routines

C doesn't maintain:

Data types

C contains different types of values: integer with various length (short int, lonh int) with sign or without (unsigned), with floating point (float, double), character, enumerable type, structures, unions.

Simple:

Composite:

Memory managment

One of the most important functions of a programming language is to provide facilities for managing memory and the objects that are stored in memory. C provides three distinct ways to allocate memory for objects:

  • Static memory allocation: space for the object is provided in the binary at compile-time; these objects have an extent (or lifetime) as long as the binary which contains them is loaded into memory
  • Automatic memory allocation: temporary objects can be stored on the stack, and this space is automatically freed and reusable after the block in which they are declared is exited
  • Dynamic memory allocation: blocks of memory of arbitrary size can be requested at run-time using library functions such as malloc from a region of memory called the heap; these blocks persist until subsequently freed for reuse by calling the library function realloc or free

These three approaches are appropriate in different situations and have various tradeoffs. For example, static memory allocation has little allocation overhead, automatic allocation may involve slightly more overhead, and dynamic memory allocation can potentially have a great deal of overhead for both allocation and deallocation. The persistent nature of static objects is useful for maintaining state information across function calls, automatic allocation is easy to use but stack space is typically much more limited and transient than either static memory or heap space, and dynamic memory allocation allows convenient allocation of objects whose size is known only at run-time. Most C programs make extensive use of all three.

Where possible, automatic or static allocation is usually simplest because the storage is managed by the compiler, freeing the programmer of the potentially error-prone chore of manually allocating and releasing storage. However, many data structures can change in size at runtime, and since static allocations (and automatic allocations before C99) must have a fixed size at compile-time, there are many situations in which dynamic allocation is necessary. Prior to the C99 standard, variable-sized arrays were a common example of this. Unlike automatic allocation, which can fail at run time with uncontrolled consequences, the dynamic allocation functions return an indication (in the form of a null pointer value) when the required storage cannot be allocated.

Libraries

The C programming language uses libraries as its primary method of extension. In C, a library is a set of functions contained within a single "archive" file. Each library typically has a header file, which contains the prototypes of the functions contained within the library that may be used by a program, and declarations of special data types and macro symbols used with these functions. In order for a program to use a library, it must include the library's header file, and the library must be linked with the program, which in many cases requires compiler flags.

The most common C library is the C standard library, which is specified by the ISO and ANSI C standards and comes with every C implementation.

Examples

The program prints "hello, world" to the standard output, which is usually a terminal or screen display.

#include <stdio.h>

int main (void)
{
  puts ("Hello, World!");
  return 0;
}

Bubble sort:

#include<stdio.h>
#define N 1000
 int main()
 {
    int n, i, j;
    int a[N];
    scanf_s("%d", &amp;n);

    for(i = 0 ; i < n; i++)
	{ 
        scanf_s("%d", &amp;a[i]);
    }
    for(i = 0 ; i < n - 1; i++)
	{ 
       for(j = 0 ; j < n - i - 1 ; j++)
	   {  
           if(a[j] > a[j+1])
		   {           
              int tmp = a[j]; a[j] = a[j+1] ; a[j+1] = tmp; 
           }
        }
    }
 }

Further reading

External links

  1. ISO C Working Group official website
  2. A history of C
  3. comp.lang.c Frequently Asked Questions
    1. "Verilog HDL (and C)" (PDF). The Research School of Computer Science at the Australian National University. 2010-06-03. Retrieved 2013-08-19. 1980s: ; Verilog first introduced ; Verilog inspired by the C programming language