Jump to content

Talk:C (programming language)/Archive 1

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Fresheneesz (talk | contribs) at 00:34, 3 June 2006 (fixing). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

00:33, 3 June 2006 (UTC)

This is an archive, which contains full sections which themselves only contain (to my knowlege) comments made before 2006.

Confusing wording

  >Parameters that are always passed to functions by value, never by reference

It is typical in C programming, and promoted by the C syntax, to pass pointers to functions. I agree that pointers are passed as parameters and by value, however, functions typically do end up with pointers. Therefore, a function may manipulate the value of variables, arrays and structures which have been passed to it using pointer parameters, which is the point of the issue. I suggest this to be more concise, and accurate:

  Parameters and references are always passed to functions by value.

What ??? this whole item is silly and wrong. this isn't C++, its C. Pointers are refererences: I make a struct. I pass the address of the struct to a function. What about this isn't pass by reference?

Actually, scalar arguments are passed by value, but arrays are passed by name.
No, they aren't. They decay to pointers when passed to a function (like they do in almost any expression), which are then passed by value. --Mellum 06:09, 13 September 2005 (UTC)
Pointers themselves (being scalars) are passed by value, but the pointer may be be used to pass by name / address (e.g., &num). (Remember to sign your contributions by entering four tildes) wrp103 (Bill Pringle) - Talk 01:38, 13 September 2005 (UTC)
Passing by name is different from passing by reference (which is similar to what happens if you pass the adress obtained with &num). Call by name reevaluates the expression every time it is needed, and is not supported by C. --Stephan Schulz 22:18, 18 September 2005 (UTC)
The statement is well worded. It is true that parameters are always passed by value in C, and it is the only parameter-passing mechanism C supports. However, one can emulate pass by reference to some degree by passing pointers (which are also passed by value) to a data object. Denis Kasak 11:12, 23 September 2005 (UTC)
  Some features that C lacks that are found in other languages include:
  >Automatic garbage collection

Local variables defined in a function are unallocated when the function is exited, unless defined static. Thus, C implementations do automatically free memory without the programmer needing to explicitly unallocate memory. Unless the memory has been allocated dynamically using malloc(), in which case, specific deallocation is required. I suggest:

  Automatic garbage collection of dynamically allocated memory
I believe the issue here is that in some languages (e.g., Java), you can explictly allocate data but you don't have to (or can't) explicitly free data. In Java you can use the new keyword to create an object, and Java will free it when it goes out of scope. You can do the same thing in C++, but you have to explicitly free each object. Local variables defined within a function are automatic variables, not dynamically created variables. wrp103 (Bill Pringle) - Talk 01:38, 13 September 2005 (UTC)
Automatic garbage collection commonly refers to automatic freeing of dynamically allocated objects. Local objects in C are not dynamic and therefore not garbage collected as such; their automatic nature is rather a consequence of the design of the language. Their scope is limited to the function in which they had been defined and they do not exist outside it. This kind of behaviour is not commonly referred to as 'garbage collection' and in my opinion nothing needs to be reworded. Denis Kasak 11:58, 23 September 2005 (UTC)

Reorganization!

I propose a reorganization of the series on the C programming language. I put forth the following four general article headings:

  1. History of the C programming language—The creation and original uses of C, its development at AT&T/Bell Labs, the various attempts at standardising
  2. Syntax of the C programming language—The syntax (grammar rules, not behavior) of C
  3. Semantics of the C programming language—The semantics (behavior and relationships of syntatic components) of C
  4. Standard C library—History of std library, list of headers, platform variations (BSD, GNU, VC++/Win32, Borland/Win32, etc.)

I think that the current situation is a mess and needs reform. Comments welcome, of course.—Kbolino 06:00, Apr 7, 2005 (UTC)

Sounds good. I assume you will also have a main article that will provide an overview of the language and pointers to the subarticles w/ explanations of their content. I would suggest that we also add a section on common techniques, which would cover and/or refer to additional articles that cover other subjects (e.g., malloc, the dynamic array issue, etc.) wrp103 (Bill Pringle) - Talk 16:03, 7 Apr 2005 (UTC)

Any reorganization should probably reflect that an encyclopedia article on a programming language might have several goals to address different kinds of readers, but that being in any way a tutorial (or even a reference manual) is probably *not* one of those goals. I can think of three worthy sections, all worthy of inclusion (i.e. as subsections):

  1. classification (among other programming languages)
  2. history (including versions of standards)
  3. description of salient features (minimal, lotsa punctuation, much functionality deferred to library functions, etc.)

There's definitely a place for a few examples, if a reader wants to be able to recognize a C program when he sees one. But I doubt that a full syntactic or semantic definition of the entire language is necessary or appropriate. Steve Summit 05:53, 13 July 2005 (UTC)


Am I the only one that thinks this article needs a serious trim? Large parts of it should be split onto their own page; the main page should be about 20% of what it currently is. Akihabara 09:23, 22 September 2005 (UTC)

O Operators

can somebody please expand on the O(1) req on operators? O(1) as a function of what? Is a C impl on x86 for example is not complaint if for example the expr:

' x >>= c; ' is O(c) ? (since x86 shl reg,cl is O(cl)), how about mult/div? --Oyd11

I would try to explain it if I could find it, but I can't. Does this comment refer to some obsolete text? Steve Summit


