C
- Appeared in:
- 1972
- Influenced by:
- Influenced:
- Paradigm:
- Typing discipline:
- File extensions:
- .c .h
- Dialects:
- Versions and implementations (Collapse all | Expand all):
C is an imperative (procedural) general-purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system.
It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. C was therefore useful for many applications that had formerly been coded in assembly language.
Despite its low-level capabilities, the language was designed to encourage machine-independent programming. A standards-compliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with little or no change to its source code. The language has become available on a very wide range of platforms, from embedded microcontrollers to supercomputers.
Elements of syntax:
| Inline comments | // (C99 only) |
|---|---|
| Nestable comments | #if 0 ... #endif |
| Non-nestable comments | /* ... */ |
| Case-sensitivity | yes |
| Variable identifier regexp | [_a-zA-Z][_a-zA-Z0-9]* |
| Variable assignment | = |
| Variable declaration | type variable |
| Variable declaration with assignment | type variable = value |
| Block | { ... } |
| Physical (shallow) equality | == |
| Physical (shallow) inequality | != |
| Comparison | < > <= >=, strcmp for strings |
| Function definition | returntype f(type1 p1, type2 p2, ...) |
| Function call | f(a, b, ...) |
| Function call with no parameters | f() |
| If - then | if (condition) ... |
| If - then - else | if (condition) ... else ... OR ( condition ? ... : ... ) |
| Loop forever | while (1) ... |
| While condition do | while (condition) ... |
| Do until condition | do ...while (!condition) |
| For each value in a numeric range, 1 increment | for (int i = 1; i <= 10; i++) ... |
| For each value in a numeric range, 1 decrement | for (int i = 10; i >= 1; i--) ... |
Examples:
Hello, World!:
Example for versions Turbo C++ 1.01, g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
Factorial:
Example for versions gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4This example uses recursive factorial definition. Note that 13! and larger causes an overflow, so the last lines of the output look like this:
13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184
#include <stdlib.h> /* needed for EXIT_SUCCESS */
#include <stdio.h>
unsigned long long factorial(unsigned long long n)
{
if (n == 0) {
return 1;
} else {
return n * factorial (n - 1);
}
}
int main(void)
{
int n;
for (n = 0; n <= 16; n++) {
printf("%i! = %lld\n", n, factorial(n));
}
return EXIT_SUCCESS;
}
Fibonacci numbers:
Example for versions gcc 3.4.5, gcc 3.4.5 (Objective-C)This example uses recursive definition of Fibonacci numbers. Note the difference from C++ example: loop counter must be declared outside of the loop, and printf is used for output instead of std::out.
#include <stdio.h>
int fibonacci(int n)
{
return ( n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2) );
}
int main(void)
{
int n;
for (n=1; n<=16; n++)
printf("%d, ", fibonacci(n));
printf("...\n");
return 0;
}
Comments
]]>blog comments powered by Disqus
]]>