This commit is contained in:
Miloslav Ciz 2024-08-05 22:39:28 +02:00
parent 275c83d379
commit 793eff5870
19 changed files with 2117 additions and 1835 deletions

78
c.md
View file

@ -20,6 +20,80 @@ Mainstream consensus acknowledges that C is among the best languages for writing
**[Fun](fun.md)**: `main[-1u]={1};` is a C [compiler bomb](compiler_bomb.md) :) it's a short program that usually makes the compiler produce a huge binary.
## Examples
Let's write a simple program called **[divisor tree](divisor_tree.md)** -- this program will be interactively reading positive numbers (smaller than 1000) from the user and for each one it will print the [binary tree](binary_tree.md) of the numbers divisors so that if a number has divisors, the ones that are closest to each other will be its children. If invalid input is given, the program ends. The tree will be written in format `(L N R)` where *N* is the number of the tree node, *L* is its the node's left subtree and *R* is the right subtree. This problem is made so that it will showcase most of the basic features of a programming language (like control structures, function definition, [recursion](recursion.md), [input/output](io.md) etc.). Let's from now on consider this our standardized program for showcasing programming languages.
Here is the program written in C99 (let this also serve as a reference implementation of the program):
```
#include <stdio.h> // include standard I/O library
// recursive function, prints divisor tree of x
void printDivisorTree(unsigned int x)
{
int a = -1, b = -1;
for (int i = 2; i <= x / 2; ++i) // find two closest divisors
if (x % i == 0)
{
a = i;
b = x / i;
if (b <= a)
break;
}
putchar('(');
if (a > 1)
{
printDivisorTree(a);
printf(" %d ",x);
printDivisorTree(b);
}
else
printf("%d",x);
putchar(')');
}
int main(void)
{
while (1) // main loop, read numbers from the user
{
unsigned int number;
printf("enter a number: ");
if (scanf("%u",&number) == 1 && number < 1000)
{
printDivisorTree(number);
putchar('\n');
}
else
break;
}
return 0;
}
```
Run of this program may look for example like this:
```
enter a number: 32
((((2) 4 (2)) 8 (2)) 32 ((2) 4 (2)))
enter a number: 256
((((2) 4 (2)) 16 ((2) 4 (2))) 256 (((2) 4 (2)) 16 ((2) 4 (2))))
enter a number: 7
(7)
enter a number: 0
(0)
enter a number: 4
((5) 15 (3))
enter a number: quit
```
## History and Context
C was developed in 1972 at [Bell Labs](bell_labs.md) alongside the [Unix](unix.md) operating system by [Dennis Ritchie](dennis_ritchie.md) and [Brian Kerninghan](brian_kerninghan.md), as a successor to the [B](b.md) language ([portable](portability.md) language with [recursion](recursion.md)) written by Denis Ritchie and [Ken Thompson](ken_thompson.md), which was in turn inspired by the the [ALGOL](algol.md) language (code blocks, lexical [scope](scope.md), ...). C was for a while called NB for "new B". C was intimately interconnected with Unix and its [hacker culture](hacking.md), both projects would continue to be developed together, influencing each other. In 1973 Unix was rewritten in C. In 1978 Keninghan and Ritchie published a book called *The C Programming Language*, known as *K&R*, which became something akin the C specification. In March 1987 [Richard Stallman](rms.md) along with others released the first version of [GNU C compiler](gcc.md) -- the official compiler of the [GNU](gnu.md) project and the compiler that would go on to become one of the most widely used. In 1989, the [ANSI C](ansi_c.md) standard, also known as C89, was released by the American ANSI -- this is a very well supported and overall good standard. The same standard was also adopted a year later by the international ISO, so C90 refers to the same language. In 1999 ISO issues a new standard that's known as C99, still a very good standard embraced by [LRS](lrs.md). Later in 2011 and 2017 the standard was revised again to C11 and C17, which are however no longer considered good.
@ -288,10 +362,6 @@ The following are some symbols ([functions](function.md), [macros](macro.md), ..
| *clock()* | *time.h* | Returns approx. CPU cycle count since program start. |`printf("CPU ticks: %d\n",(int) clock());`|
| *CLOCKS_PER_SEC* | *time.h* | Number of CPU ticks per second. |`int sElapsed = clock() / CLOCKS_PER_SEC;`|
## Some Programs In C
TODO
## See Also
- [B](b.md)