Do you think it's good to use Evolution for writing down the changes, or K and R C will suffice? Thanks, Uriyan

Actually I don't think either topic deserves a sub-page. I think a section on the main page would do. --drj

Eccentricity

In C, the months of the year are numbered wrong. They are all low by 1. For example: January is month 0. December is month 11. This is not an obvious problem to English-speakers, but if your native language uses numbers to name the months of the year...

Numbering from 0 is not eccentricity. It's how computers think. --Taw

That also has nothing at all to do with the C language, but with the standard libraries--that distinction should be made. --LDC
Then why aren't days-of-month numbered starting from 0? 193.167.132.66 08:53, 10 Feb 2005 (UTC)
Because you probably don't have an array that stores the days of the month in text format. It is quite common to have an array of character strings w/ "Jan", "Feb", "Mar", etc. The same thing applies to week days ("Mon", "Tue", etc.) wrp103 (Bill Pringle) - Talk 15:43, 10 Feb 2005 (UTC)

I don't even think thats an eccentricity since its fairly common in programming to do it that way --Alan D

Fairly common today, but it wasn't in the Good Old Days. That makes it a fad, albeit a fairly long running one. Hint: make a list of languages since Day 1 and see when numbering from 0 began. GregLindahl

Java uses the same numbering scheme. Possibly because there is a zero-based array of month names. Although numbering from zero in both C and Java is more of a convenience for the routines that perform array handling than anything else. If your array starts at location AC00, the address of the 0th element is AC00, the address of element 1 is AC00 + element_length, the address of the 2nd element is AC00 + 2 * element_length, and so on. I prefer to start at element 1, but we're all pretty much stuck with the convention. (Perhaps I'll step up and start writing about software engineering, something I actually have some expertise in.) Ed Poor


Example date (works for C and Java): May 29
Month=4, day=29

Why is the month shifted but not the day of the month? Besides, if it is "the month is not a number", you deserve a slap in the face from your Korean (or Japanese or Chinese) secretary; ask her about it! In all these languages, May is literally "five month". (I think.) -- Juuitchan

Yes, but you'd translate it into english as 'month five' if you didn't want to use the name - 五月 is how you'd write it in chinese characters.

If you want to blame someone for the system in use, I'd suggest starting with the Jesuits, who as the scientific wing of the catholic church spread clockwork and steel cannons across much of asia. They also brought their date systems with them, which is why a 24 hour clock and seven day week is pretty much universally accepted.

The difference between day and month in terms of indexing can be reduced to that of the difference between both nominal and cardinal values and ordinal values.

The system we use uses nominal values for months, and ordinal values for days and years. It should probably use ordinal values for the lot. Note, though that Korean (and I expect Japanese and Chinese) uses cardinal values, rather than ordinal values for month. O-Ueol (五月) vs' O-Beon-JJae-Ueol (五番째月), except that normally the chinese characters aren't used in the second case.

This was probably far more than you wanted to know, but the point is that given that there are trivial mappings between these forms, the particular representation format chosen isn't that big a deal.

If you want to see _real_ problems with computational notions of time, I'd refer you to 'A Long, Painful History of Time' (Erik Naggum [1]).

-- 203.231.161.129

You know, some research shows the idea of clock work and units of 12 come from a much older basis even if promotion of that method was performed by others: This page examines the history of the 24 hour analog dial

-- laundrypowder


The claim that C is the dominant microcomputer applications language is now somewhat dubious, IMHO. In the Windows world, it's probably a toss-up between Microsoft's C++ and Visual Basic, I'd guess. C still rules for embedded systems (that is, the ones not written in Assembler), in the Unix world (particularly for apps that don't have a GUI), and people who can't be bothered remembering C++'s arcane semantics for multiple inheritance and operator overloading :) --Robert Merkel

How much Visual C++ is actually plain C with a tiny bit of C++ here and there? GregLindahl
Good question, but one which I don't know the answer to. The only big Windows development project I ever saw was in "real" C++, with a class hierachy etc. etc. It was an absolute PITA to work with, though - in the end I just gave up and wrote the code I needed by monitoring a socket interface this code had with a socket monitor I ginned up with Cygwin.--Robert Merkel
Any windows programming that uses MFC is making extensive use of C++, just by virtue of modelling windows in terms of objects. I think that's actually a good portion of Visual C++ programming.
Yup. Visual C++ (the language) is mostly C++. Of course, the product Microsoft Studio, Visual C++, has compilers for both the language C and the language C++ (and options to turn microsoft extensions to both of those language on or off). Secretly the two compilers are the same, but accepting different input languages.

Recent stats of what percent of code in RedHat–dash is written in which programing language, clearly shows that C# is dominant, at least on Unices. It's very probable that C++ is much more popular on Windows Word, but I seriously doubt that many apps are written in VB. --Taw


It would be nice to have links to an online manual and online tutorials. There probably are some with under the GNU Free Documentation License. --Hirzel


comp.lang.c (a wonderful resource for C, btw -- some very competent people there) recommends Tom Torfs' tutorial at http://www.geocities.com/tom_torfs/c.html and Steve Summit's class notes at http://www.eskimo.com/~scs/cclass/cclass.html.

Honestly, most online tutorials for C are terrible and demonstrate that the author has little clue about the actual C standard. --mgmei


Moved from article:

