ctut/README.md

88 lines
3.7 KiB
Markdown
Raw Normal View History

2019-03-14 04:13:46 +00:00
# C Musings
Some musings on C-family languages, by Nora for Ania!
## History of the C Family
In the early days of computing, MIT, General Electric, and Bell Labs created MULITCS,
the Multiplexed Information and Computing Service. This operating system introduced
many ideas that are commonplace today. It also kind of sucked, and the project fell apart.
From the ashes rose UNIX, an operating system which, like all OSes at the time, was written
entirely in non-portable assembly code for a specific machine, the DEC PDP-10. It used
an interpreted language called B for some non-performance-critical code that needed to be
easy to modify.
As it became popular in the early 1970s, Dennis Ritchie, Brian Kernighan, and others decided
to port it to a number of other computers. To ease this task, they invented a computer language
that abstracted over the specifics of machine language of any particular computer, but was
close enough to the model exposed by most machines that, with a little machine-specific
assembly code, the only thing that had to be ported to each new machine was the compiler.
The compiler is a "simple" piece of code that translates the C code to machine-specific
instructions. Nowadays, compilers are extremely complex, as they perform optimization as
well as simple compilation.
Because C is so low level, it is entirely _imperative_. All of its high-level features,
such as structures and function pointers, come from the way the machine works, not from
any magic (runtime) applied by the compiler.
### C++
C++ is an invention of Bjarne Stroustroup, a brilliant computer scientist who decided to
apply object oriented principles to C. The result was an absolute mess of a language
that is nonetheless very powerful and, as time goes on, is becoming more and more safe
and easy to use.
It has somewhat more of a runtime then C, and the compiler does a lot more work, including
permitting type-system level templating.
### C#
C# is Microsoft's answer to Java. It's really similar to Java and has very little to do
with C.
## The C Programming Language
That's the name of a [terrible book](https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628)
that you shouldn't read. I did, it blew my mind and I learned nothing.
Anyway, what defines the C language?
C is **statically typed**. That means, like Java and unlike Python, a variable has to
keep the same type throughout the program:
```c
int a = 24;
a = "Hello, world!"; // Compiler error
```
C is **weakly typed**. That means, unlike both Java and Python, C will do it's _very best_
to do _something_ even when you give it types that make little sense, _based on the way
the PDP-10 would have done it in its CPU_.
```c
int a = 25;
char b = 'b';
a + b; // This is an integer of the value 25 plus the integer value of the letter b.
```
C is **procedural**. Its primary abstraction is the procedure call, or function call.
This is as opposed to object-oriented languages, for example.
Finally, C is **memory unsafe**. Because it has no runtime and gives you access to the
very lowest level of the machine (or at least does a good job of pretending), you can
do silly things without anyone stopping you.
```c
char[] c = "Hello, world!"; // A string is an array of characters.
char nonsense = c[134]; // This will cause a problem, probably.
```
This code won't throw an `IndexOutOfBounds` exception or anything; there's no runtime
to check that. Instead, the compiler will very earnestly try to write code that does this;
it will get the address of the start of the array `c`, compute the memory address 134 bytes
later, and try to see what's there. This will certainly be gibberish and might cause the
operating system to kill your program.
2019-03-14 03:51:04 +00:00