C examples...?

J

jwl

Hi everyone

I learn best by example so I'd like to find C source code to study,
especially stuff showing how to use the standard library. The trouble is,
although I can find tons of non-standard code, I can't find any strictly
ISO C code. Does anyone know a link to some interesting programs in
standard C?

Thanks for your time
jwl
 
G

Giovanni

jwl said:
Hi everyone

I learn best by example so I'd like to find C source code to study,
especially stuff showing how to use the standard library. The trouble is,
although I can find tons of non-standard code, I can't find any strictly
ISO C code. Does anyone know a link to some interesting programs in
standard C?

Thanks for your time
jwl

Thou all the gurus in this NG will probably contradict me the majority
of the code you say non-standard are standard as long as the compile
witout errors. If you have doubts compile them wit the ansi option
(e.g. "gcc -ansi ....") and you'll find where they fail.

Ciao
Giovanni
 
R

Rich Gibbs

jwl said the following, on 12/25/04 10:31:
Hi everyone

I learn best by example so I'd like to find C source code to study,
especially stuff showing how to use the standard library. The trouble is,
although I can find tons of non-standard code, I can't find any strictly
ISO C code. Does anyone know a link to some interesting programs in
standard C?

One source you might want to have a look at is P.J. Plauger's excellent
book, _The Standard C Library_. It discusses what the standard requires
of the library, and contains an implementation of the library, which is
in portable C to the extent possible. Mosy important for learning, it
contains a discussion of *why* things are done.
 
M

Malcolm

jwl said:
I learn best by example so I'd like to find C source code to study,
especially stuff showing how to use the standard library. The trouble is,
although I can find tons of non-standard code, I can't find any strictly
ISO C code. Does anyone know a link to some interesting programs in
standard C?
Look up GNU.

I don't think your strategy is going to be very productive, however. A lot
of real code is difficult to read, unless it is deliberately written for the
purpose of instructing beginners.

Also,a large number of real world programs use some non-standard library
calls. The facitilites provided by stdlib are very limited, for instance it
is not possible to list files in a directory, query the state of the
keyboard, or use any type of graphics including writing characters to set
locations on the screen.
 
A

Al Bowers

jwl said:
Hi everyone

I learn best by example so I'd like to find C source code to study,
especially stuff showing how to use the standard library. The trouble is,
although I can find tons of non-standard code, I can't find any strictly
ISO C code. Does anyone know a link to some interesting programs in
standard C?

Take a look at the snippets collection which is online. Most of
the code is Standard C and compatible with any os. It
identifies the code that is os specific.

The link:
http://c.snippets.org/browser.php
 
A

Arthur J. O'Dwyer

I learn best by example so I'd like to find C source code to study,
especially stuff showing how to use the standard library. The trouble is,
although I can find tons of non-standard code, I can't find any strictly
ISO C code. Does anyone know a link to some interesting programs in
standard C?

http://www.contrib.andrew.cmu.edu/~ajo/
http://www.contrib.andrew.cmu.edu/~ajo/free-software/

If you find anything non-standard there, that isn't marked as such, with
the exception that I use 'is*(k)' instead of 'is*((unsigned char)k)' in
some programs, let me know at once! ;)

Richard Heathfield keeps a collection of C snippets, too, which
certainly ought to be portable (right?), but I won't vouch for that.
And I know that a lot of other clc regulars past and present have at
least one portable C library that they like to plug off and on. So
those might be decent starting places.

HTH,
-Arthur
 
I

infobahn

Giovanni said:
Thou all the gurus in this NG will probably contradict me the majority
of the code you say non-standard are standard as long as the compile
witout errors.

Not so. Firstly, the Standard doesn't define compiler "errors" as such.
Instead, it talks about diagnostics. Secondly and more importantly,
code that compiles doesn't necessarily mean code that works. Consider
the following code:

#include <stdio.h>

void foo(int *m, int *n)
{
*m = *n++; /* Danger! */
}

int main(void)
{
int i = 42;
foo(&i, &i);
printf("%d\n", i); /* what does this print? */
return 0;
}

is non-standard in the sense that the Standard does not define
its behaviour, but it will typically not generate an error.

> If you have doubts compile them wit the ansi option
(e.g. "gcc -ansi ....") and you'll find where they fail.

Insufficient.
 
I

infobahn

jwl said:
Hi everyone

I learn best by example so I'd like to find C source code to study,
especially stuff showing how to use the standard library. The trouble is,
although I can find tons of non-standard code, I can't find any strictly
ISO C code. Does anyone know a link to some interesting programs in
standard C?

Search the archives for this newsgroup (I think Google is perhaps still
just about usable); you will find plenty of sample source code not only
presented, but also dissected and improved.
 
C

Chris Torek

... the Standard doesn't define compiler "errors" as such.
Instead, it talks about diagnostics. Secondly and more importantly,
code that compiles doesn't necessarily mean code that works.
Right.

Consider the following code:

#include <stdio.h>

void foo(int *m, int *n)
{
*m = *n++; /* Danger! */

I think you meant to write:

*m = (*n)++;

As it is, since the postfix "++" binds more tightly than the prefix
"*", the expression means "increment n, and use the value it had
before said incrementation as the operand of unary-star."
}

int main(void)
{
int i = 42;
foo(&i, &i);
printf("%d\n", i); /* what does this print? */
return 0;
}

is non-standard in the sense that the Standard does not define
its behaviour, but it will typically not generate an error.

Given the corrected (or "adjusted to be in-correct"? :) ) version
of function foo(), indeed. No diagnostic is required, and in order
to produce one, a compiler would have to deduce that *m and *n
both refer to the same underlying object (the "i" in main), thus
notice that the line in foo therefore has the undefined effect of
"i = i++", *and* be coded to complain about that. Even the latter
is all too rare, and the former requires extensive alias analysis,
which -- while easy enough in computing theory -- is rare for
practical reasons (it tends to take a lot of compile-time CPU
power for little code-improvement).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top