C is a high level language, meaning that the source code of a program can be written without detailed knowledge of the computer's CPU type. Before the program can be used, the source code must be translated into the required machine language by a compiler. In contrast, programs written in an assembly language can only be run on one type of CPU.
(The above definition of high level language is not meaningful. Consider the evolution of the x86 instruction set toward virtual machinehood. Does this mean that x86 assembly is a high level language?)
If we insert "of abstraction" between "level" and "langauge", the phrase becomes less ambiguous, but also
somewhat awkward. Regardless, can a language which saddles the programmer with responsibility for
memory management really be considered to have a high level of abstraction? C is clearly more abstract
than most assembly languages, but that hardly makes it high level.

I'm not sure what 203.231.161.129 means about "virtual machinehood", but I can see that under this definition, x86 assembly language could be considered a high level language due to the existence of emulators. -- Tim Starling 07:54 26 Jun 2003 (UTC)

Modern x86 implementations are more and more moving toward risc cores running virtual machines (in microcode or whatever) which provide x86 compatible instruction sets. Hyperthreading for example, is a case of attempting to exploit such an underlying architecture without affecting the definition of the x86 machine (ie, mapping many registers to the x86's few, so that you can increase some hparallelism).

In this regard the x86 architecture is shifting toward defining a virtual machine, much like JVM, rather than specifying a hardware cpu. Not that there is a meaningful distinction in any case.

Likewise, we can see C programs running in a virtual machine defined by the C standard, and supported by the runtime structure of the binary produced.

High and low level are fundamentally ideological terms, and have almost no objective meaning, nor objective definition what-so-ever. So far the only meaningful definition of level that I've found has been in terms of 'the ability to express invariant structure' with more being higher level. Note that by this definition, python ends up being only slightly higher than assembly, since it has almost no ability to define invariant structure.

I wish people would stop using these terms, anyhow, as they are very silly.

-- 203.231.161.129

B origin from Wombat encyclopedia

The page says the language B got its name from BCPL. But Wombat's encyclopedia says this is wrong. It says "B had nothing to do with BCPL. B was in fact a revision of an earlier language, bon, named after Ken Thompson's wife, Bonnie."
Jay 22:44 5 Jul 2003 (UTC)

Maybe it does but, at best, Wombat is a tertiary source, quoting a secondary source, Foldoc, and we have to ask ourselves "How do they know that ? What is their primary source for that information ?" Our source for the statement that "B is based on BCPL" is the primary source, User Reference for B, written by Ken Thompson, in which he states that:

B is a computer language directly descendant from BCPL. B 1s running at Murray Hill on the DEC PDP-11 computer under the UNIX-11 time sharing system. B is good for recursive, non-numeric, machine independent applications, such as system and language work.

This is a pretty unequivocal statement of B's ancestry from Ken Thompson himself. Having said that, I have no doubt that Ken reused code from his work on the bon compiler in writing the B compiler -- that's the nature of programming -- but according to Ken himself, the language design was based on BCPL, not bon, so it doesn't really matter what others say. -- Derek Ross | Talk 19:01, 2004 Jul 14 (UTC)

We're dealing with name-origins here, not the development of programming languages themselves, which you would realize is a different issue. You can have a look at the link in the below discussion #B from Bon or BCPL ?, for a primary source. Jay 19:16, 14 Jul 2004 (UTC)

And indeed I did as you will see if you read my comments there. However it turns out that the document concerned is a description of bon. It has nothing to say about B. The best information that we have about the link between the names is that which comes from Dennis Ritchie, and he favours the B from BCPL explanation.

I would also like to point out that the Wombat article which you quote does not appear to be talking about name-origins of B, but rather about the development of the programming languages themselves, which is, of course, a different issue. It states that the B language is a revision not of BCPL, but of bon (which I have shown to be contradicted by Ken Thompson's statements) and that the origin of the name bon is Ken's wife's name (which may be true) but it does not say anything about the origin of the name, B.-- Derek Ross | Talk 22:07, 2004 Jul 14 (UTC)

On looking more closely, the page talks about both name origins and prog. language development, and it claims B is from bon both ways, which as you say is contradictory. The languaging suggests that it is intended to debunking the B is from BCPL myth. The page also gives a reference to backup its claim "["The Programming Language B", S.C. Johnson & B.W. Kernighan, CS TR 8, Bell Labs (Jan 1973)].". So I guess until someone reads up the book and doesn't find the statements as mentioned, whats written on that page will get the benefit of doubt. Jay 19:06, 16 Jul 2004 (UTC)

Dennis Ritchie has made "The Programming Language B", available at [2]. It is in two sections, both of which I have read. Both mention the connection between B and BCPL. Neither mentions any connection between B and bon. In fact neither mentions bon at all, so I guess that we can discount what Wombat says. -- Derek Ross | Talk 20:56, 2004 Jul 16 (UTC)

Spelling of behaviour

I see that the spelling "behaviour" in place of "behavior" is used in the C programming language article. My copy of the C Standard uses "behavior". Should this Wikiopedia article use spelling consistent with the C language Standard? Or, maybe there is more than one English language version of the C Standard? LarryW 01:57, 3 Aug 2003 (UTC)

I think we should use the spelling of the ISO C standard for technical terms such as undefined behavior and unspecified behavior. I'm pretty sure that there's only one official English-language version of the ISO standard (at least for C99). --Zundark 10:31, 3 Aug 2003 (UTC)
OK -- LarryW 23:16, 3 Aug 2003 (UTC)

B from Bon or BCPL ?

There were no responses to my comment of 5th July suggesting B was a revision of bon and not BPCL as claimed. Hence I've made the change in the main page.
Jay 11:42, 10 Aug 2003 (UTC)

Jay,see http://cm.bell-labs.com/cm/cs/who/dmr/chist.html. I think the language there implies that C descended from BCPL, not bon. (At the end of the History: the setting section). -- Kushal kumaran
The line is not conclusive enough. Ritchie says "most probably", and also does not claim that the Bon theory is wrong. Moroever, the creator of "B" is Ken Thompson and the Bon theory is taken from a paper/book (Thompson 69) written by him (this is mentioned in the same line). Jay 18:35, Aug 20, 2003 (UTC)
Firstly, Ken Thompson states in the User Reference to B that B was designed and implemented by D. M. Ritchie and the author., so I think that we have to give quite a lot of credit to what Dennis Ritchie believes and if he says most probably, then as the acknowledged co-creator of B, he is in a much better position to know the truth than anyone other than Ken Thompson.
You're right, I didn't about know the contribution of Dennis Ritchie to B. The first paragraph of B programming language doesn't have a mention of Ritchie. Jay 19:06, 16 Jul 2004 (UTC)
Secondly, the [Thompson 69] reference is to Bon — an Interactive Language, which is an undated AT&T Bell Laboratories internal memorandum from around 1969 which describes the Bon language. B did not exist when it was written and thus it has nothing to say about the relationship between the two languages, although it may explain why Bon is so named.
How can you say B didn't exist when the memorandum was written (in 1969)? The same page (C history paper) says in the Acknowledgments section, "Ken Thompson created the B language in 1969-70". Moreover the author of the paper (Dennis M. Ritchie) has listed the references he used in writing the paper, and when he has mentioned Thompson 69 as a reference to the alternate theory, it does mean he has read up the memorandum to get the info. Considering that Ken Thomson initially developed B and Ritchie pitched in later, its safer to assume that Thompson's words have more weightage. Jay 19:06, 16 Jul 2004 (UTC)
The memorandum was written "ca. 1969" which could easily mean 1968 or 1970. Since it is a technical description of a MULTICS language, I think that it's highly likely that it was written between the time that Thompson finished work on bon and the end of Bell Labs involvement with MULTICS. Bell pulled out in April 1969, so that would place the memorandum sometime before May 1969. Work on Unix started in the summer after Bell Labs pulled out of Multics. At first Unix was written in assembler. Work on B started after Unix was substantially complete which puts it towards the end of 1969 or the beginning of 1970 (In fact according to Bell Labs pages on Unix History at [3], work on B didn't start until 1971 which can't be correct). So the memorandum on the bon programming language was probably written between 3 and 18 months before work on B started. -- Derek Ross | Talk 22:09, 2004 Jul 16 (UTC)
In any case the conclusive line implying that C descended from BCPL can be found in the User Reference to B as I stated earlier. -- Derek Ross | Talk 19:20, 2004 Jul 14 (UTC)
Did you mean B ? Jay 19:06, 16 Jul 2004 (UTC)
I meant C. I was referring to Kushal kumaran's response above. In any case saying that the statement is true for C, implies that it's true for B. -- Derek Ross | Talk 22:09, 2004 Jul 16 (UTC)

Other libraries

It would be nice to have a paragraph about other important libraries besides the standard C library.

Which other libraries?

Maybe glib, for instance. -- Taku 01:38, Nov 16, 2003 (UTC)


By 1973, the C language had become powerful enough that most of the kernel of the Unix operating system was reimplemented in C, perhaps following the examples of the Multics system (implemented in PL/I), Tripos (implemented in BCPL), and perhaps others.

But according to the TRIPOS page, TRIPOS was was developed in 1978, five years after Unix was re-implemented in C. Which is right? -- Khym Chanur


I would like to split off anatomy of C section to its own aritcle and merge ANSI C and ANSI C standard library with this article. It is more relevant to discuss history or standards than syntax details in this article, I think. Any thought or objection is welcomed. -- Taku 01:38, Nov 16, 2003 (UTC)


" During the late '70s, C began to replace BASIC as the standard microcomputer language," : BASIC was never a "Standard" programming language in the '70s. It was a teaching language. Only with the rise of MS's Visual basic did it become a real-world language, and it's still very limited in its application (it's mostly useful for GUIs). orthogonal 04:01, 26 Nov 2003 (UTC)

I think it depends on how you interpret the word "standard". If you mean "defined by formal written standards", then I'd agree that BASIC is not, and has never been, a standard language. But I don't think that's what's meant here. During the late 1970s, microcomputers were typically programmed in either assembly or BASIC - consider as examples the MITS Altair 8800 & 680, Commodore Pet, TRS-80, Apple 2, VIC-20 and Commodore 64. I'm rather suspicious of the IBM(1981) reference, though: I was using IBM PC's in 1989 and there wasn't a hint of C on them. -- Ortonmc 04:15, 26 Nov 2003 (UTC)
For amateur end users, perhaps; but I was taking it to men that most applications were written in BASIC, which I find dubious. orthogonal 04:26, 26 Nov 2003 (UTC)
It wasn't only a teaching language. At least one OS, DEC's RSTS/E, was implemented nearly entirely in BASIC (DEC's BASIC-PLUS in this case). - Lady Lysine Ikinsile 08:45, Jun 9, 2004 (UTC)

I have removed the "standard" comment, since it is ambiguous. As well as the 1981 comment, since this text is not overly clear on this. Please direct such specific comments to my talkpage, to be sure I noticed them. Lirath Q. Pynnor


'C++ (thus avoiding the issue of whether the successor to "B" and "C" should be "D" or "P".)' Why would it be "P"? -- Ortonmc 04:15, 26 Nov 2003 (UTC)

BCPL was the precursor to B and C... -- Merphant
Ah, I see. That would definitely limit the number of successors. :-) -- Ortonmc

Removed the image. To my understading of the ANSI C standard, void main(void); is not an acceptable prototype for main. Dysprosia 10:39, 27 Apr 2004 (UTC)


A C program that prints "Hello world!"
This is fixed now, but some question remains about whether an image is a useful thing to have. It's displayed on the right. Dysprosia suggests, quite reasonably, that a text box might be more appropriate (see User talk:aarchiba); I think the image is useful to have for possible use on the front page and for its syntax highlighting and smaller size (also the text box does not format correctly on my machine).


#include <stdio.h>

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

A C program that prints "Hello world!"

The text box is as shown here. --Andrew 11:15, Apr 27, 2004 (UTC)

One could probably use the new/nifty table image syntax to achieve a better result, but I'll muck around with trying that later. Dysprosia 12:33, 27 Apr 2004 (UTC)


I think the syntax highlighting is a mistake. While syntax highlighting is useful to programmers, anyone reading the article to learn about C won't know enough to benefit from the syntax highlighting, and worse, might mistakenly conclude that the syntax highlighting is essential to the code, or that one set of highlighting rules is standardized across the C Programming Language. (Furthermore, I think anyone learning the language should avoid highlighting until they fully understand the various entities being highlighted.) orthogonal 09:18, 15 May 2004 (UTC)

I agree, I think the syntax highlighting is distracting and possibly confusing for those unfamiliar with it. -- anonymous, 17:25, 15 May 2004 (BST)
Thirded. I am a huge fan of syntax highlighting, but I don't think it serves to help anyone understand C; one might potentially think "oh, so if I write a phrase in pink, it gets printed on the screen." -- Wapcaplet 16:24, 15 May 2004 (UTC)
I removed the syntax highlighting and the references to it in the explanatory text. I also removed the 'hello, world' image; seems silly to have a picture of something that can be found on the page in text. -- Merphant 22:02, 15 May 2004 (UTC)

Handwritten assembly is always better...

It should be noted, however, that for complex modern processors the machine code generated by C compilers is usually faster than hand-written assembly.

As Robert Dewar has pointed out, that is not a meaningful statement. The human can not approach the speed with which a computer compiles code. But if there is no time limit put on the human, eventually she will produce code that is optimal. In fact, a common first step in producing hand-tuned assembly is compiling the code with a good compiler, which makes it hard to be slower than the code generated by that compiler.

Any real comparision is going to involve a particular set of compilers, certain time limits and certain programmers (some of the published comparisons involve undergrads, which is certainly different from results using RMS and Mel), and is not going to be useful much beyond that. --Prosfilaes 01:40, 31 May 2004 (UTC)

Well, better is a tricky term. I think you have actually advanced an argument that a human, under the right circumstances, can make (a little bit of) a program run faster than the compiler can. This is surely true. One old joke was that you could always take out 10% (it just kept getting harder to do). On the other hand, there is a famous cautionary tale, I can not recall the source at the moment, about an "optimization" that did not get called for years. There is another school of thought which says that people can better spend their (valuable) time thinking about the choice of algorithm and overall design, and that the same effort spent in this way usually results in much larger payoffs in terms of speed.AJim 05:56, 31 May 2004 (UTC)

I think they meant faster as in faster speed in execution, not writing. Dysprosia 05:59, 31 May 2004 (UTC)

I also think they meant speed of execution. What I meant to say was that an hour invested in working on the overall design was likely to yield more of a speedup in execution than an hour invested in trying to write better machine code than the compiler. It is well known that given the same problem to solve, a group of programmers will sometimes produce programs with an enormous range of execution times. We are talking orders of magnitude here, not a few percent. Experienced programmers usually do much better than novices in terms of speed. What distinguishes the faster programs is not faster low-level optimizations, but faster algorithms. AJim 16:11, 1 Jun 2004 (UTC)

confusion about arrays -- should we describe a rarely-used "feature", or a common work-around ?

Think I disagree with this sentence:

The arrays can appear to have more than one dimension; dimensions are laid out in row-major order (stricly speaking, they are arrays of arrays).

I believe it could be

Two-dimensional arrays can be implemented, for example, as arrays of pointers to arrays of pointers, with higher-dimensioned arrays defined analogously: arrays of pointers to arrays of pointers to arrays of pointers, etc.

-- anonymous

This statement used to make sense. Some ill-informed contributor made it confusing. Static multidimensional arrays are in fact, single-dimensional in reality, and laid out in row-major order, but they can be implemented as arrays of arrays. I'll try to fix it. Derrick Coetzee 20:59, 10 Jul 2004 (UTC)

Summary

On one hand, I want to say your proposed replacement is incorrect -- the original correctly describes 2 dimensional static arrays (and higher dimensions), while your replacement does not.

On the other hand, static arrays of more than 1 dimension are very rarely used in real programs. Nearly everyone builds higher-dimensional arrays out of pointers and (1D) arrays, as dynamic arrays, in exactly the way your proposed replacement describes. (A few people use "sparse arrays", which are dynamically built out of pointers and 1D arrays in a completely different way).

Details

The data structure that C creates when the source code says

   double NewYork [10][10] = {0.0};

(a 2 dimensional static array) is simply a block of 100 double variables packed one right after the next; there is no array of pointers.

The handling of 2 dimensional static arrays in C is widely believed to be seriously flawed; see the C FAQ:

"Rather than worrying about all of this, one approach to using multidimensional arrays of various sizes is to make them all dynamic" -- http://www.eskimo.com/~scs/C-faq/q6.20.html

So when you want what mathematicians would call a 2D array of real numbers, most people end up using a (1D) array of pointers to (1D) arrays of double. We end up using an "array of pointers to arrays of pointers to arrays of double" for 3D arrays. That's all described in http://www.eskimo.com/~scs/C-faq/q6.16.html .

Because it's a bit of a hassle to do this for every array, many people wrap this all up in a matrix-manipulation library -- and unfortunately, there are tons of incompatible libraries for manipulating matrices in C.

--DavidCary 21:47, 10 Jul 2004 (UTC)

You make a strong case; however, the issue is not that I consider static arrays more important, but that this is the Types section being written in, and dynamic arrays are not a type supported at the language level. It certainly deserves mention, and perhaps reference from this section, but not in this section, which I consider to be reserved for language-supported type constructs. Deco 18:31, 11 Jul 2004 (UTC)

Wrp103

I'm the person who inserted the comment about multi-dimensional arrays being arrays of arrays. According to K&R section 5.7 Multi-Dimensional Arrays (I have the original here at work, so the updated version for ANSII C might have changed), when explaining the array day_tab, they state:

a two-dimensional array is really a one-dimensional array, each of whose elements is an array. Hence subscripts are written as

  day_tab[i][j] 

rather than

 day_tab[i, j]
I might be misunderstanding you, but here's my response. Many dynamic arrays literally store arrays of array pointers, and static arrays have this semantics, but static arrays are not stored this way, in a literal sense. They're laid out as one-dimensional arrays in row-major order, and pointers to subarrays are computed, rather than retrieved. That's why this code example produces "1234":
   #include <stdio.h>
   int main()
   {
       int a[2][2] = {{1,2},{3,4}};
       int* b = &a[0][0];
       int i;
       for(i=0; i<4; i++)
           printf("%d", b[i]);
       return 0;
   }
Deco 02:39, 13 Jul 2004 (UTC)
Actually, I wasn't talking about dynamically allocated multi-dimensional arrays. I was talking about the fact that in C, there aren't really multi-dimensional arrays, but rather arrays of arrays, which produce the same effect.
I do a lot of dynamic arrays (and in fact created an ADT that is available from my web site), but only single dimension arrays. Generally, I create a dynamic array of pointers to structures, so I haven't ever bothered to try multi-dimensional arrays. I have always felt that multi-dimensional arrays were mostly useful for matrix algebra and other math applications, not general data structures.
You are correct that C will allocate a single block of code, and map the coordinates to the proper memory location. That's why I didn't understand what was meant by metadata.
Wrp103 18:12, 13 Jul 2004 (UTC)

As for allocating multi-dimensional arrays, I had never heard of anyone doing it the way you describe, although it is no doubt possible. I'm not sure what you meant by metadata, however, since an array is simply a block of memory of the necessary size to store the data. In the case of a multi-dimensional array, each element would be a pointer to the second array.

BTW - I almost never use multi-dimensional arrays, and encourage my students to use arrays of structures instead. IMHO, they are a carry-over from when we didn't have data structures, and would use multi-dimensional arrays where the rows were for a given entity, and the columns were the fields of that entity.

It's true that multidimensional arrays were used for this purpose, but this was never the intended purpose nor even a good idea. Static multidimensional arrays were more useful in numerical computational of the sort normally done with Fortran. Derrick Coetzee 02:39, 13 Jul 2004 (UTC)

BTW2 - I admit I'm an old fogey, but I really object to adding the comment lines for the hello, world application. Actually, I don't care for the return 0 being added for that matter. The beauty of that programming example was in its simplicity. The fact that the program is well explained immediately below doesn't (IMHO) the addition.

return 0 is necessary. main returns an int, and leaving it off is sloppy. Dysprosia 23:43, 12 Jul 2004 (UTC)
Actually, in C99 this is not true anymore. To quote the standard:
5.1.2.2.3 Program termination
1 If the return type of the main function is a type compatible with int, a return from the
initial call to the main function is equivalent to calling the exit function with the value
returned by the main function as its argument;10) reaching the } that terminates the
main function returns a value of 0. If the return type is not compatible with int, the
termination status returned to the host environment is unspecified.
Forward references: definition of terms (7.1.1), the exit function (7.20.4.3).
Lady Lysiŋe Ikiŋsile | Talk 07:54, 2004 Jul 13 (UTC)
My comment on the return 0 was because it wasn't included in the original "hello, world" program.
Wrp103 18:12, 13 Jul 2004 (UTC)
I agree that the comments are misplaced; explanation belongs in the text. The return 0 is important, not so much because of what's allowed, but because it helps demonstrate the procedural paradigm in a very simple setting. Deco 02:39, 13 Jul 2004 (UTC)


