Re: Comments requested: brief summary of C

From: j (jake30NOSPAM_at_bellsouth.net)
Date: 02/24/04


Date: Tue, 24 Feb 2004 02:44:19 -0800


"Adam Barr" <adamba@gte.net> wrote in message
news:ce782195.0402232215.1bb20bfb@posting.google.com...
> For a book I am working on, I have written a brief (7 page) summary
> of C. The intent of this is that an experienced programmer who did
> not know C would be able to get enough information to read and
> understand C programs. It is not meant to be a 100% complete
> summary of the language, but everything in there should be correct.
>
> If anyone is interested, please feel free to read it and send me
> comments. Note that the material is copyrighted, this document is
> provided for review only, and the ideas contained in any comments may
> be used in the book, in whole or in part, with no rights granted to
> the person who provided the comments. I will however acknowledge by
> name (if desired) anyone who sends comments.
>
> The information is in PDF form per my publisher's request, but if this
> is a problem for anyone, email me and I can send it to you in plain
> text.
>
> The file is at
>
> http://www.proudlyserving.com/language/c.pdf
>

``Statements in C end with a semi-colon ( ; ).''

No, not true. By that definition the following
qualifies as a statement:

int foo;

A declaration is _not_ a statement. Read 6.8 of c99.

``C treats all blanks space as equivalent, so line breaks
and indents are for readability only.''

So #define FOO 10
is the same as #defineFOO10 ?

``A single statement without the braces also counts as a
block of code.''

No, it counts as a single statement.

``A char holds a single byte. Single characters are
surrounded by single quotes, such as 'a' and 'x' .''

I think you should also mention that char is an integer type
and the character literals are of type int. Otherwise
your statement can be misleading.

``Strings are simply arrays of type char. By convention,
a string is terminated with a 0 value, written as a single
character '\0' .''

Not adequate enough. As the standard says:
``A string is a contiguous sequence of characters terminated
by and including the first null character.''

``(you could put a different character in the tenth byte,
but it would then not be a properly-terminated string
according to C conventions).''

It would not be a string whatsoever if there is no
null character.

``char pointers are often assigned to constant strings, for example
city = "Boston";

It is not assigned to ``constant'' strings(you should call it a string
literal). It is assigned the address that the string literal decays to.

``Pointers are also dereferenced with * , so *city is the first byte
pointed to by city . In fact, pointers and arrays are often used
interchangeably, and the first char in the city array could be
referenced as city[0] or *city .''

This might mistakingly lead someone to believe that
pointers and arrays are one in the same if you do not
expound further on their differences.

``Note that C does no checks for validity of pointers, so *city
will likely cause a crash if city is uninitialized, and name[20]
gives an undefined result if name is allocated as above with room
for only 10 chars .''

C? You mean C implementations?

``Pointer arithmetic is allowed and automatically compensates
for the size of the element pointed to.''

I would use ``object'' not ``element''

``More generally, array[n] is equivalent to *(array + n) and in
fact is defined as such.''

No it isn't. It is equivalent to *((array) + (n))
The added parens are important. If your method
was actually used then the following would
fail:

a[i, j];

Which would be equivalent to *(a + i, j)

Which would end up dereferencing the value of ``j''
as opposed to *((a) + (i, j))

``typedef struct _record {
int element1; char element2;
struct _record * next;
} record, * record_ptr;''

The standard says that identifiers that begin with an
underscore are always reserved for use as identifiers
with file scope.

``int array[20];
for (i = 0; i < 20; i++)
{ { code involving array[i]; } }''

``i < sizeof array / sizeof array[0]''
is the more common convention

``Comments are denoted by // ;''

Only under c99 they are.
It doesn't matter if your c89/90 implementation
_happens_ to support them. That is, of course, if
portability is a concern.

> I have written similar summaries for other languages and have or will
> post them in the appropriate newsgroups.
>
> Thank you.
>
> - Adam Barr
> adamba@gte.net



Relevant Pages

  • Re: Can C do it ?
    ... C does not have a native string type. ... has as it's arguments two char pointers. ... Using a struct would be very limiting: ...
    (comp.lang.c)
  • Re: Newbie seeking VB to ANSI C Conversion assistance
    ... >> string manipulation and pointer usage in C. ... >> different seeds and decoding strings. ... > int main(int argc, char *argv) ... > are pointers and since there is no space allocated to store ...
    (comp.lang.c)
  • Re: Can C do it ?
    ... C does not have a native string type. ... has as it's arguments two char pointers. ... But remember, if you misuse it terribly enough, demons /will/ fly out ...
    (comp.lang.c)
  • Re: Function Problem
    ... but fnReplace takes pointers to strings of length 16. ... char *s = malloc); ... > your function fnReplace returns the address of the auto variable "string". ... > Regards, Helge ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Reading Zip file contents without DLLs... How?
    ... It will point into some string ... > surrounding code used Delphi long string variables in such a way as ... I'd think it's a sign of delusion about the nature of pointers. ... comparing a Char and a String. ...
    (alt.comp.lang.borland-delphi)