C , by Dennis Ritchie, is a programming language, most notable as the implementation language of Tcl ;)
The ANSI committee as well as the ISO committee have approved a standard reference for C.
Support for C is traditionally provided by a compiler. Some efforst at a C interpreter also exist.
Most Unix systems provide vendor-supported compilers. They are often unbundled products these days.
Support for C can also take the form of a run-time compiler.
tcc provides a libtcc library, which allows a running program to compile and link C code without touching disk. In milliseconds for a small chunk of code. (x86 only as of Jun 2003)
AMG: One annoying thing about C is that, like Fortran, it uses = as its assignment operator. This is very easy to confuse with the comparison operator ==, resulting in either of the following two coding errors:
x == 5; /* Nothing happens, except maybe a warning if -Wall is on. */ if (x = 6) {...} /* Unintended assignment and incorrect logic. */
Algol uses := as its assignment operator, and C is ancestrally derived from Algol, so I was always mystified about why C reverted to Fortran's assignment syntax. But I just now figured it out, I think. A := assignment operator looks almost identical to C's != inequality operator, so using := instead of = for assignment simply trades one potential coding error for another. To fix that problem, != would have to be replaced with, say, /= (borrowed from Ada), but that (1) collides with the compound division and assignment operator /= and (2) breaks symmetry with the logical negation operator !. Or instead use \=, but that will behave unexpectedly when placed inside double quotes. Or use <> and be like BASIC and SQL. Shrug!
[name redacted]: the change from := to = was made during the transition from BCPL to B (and simply kept when going from B to C), and IIRC the reason was that Ken Thompson felt that an operator that was going to be used as often as assignment was should be as short as possible. The reasons you mention may of course also have affected his decision, but there was never any secondary ambiguity with "divide-assign", which was written as =/ in B.
Larry Smith: Frankly, in this day and age, assignment should be "←". We need to start using unicode for programs and frankly, it's past time to worry about the kind nonsense we have had to put with in ascii's limitations. Ascii is so 20th century. Swap should be ↔,less than, <, greater, >, equal, =, multiply and divide, × and ÷ and lessthan or equal and greater or equal should be ≤ and ≥. APL, essentially the only programming languages to get this, was right. J is, in fact, something of a step backward at the very moment in time when APL's disadvantages are now fewer in number and less in nature. In fact, we could do a lot of this for "nearly" free by just bolting in an APL interpreter in place of the expression evaluator (or, more likely, as an alternative for some time). We could use http://www.nars2000.org/ to handle the grunt work. Likely all it would need would be a way to meld their separate notions of variables (perhaps simply by adding the new types into the Tcl type, since NARS2000 can handle some rather unique types) but, as with Tcl, everything is essentially a string because it must, conceptually, pass through the interpreter's line-oriented interface, as unicode.
EMJ: Unicode for programs? One of these decades, it's too much of a revolutionary change to be viable now. APL is a different country, too many people will not get it.
Larry Smith: I try to look beyond my own limitations. I am far from an APL expert, in fact, my APL has largely bit-rotted in my brain - but it remains one of the most popular programming languages for extremely heavy-duty mathematics, and many, many people do "get" that. Rather than sidling up to it unintentionally, it would be good for Tcl, as well as APL, to join the two. Each is especially good at one particular domain that can be hard for the other. Having it available as a back end for heavy math while tcl handles GUI and control functions make for a very powerful combination, especially since previous attempts at adding GUI concepts to APL have not, in my opinion, anyway, been very successful. Since it marries two essentially disjoint programming communities to their mutual advantage, it would change the market dynamics of both significantly.
As for unicode, Tcl is uniquely good with unicode, and should be pushing this boundary as a natural advantage over other programming systems. For all that they are supposedly "based" on unicode, neither java nor javascript are graceful about how they handle unicode, and Tcl's basic rules would adapt to unicode input without so much as a satisfied burp. Actually, as I pointed out in https://wiki.tcl-lang.org/48736 Tcl is very nearly there already. It cheerfully accepts unicode characters in-line.
bch: One thing that I do in C (indeed, Perl too) is use typical rvalues on the left side when doing comparisons, which will fail compilation if the assignment operator is mistakenly used instead of the comparison operator. Eg: use if(9==a){...} instead of if(a==9){...}; in the case of mistakenly using assignment operator, this will fail if(9=a){...}, which is good; the programmer will be notified that his intent was mis-typed. If the rvalue is indeed on the right, though, one would have typed if(a=9){...}, which will compile, but is unlikely to represent the programmer's intent, and lead to hard-to-find bugs. Reading expressions written this way (w/ rvalues on left) may seem "backward" to some people, and the practice is affectionately dubbed Yoda Conditions , [L3 ]
AMG: Cute. I've seen that style too. Unfortunately, it doesn't help when both sides of the comparison (or unintended assignment) are valid lvalues. Thankfully, gcc -Wall provides useful warnings:
x == y; | warning: statement with no effect |
if (x = y) {...} | warning: suggest parentheses around assignment used as truth value |
To silence the latter warning on a case-by-case basis, type: if ((x = y)) {...}. In my opinion, the extra parentheses highlight the fact that this expression isn't Business As Usual, so the reader will look at it more carefully and is more likely to notice that it uses assignment instead of comparison. Of course, only silence this warning when the code really is doing what you intend, and please put a comment nearby to testify to this fact and explain why. Also, watch your warnings like a hawk, and don't release code that emits any warnings in your chosen build environment(s). (You can't control if some alien compiler puts out weird warnings because it used MS-Word to grammar-check your comments.)
Sometimes I really do want my conditional expression to have the side effect of assigning into a variable. I need this when I want to save some intermediate value of a complex conditional for later processing. A simple example is calling a syscall (read()) that either returns a useful result (number of bytes read) or an error flag (-1, check errno for details).
if ((ret = read(fd, buf, max)) == -1) { perror("read"); } else { printf("read %d bytes\n", ret); }
This style is also available in Tcl:
proc getline {} {...} while {[set line [getline]] ne ""} { puts "got header line: $line" } puts "got blank line, the header is now done"
As you can see, Tcl makes it impossible to mix up assignment and comparison. ;^)
PYK 2015-07-30: Undefined behaviour, in combination with optimizing compilers, leads to unpleasant surprises. Here is one from the Linux kernel, as described by A Guide to Undefined Behaviour in C and C++, Part1 , John Regehr, 2015-07-21:
static void __devexit agnx_pci_remove (struct pci_dev *pdev) { struct ieee80211_hw *dev = pci_get_drvdata(pdev); struct agnx_priv *priv = dev->priv; if (!dev) return; ... do stuff using dev ... }
The error is that since the behaviour of dev->priv is undefined if dev is NULL, a compiler might decide to omit the following check for NULL, leading the program to do stuff using dev when dev is NULL, and even though there is an explicit check in the code for the case that dev is NULL. Apparently this is exactly what some versions of gcc do.
PYK 2020-07-18: An annoyance that has bled over from C into Tcl and other languages is that bitwise operators have lower precedence than the equality operator. In The Development of the C Language* , Dennis Ritchie explains that in the B programming language & is a bitwise operator in ordinary contexts but a logical operator in conditional contexts. The late introduction into C of the && and || operators to remove this context sensitivity meant that in conditional expressions translated from B && should sometimes replace &. In B, it was common to write
if (a==b & c) ...
, to check that that a is equal to b, and c is not 0. In order to accomodate this idiom, && and || were given lower precedence than ==. Unfortunately, this means that in order to compare the result of a bitwise operation. An additional set of parenthesis is required to get the intended meaning:
if ((a&mask) == b) ...
How does performance in Tcl compare to comperable programs in C?
For instance, certainly there are types of programs which can likely be developed more quickly in Tcl than in C.
Are there types of things that run faster in Tcl than C? If not, how much slower than C is Tcl? 10 times slower? 50 times slower? 100 times slower?
AMG: I just thought of a cute alternate name for C: "lightspeed". You know, 'cuz physicists use the constant "c" to represent the speed of light. :^) I leave it up to you to figure out if this name has any deeper meaning. At least it would give C an excuse to have a snazzy, energetic theme song .