I think part of the confusion that arose from my original edits were because I made two unrelated changes that were perceived as related.

I commented on the nature of multi-dimensional arrays in C, and I added an example of how to create single-dimension dynamic arrays in C. I think somebody assumed I thought they were related, which is what started this whole thread. I almost never use multi-dimensional arrays, and never even considered trying dynamic multi-dimensional arrays. However, I have noticed that many of the C text books I have used for various classes claim that you can't have dynamic arrays in C, which of course is wrong. So, when I'm teaching C, I make it a point to mention those two things.

Sorry for any confusion. I'm still getting used to this sytem. Wrp103 18:27, 13 Jul 2004 (UTC)


Wrp103 16:17, 5 Aug 2004 (UTC)

I noticed that somebody deleted my example of a constructor, and replaced calls to malloc followed by clearing memory to calloc. The purpose of the constructor is to initialize the data structure to default/valid values; my example of zeroing the structure was for simplicity. BTW - NULL isn't always all zeros (I recall working on a mainframe where this messed up a lot of programs that used things like "if (ptr)", assuming NULL would return False.)

That was me. Yes, I'm aware that (although NULL is always 0), the value of a NULL value might not be 0. However, I think I only removed the code which called memset() with '\0' as the argument, which is not compatible with systems where null isn't 0 anyway, and is basically identical to what calloc does. The code which explicitly assigns ... = NULL was left.
In any case I like the new (useful) example of a C ctor. Kate | Talk 16:23, 2004 Aug 5 (UTC)

(Christopher Thompson added the next bit) The following makes no sense: In C, sizeof('a') == sizeof(int); In C, sizeof('a') == sizeof(char) (For the record this has been corrected now. Cyoung 04:12, 23 Sep 2004 (UTC))

Wrp103, the statement you make above: BTW - NULL isn't always all zeros (I recall working on a mainframe where this messed up a lot of programs that used things like "if (ptr)", assuming NULL would return False.)
While it is true that NULL isn't always all zeros, "if (ptr)" will ALWAYS return false if ptr is NULL, since the compiler will recognise that ptr is a pointer, and compare it against whatever bit pattern is used to represent NULL on the architecture. This means that if (!p) and if (p == NULL) and if (p == 0) are all equivalent and correct, regardless of what bit pattern is used to represent NULL. See Steve Summit's comp.lang.c FAQ Q5.3 Cyoung 04:12, 23 Sep 2004 (UTC)

bottom line on "confusion about arrays"

Formally, C has only statically-allocated, fixed-size, one-dimensional arrays. (I ignore for the moment the variable-length arrays of C99.) But you can build, using "off the shelf parts", any of:

  • a statically-allocated, fixed-sized, multidimensional array (using arrays of arrays)
  • a dynamically-allocated, variable-sized, one-dimensional array (using malloc and pointers)
  • a dynamically-allocated, variable-sized, multidimensional array (using malloc and pointers to pointers)

There are also variations on the last theme; for example dynamically-allocated simulated multidimensional arrays can be "ragged" (each row can have a different number of allocated columns).

All of these techniques are reasonably straightforward and very widely used.

Steve Summit (talk) 02:49, 11 December 2005 (UTC)

This is a nicely-worded summary and is in line with the comp.lang.c FAQ and standard. Well put. Deco 03:48, 11 December 2005 (UTC)
Not surprising. Did you notice who wrote it? -- 66.96.28.244 05:15, 12 December 2005 (UTC)

Array of structs?

In all honesty I find the example of converting a multidimensional array to an array of structs not only unnecessarily long and drawn out, but also misled. Multidimensional arrays should never be used as arrays of records; this may or may not be obvious, but in any case, multidimensional arrays have perfectly legitimate uses, such as representing matrices, that have nothing to do with structs, and it was for these purposes that they were designed.

Much of the material in the example perhaps should be in the article somewhere, or some other article, but its current placement isn't useful. In particular, constructors and destructors as used in C are a practice, and are not relevent to the C language per se. The same is true of the previous content I added related to creating dynamic arrays, which I admit now is misplaced. There are dozens of mentionable practices in C, including placing constants on the left side of equality comparisons, Duff's device, arrays that extend off the end of records, setting pointers to deallocated objects to NULL, parenthesizing macro arguments, and so on.

In short, if we are really to tabulate C practices, this should either be done in a separate section, or eventually, a separate article. Let storage talk about the basics of storage as built in to the language itself. Keep common practice cleanly separated from core language features. I am prepared to make this change. Is there any opposition? Deco 04:08, 31 Oct 2004 (UTC)

I went ahead and created the section, entitled Common Practices, and I moved my bit on allocating dynamic arrays there. If there is no objection, I will remove the code on converting multidimensional arrays to arrays of structures, and insert a section on constructors and destructors into the new section. Deco 05:04, 31 Oct 2004 (UTC)
With no objections thus far, this change has been performed. If objections later arise, feel free to edit or revert and discuss. Deco 01:37, 1 Nov 2004 (UTC)
The contents of Common Practices section are fine, but I think that perhaps they better belong in someplace like wikibook's Programming:C tutorial, rather than in the main encyclopedia article. The section is no longer describing the language, but various people's preferred use of the language... - Key45 19:15, 17 Nov 2004 (UTC)
I agree. I introduced this section mainly as a compromise (in order to get that content extracted from other sections without upsetting anyone), and don't really think it belongs in an article about the language. Deco 19:19, 17 Nov 2004 (UTC)
This section now lives in its new home at Wikibooks' C common practices. I invite those interested in this section to contribute to it at its new location. Deco 10:14, 16 Jan 2005 (UTC)

C90

C90 redirects here. But "C90" isn't anywhere on this article. Brianjd 09:28, 2004 Nov 29 (UTC)

C90 is an informal term for the C language as described by the 1990 international ISO standard. C89, similarly, describes the ANSI 1989 standard, which is almost identical. They're not mentioned here mainly because they are informal and not totally universal, although the standards are described. Deco 22:11, 15 Dec 2004 (UTC)

large programs

This is no accident; C was created with one important goal in mind: to make it easier to write large programs with ...

If we mean it's original creation circa 1972, this is certainly not the case. C was at that stage for a machine with max program size 12K, there was no way you could write a large program in C. Rather, C was created so they would have something slightly nicer and more portable than assembler to use on a machine that was too small to fit almost any other language then in existence. Some support for large programs emerged later, but really large programs have never been C's strength. It shines for small to medium sized, fast, close to the metal stuff.

It's a matter of what you mean by "large." C was an important early structural programming language, and structural programming enables the creation of more modular and organized and so larger programs. Its type system also facilitates maintenance of larger programs that are difficult in assembly. Even a program of a few K in code bytes is quite an undertaking in assembly, while something that could be written as a small assembly program really doesn't require a higher-level language like C (assuming porting it isn't a big effort.) Maybe this isn't reflected in the text, but I'm not sure how to word it. Deco 22:06, 15 Dec 2004 (UTC)

At any rate, I wouldn't say that C was created with "one" goal in mind, and certainly not that "writing large programs" was the primary goal. C, like Unix, was created by programmers for programmers, as a pleasant tool/environment for writing the kinds of programs they wanted to write, using the philosophy they were then exploring and inventing. The most important aspects of that philosophy (again applying to both C and Unix) were probably: minimalism, and freedom from arbitrary limitations or other strictures. Steve Summit 05:24, 13 July 2005 (UTC)

There are far too many external links, some are for rather unimportant programs, almost seems like a bit of advertising or fanfare has slipped in here. I'm taking a bush-whacker to it, but feel free to add anything back that seems critical. Daniel Quinlan 08:39, Feb 10, 2005 (UTC)

I agree, but think some of these should later reappear in articles about "C tools" and so on. Deco 19:28, 10 Feb 2005 (UTC)

Array changes

For what it's worth, dynamic arrays are not really supported by the C language proper. Arrays whose size are known at runtime can be allocated using the standard library, but these do not have array types — they are assigned to pointer variables and merely treated syntactically in a manner similar to arrays. This is the original reason I originally omitted them. I realise it seems counterintuitive to say C doesn't have dynamic arrays, but really it's something you implement in C, not something C gives you. This is why I've softened the language of this part.

Oh, and multidimensional arrays are not arrays of arrays. This is just syntax — they're laid out in row-major order, and the index is computed using multiplication, not obtained by successive pointer fetching as in a true array of arrays. I've attempted to clarify this. Deco 04:45, 6 Apr 2005 (UTC)

We've discussed this before, and again I refer you to K&R, which specifically states that C doesn't have multidimensional arrays, but rather arrays of arrays. As for dynamic arrays in C, the reason I keep bringing it up is because my students keep refering to books & articles claim you can't have dynamic arrays in C. If you want to call it something else, that's fine, but I think it is important that people are aware that they can implement dynamic arrays in C. wrp103 (Bill Pringle) - Talk 03:49, 7 Apr 2005 (UTC)
I guess you're right. Semantically, they are arrays of arrays, even if they're not necessarily stored in that manner. I agree with respect to dynamic arrays (let's mention it like we are now but just not use that term). Deco 01:22, 9 Apr 2005 (UTC)

I think there are a lot of areas of overlap between the C language, C syntax, and C standard library articles. I do think that this needs to be resolved.—Kbolino 04:20, Apr 7, 2005 (UTC)

Text added to C syntax section

Before anyone complains (or reverts me), I wanted to explain the text I added to the C syntax section — the idea is to provide a supercondensed summary for those who don't want to dive into all the details of the full article. This is a very common thing to do wherever you see a main article note, and I think it's appropriate here, although the summary is necessary technical and missing lots of information. If they need to know more they can visit the main article. Deco 04:11, 18 Apr 2005 (UTC)

Oy! You removed the comment bit that I'd just rewritten. I agree with your changes b.t.w. -- it should be in syntax, but you coulda checked if it was there first! Never mind, I moved it myself. I still think the main page is far too heavyweight and detailed. Most of the information should be moved to separate articles, with just very brief coverage on the main page. Akihabara 08:22, 18 Apr 2005 (UTC)