C examples and codes

R

Richard Heathfield

Philip Potter said:

This is after a quick skim, I haven't read most of it.

That sounds familiar. (We've been here before. And no doubt we'll come here
again.)

If we perform the "Find Bugs In Navia Tutorial" ritual another few dozen
times at the same frequency as currently, and *if* he takes notice - which
isn't a given - then he should have a tutorial of merchantable quality by
about 2185 or so. Until then, the value of the print version is rather
less than the value of the equivalent amount of blank paper. (Blank paper
is useful, you see.)
 
T

Tor Rustad

Tsb said:
I have read about learning C programming language. Lots of them said
the best way to learn C is reading codes.

So where can I find codes and some examples?

Some C books comes with source code, e.g.

"The Standard C Library", by P.J. Plauger

or peek at sourceforge for an open-source project of your interest.
 
B

Ben Bacarisse

jacob navia said:
Philip Potter wrote:
However I can already see

Look, it is a tutorial for C. Since "Standard C" doesn't RUN anywhere
(you need some specific compiler, some specific operating system,
some way of doing windowed output etc) it is a tutorial of
C in a certain context. The first part is about standard C,

That is not really true. The text starts on page 14, and the first
Windows specific parts are on page 17. On page 19 we get "Console
mode programs and windows programs" followed by lost of systems (even
compiler) specific things. I would prefer a C tutorial to have at
most a few references to compiling and running programs or, better, to
have these in a separate part. This is good of you are writing a "C
on Windows" or "lcc-win32" tutorial, but it makes it read rather
muddled for someone using another system and compiler.

You have statements like "transforming one type of pointer into
another needs no code at all at run-time" which is definitely
system-specific, and you assert that "long" and "int" are compatible
types which is, at best an lcc-win32 extension. (Oddly, this is
immediately followed by the correct rules for compatibility which
contradicts the earlier statement.)

I know how hard it is to write a good tutorial, but have quite a few
factual errors. These would be worth correcting, surely?
 
J

jacob navia

Philip said:
This is sheer nonsense. Standard C runs anywhere there is a standard C
implementation. (And you shouldn't write programs which require
"windowed output").

Why shouldn't I? I mean using windowed output is quite normal this
days.
Here are a few bits I've picked out:

p2: "A program in C is written in one or several text files called
source modules. Each of those modules is composed of functions, i.e.
smaller pieces of code that accomplish some task, and data, i.e.
variables or tables that are initialized before the program starts."

That defines static data, but what about automatic variables and
dynamically allocated data?

You are at page 2. Do not be surprised if I haven't explained
EVERYTHING :) Read ON!
p2: "Lisp and scheme, two list oriented languages featured automatic
garbage collection since several decades."

This should be "Lisp and scheme, two list oriented languages, have
featured automatic garbage collection for several decades." (For a
non-native speaker, your english is very good, but your usage of "since"
here is wrong, and you frequently use this incorrect idiom. This will be
the last spelling/grammar criticism from me, since it is offtopic.)

OK.
On p18, your example of a typecast is in a position where the cast is
unnecessary:
float f = 67.8f;
double d = (double)f;

Of course it is unnecessary. I just wanted to show an example of a cast,
and what it does. There is an explicit reference to a more detailed
explanation later.
On p19 your description of the size of arithmetic types is mostly wrong.
It is true that char is 1 byte (by definition) but it is not true that
short must be 2 bytes, int must be 4, long must be 4 or long long must
be 8.

If you read two sentence before the table you will see:
"The implementation of the C language by the lcc-win compiler..."
p21: "Machine addresses are just integers, of course. For instance, if
you have a machine with 128MB of memory, you have 134 217 728 memory
locations."

I don't know what you're talking about, but it's not standard C. Machine
addresses are not necessarily "just integers".

In this implementation (and many others) yes.
p54:
"1.13.10 union.
You can store several values in a single memory location or a group of
memory locations with the proviso that they can’t be accessed at the
same time of course. This allows you to reduce the memory requirements
of a structure, or to interpret a sequence of bits in a different fashion.
For a detailed discussion see “Unions” on page 107."

This isn't very clear. It sounds like I can store multiple variables in
a union simultaneously, provided I don't access them simultaneously.

Well... that's a union dear.
p57:
"1.13.19 unsigned.
Integer types (long long, long, int, short and char) have the most
significant bit reserved for the sign bit. This declaration tells the
compiler to ignore the sign bit and use the values from zero the 2^n for
the values of that type. For instance, a signed short goes from –32767
to 32767, an unsigned short goes from zero to 65535 (2^16). See the
standard include file <stdint.h> for the ranges of signed and unsigned
integer types."

Why even mention a sign bit? Also while that is the minimum range for a
signed short, it may be larger (and it wouldn't surprise me if it is
larger on lcc-win32!)
The mentioning of the sign bit makes the whole thing easier to
understand
p123: C does not require ASCII. You fail to communicate this and
actively mislead your readers by saying the ordering used by strcmp is
based on the ASCII character set.

True. Will change that.
p134: alloca() is not standard C, but you do not state this. AFAIK
_msize() and _expand are not either.
True. Eliminated msize and expand. And marked alloca and GC_malloc by
saying that they may be absent in other systems.
p138: Are you really advocating writing programs which do not free() all
that they malloc()?
Yes.
It is very convenient. The lcc compiler does that.
p146: This is a poor hash function, and you do not explain sufficiently
well the importance of a good hash function.

Yes. It is a hash function that works, but it is surely not the best and
it is not advertised as the best.
p182: IEEE 754 floating point is not required by the standard.

????

p190: QFLT_EPSILON is not part of the standard, though it looks like you
state that it is.

Overall, the typesetting is dreadful: on p3, "argument-list" should not
wrap a line. When you introduce main, you italicise it, but when you
introduce printf, you put it in double quotes. On p13, the line numbers
in the second example look like they're part of the code (and cause
syntax errors!) On p18 you have a code example in a proportional font.
On top of all this, there is no Chapter 2!

Yes, it is the windows programming part!
This is after a quick skim, I haven't read most of it. But what I have
read already means I wouldn't recommend this guide to anyone as an
introduction to standard C. In some places, you take care to inform the
reader what is standard and what is lcc-win32 specific, but in other
places you do not. And your explanations and definitions are sloppy, but
an introduction to C must be strict and careful with its use of words.

I think that you have obviously a bias against me. But your comments
are welcome. I will go on working in this.
 
J

jacob navia

Ben said:
That is not really true. The text starts on page 14, and the first
Windows specific parts are on page 17. On page 19 we get "Console
mode programs and windows programs" followed by lost of systems (even
compiler) specific things.

If I do not explain those immediately, they will NOT be able to compile!
Maybe you are new to windows, but windows user do not know a lot about
the command line and do not understand immediately that they should
open a "dos" window.
I would prefer a C tutorial to have at
most a few references to compiling and running programs or, better, to
have these in a separate part. This is good of you are writing a "C
on Windows" or "lcc-win32" tutorial, but it makes it read rather
muddled for someone using another system and compiler.
You have statements like "transforming one type of pointer into
another needs no code at all at run-time" which is definitely
system-specific,

and the sentence CONTINUES "in most implementations!"
and you assert that "long" and "int" are compatible
types which is, at best an lcc-win32 extension.

And MSVC extension and Watcom extension and gcc extension for the
32 bit version of those at least.

(Oddly, this is
immediately followed by the correct rules for compatibility which
contradicts the earlier statement.)
Not "oddly"
I know how hard it is to write a good tutorial, but have quite a few
factual errors. These would be worth correcting, surely?

Well, I do not see any errors. Yes, if you are running linux, my
tutorial would be difficult to follow, but in a few months I will have
the IDE running and lcc-win will be similar to the windows
version.
 
U

user923005

Are you saying I'm wrong? I've yet to see a C tutorial on the Web
that didn't have fundamental mistakes.

I don't think you are wrong (at *least* 99% _are_ crap), but there are
good C tutorials.
The tutorials of Tom Torf and Steve Summit spring to mind.

Whatever happened to the one grumpy code monkey?
;-)
 
U

user923005

I have read about learning C programming language. Lots of them said
the best way to learn C is reading codes.

So where can I find codes and some examples?

You are quite likely to pick up bad habits by looking at code.
For instance, nearly all of the samples in the Microsoft C online help
files contain mistakes.

Get a good C book like K&R2:
http://cm.bell-labs.com/cm/cs/cbook/

A good C class at a local college is also a very good idea, if the
instructor is proficient.
 
R

rosewater

jacob said:
Well, I do not see any errors.

It is this sort of stupidity and arrogance that makes it impossible
for people to take you seriously. Phil Potter just spewed out a list
of elementary errors a mile long, but you refuse to admit your
mistakes. That's the mark of an idiot.
 
I

Ian Collins

jacob said:
If I do not explain those immediately, they will NOT be able to compile!
Maybe you are new to windows, but windows user do not know a lot about
the command line and do not understand immediately that they should
open a "dos" window.
The best introductions and tutorials for any languages I have read all
manage to remain platform and compiler neutral, with all system/compiler
specifics left to an appendix.
 
K

Keith Thompson

Philip Potter said:
p57:
"1.13.19 unsigned.
Integer types (long long, long, int, short and char) have the most
significant bit reserved for the sign bit. This declaration tells the
compiler to ignore the sign bit and use the values from zero the 2^n
for the values of that type. For instance, a signed short goes from
–32767 to 32767, an unsigned short goes from zero to 65535
(2^16). See the standard include file <stdint.h> for the ranges of
signed and unsigned integer types."
[...]

At the beginning of the paragraph "Integer types" should be "Signed
integer types".

The ranges of the predefined integer types are given in <limits.h>,
not <stdint.h>. The latter may not exist in pre-C99 implementations.
(Does the tutorial mention that C99 is not widely implemented?)
 
P

Philip Potter

jacob said:
Why shouldn't I? I mean using windowed output is quite normal this
days.

If you write to "stdout", the output will be whatever is "normal" for
the implementation.
You are at page 2. Do not be surprised if I haven't explained
EVERYTHING :) Read ON!

This isn't merely an incomplete explanation, it's a wrong explanation.
You should focus on what data is, not examples of how data can be created.
Of course it is unnecessary. I just wanted to show an example of a cast,
and what it does. There is an explicit reference to a more detailed
explanation later.

But would it not be more education to use a cast which *is* necessary?
Otherwise you have users saying "what's the point?" -- or worse, users
who don't realise that it is unnecessary!
If you read two sentence before the table you will see:
"The implementation of the C language by the lcc-win compiler..."

You should still explain what standard C requires if you want to claim
this is an introduction to standard C. At the very least you should make
it clear that these sizes may vary on different implementations.
In this implementation (and many others) yes.

The point is that it is more harmful that helpful to think of pointers
as integers. (And that machine addresses are not necessarily just
integers, regardless of how many implementations you find where they are.)
Well... that's a union dear.

No, it isn't. That suggests that this code:

union {
int i;
long l;
double d;
} onion;
int main(void) {
onion.i = 1;
onion.l = 2;
onion.d = 3.0;
printf("onion.i is %d\n",onion.i);
printf("onion.i is %ld\n",onion.l);
printf("onion.i is %f\n",onion.d);
return 0;
}

will output
onion.i is 1
onion.l is 2
onion.d is 3.0000000

The problem is that I'm not accessing the variables simultaneously, but
I am *using* them simultaneously. There's an important difference.

You *cannot* store multiple values in a union simultaneously. I'm sure
you know this, but your language is sloppy and confusing.
The mentioning of the sign bit makes the whole thing easier to
understand

I disagree. A signed value can be negative. An unsigned value cannot.
What is hard to understand about that?
Yes.
It is very convenient. The lcc compiler does that.

It's terrible practice.
Yes. It is a hash function that works, but it is surely not the best and
it is not advertised as the best.

No, but it is advertised as a hash function which will do. If it will do
for an example, a user will expect it to do for his code. When he thinks
about hash functions, he will remember the one you provided and
copy/paste it in. (And why shouldn't he? After all, reinventing the
wheel is bad.)

n1256, 6.10.8p2:
The following macro names are conditionally defined by the implementation:
__STDC_IEC_559__ The integer constant 1, intended to indicate
conformance to the specifications in annex F (IEC 60559 floating-point
arithmetic).

If the implementation does not define __STDC_IEC_559__, it is only
constrained by the lower limits specified in 5.2.4.2.2.
Yes, it is the windows programming part!

Look again. Windows programming is in Chapter 3. There is no chapter 2.
(Though the contents do reference it.)
I think that you have obviously a bias against me. But your comments
are welcome. I will go on working in this.

Show me an example of bias and I will accept it. I think my comments
have been fair.
 
J

jjds101

I have read about learning C programming language. Lots of them said
the best way to learn C is reading codes.

So where can I find codes and some examples?

I would recommend *not* asking in comp.lang.c. If you read the
responses throughout your thread you can see that the regular
contributors are more interested in slinging mud and arguing with each
other than participating in useful discussion.

Usenet is full of kooks and crazies, really. The regulars here all
look like these guys:
http://images.amazon.com/images/P/1861000111.01.LZZZZZZZ.gif

Stick with books, and post your questions to a web forum somewhere.
The information you'll get will be more useful in real-world
programming, and you might even be able to find a good forum where you
can post simple questions. Questions that don't turn into giant flame
wars among Usenet loons who want to prove they have bigger beards than
the next guy.

Read some of the other threads, you'll see what I'm talking about.
There's nothing of value here. Move along before you turn into one.
 
J

John Bode

I don't think you are wrong (at *least* 99% _are_ crap), but there are
good C tutorials.
The tutorials of Tom Torf and Steve Summit spring to mind.

Whatever happened to the one grumpy code monkey?
;-)

He's grumpy as ever. He even has a blog
(grumpycodemonkey.blogspot.com) that he hasn't updated in forever.

I just wanted to impress upon the OP that there's a lot of bad
information out there about the C programming language, and that he's
better off sticking with authoritative sources in the beginning.
 
K

Keith Thompson

jacob navia said:
Philip Potter wrote: [...]
On p18, your example of a typecast is in a position where the cast
is unnecessary:
float f = 67.8f;
double d = (double)f;

Of course it is unnecessary. I just wanted to show an example of a cast,
and what it does. There is an explicit reference to a more detailed
explanation later.

Surely it would be better to introduce casts with an example where a
cast is actually necesssary.
If you read two sentence before the table you will see:
"The implementation of the C language by the lcc-win compiler..."

Do you discuss the rules imposed by the standard, for example, that
int is at least 16 bits?
In this implementation (and many others) yes.

Are you trying to teach C, or just a single implementation? In
standard C, pointers are not integers; they're *very* different things
with an entirely different purpose. Yes, they happen to be
implemented as integer-like chunks of data in many implementations,
but by stating this at the beginning, you encourage a dangerous
mindset.

Pointers are pointers. They point to things. Teach that.
Well... that's a union dear.

Storing a value in one member of a union, result of accessing a
different member is unspecified except in some very narrow
circumstances. (Consider that when you're wrong about something, and
somebody points it out, that's not a good time to be condescending,
"dear".)

[...]

What is your question? He's right, the C standard doesn't require
IEEE 754 floating point. Do you dispute that, or does your tutorial
not actually make that assumption?
 
K

Keith Thompson

jacob navia said:
Ben Bacarisse wrote: [...]
and you assert that "long" and "int" are compatible
types which is, at best an lcc-win32 extension.

And MSVC extension and Watcom extension and gcc extension for the
32 bit version of those at least.

(Oddly, this is
immediately followed by the correct rules for compatibility which
contradicts the earlier statement.)
Not "oddly"
[...]

No, int and long are not compatible types. They are distinct types
that may happen have the same representation in some implementations.

If int and long were compatible types then this:

int *pi = NULL;
long *pl = pi;

would be legal; in fact, it's a constraint violation.

I suggest you look up the word "compatible" in the standard.
 
J

jacob navia

Philip said:
If you write to "stdout", the output will be whatever is "normal" for
the implementation.

Yes. Under windows it means that your output is discarded.
Under linux it means the same: your output is not visible.

OF COURSE YOU KNOW THAT. But beginners don't!
You should still explain what standard C requires if you want to claim
this is an introduction to standard C. At the very least you should make
it clear that these sizes may vary on different implementations.
Yes
You are right. I added the sizes and size changes for the 64 bit version of
lcc-win. This makes it obvious that those sizes change.
The point is that it is more harmful that helpful to think of pointers
as integers. (And that machine addresses are not necessarily just
integers, regardless of how many implementations you find where they are.)

Well, this is a disagreement. I hope you do not mind.
No, it isn't. That suggests that this code:

union {
int i;
long l;
double d;
} onion;
int main(void) {
onion.i = 1;
onion.l = 2;
onion.d = 3.0;
printf("onion.i is %d\n",onion.i);
printf("onion.i is %ld\n",onion.l);
printf("onion.i is %f\n",onion.d);
return 0;
}

will output
onion.i is 1
onion.l is 2
onion.d is 3.0000000

The problem is that I'm not accessing the variables simultaneously, but
I am *using* them simultaneously. There's an important difference.

You *cannot* store multiple values in a union simultaneously. I'm sure
you know this, but your language is sloppy and confusing.

Mmmm. I will try to rework that sentence. I guess you are right.
Thanks
It's terrible practice.

Why?

The operating system will cleanup after the program closes!
Why it is a bad practice?

I mention this as a valid memory allocation strategy in the chapter
about memory allocation!

If the programs makes only allocation and almost no free() then why
bothering with freeing the memory at all?
No, but it is advertised as a hash function which will do. If it will do
for an example, a user will expect it to do for his code. When he thinks
about hash functions, he will remember the one you provided and
copy/paste it in. (And why shouldn't he? After all, reinventing the
wheel is bad.)

Then he/she will have a hash function that works but it is not very
fast. There are hash functions for different purposes and
you can write BOOKS about the subject!
n1256, 6.10.8p2:
The following macro names are conditionally defined by the implementation:
__STDC_IEC_559__ The integer constant 1, intended to indicate
conformance to the specifications in annex F (IEC 60559 floating-point
arithmetic).

If the implementation does not define __STDC_IEC_559__, it is only
constrained by the lower limits specified in 5.2.4.2.2.

OK OK, but is this necessary in a tutorial? I mean why putting all the
complexity of the standard and the possibility of non floating point
implementations into new users of the language?
Look again. Windows programming is in Chapter 3. There is no chapter 2.
(Though the contents do reference it.)

I am using an old version of frame maker from adobe since I have no
money for the upgrade (US$ 350).

And it has bugs DAMM!!!
And I did not see that one.
Show me an example of bias and I will accept it. I think my comments
have been fair.

Yes. That is true. Thanks for your help.
 
J

jacob navia

Keith said:
Do you discuss the rules imposed by the standard, for example, that
int is at least 16 bits?

Correct. I will add that.
Well... that's a union dear.

Storing a value in one member of a union, result of accessing a
different member is unspecified except in some very narrow
circumstances. (Consider that when you're wrong about something, and
somebody points it out, that's not a good time to be condescending,
"dear".)

[...]

What is your question? He's right, the C standard doesn't require
IEEE 754 floating point. Do you dispute that, or does your tutorial
not actually make that assumption?

I think that implementations without floating point are disappearing
quite fast. Actually, the only one I used was in a DSP some years ago,
where we did not bother with that since the circuit did not support it
and we had like 40-50K of RAM.

But those are extreme environments.
 
J

J. J. Farrell

The best resources by FAR are the Linux/Gnus source codes. Big dirty
applications with bugs to fix. You might not learn "Ansi C", but you
will learn real world Linux C in no time.. especially if you submit a
bug fix which is not done properly...

On the contrary, there are much better resources along these lines
available. If diving into OS code and OS utilities is an approach you
want to try, I'd recommend the OpenBSD sources at http://www.openbsd.org/.
The code is generally of extremely high quality with few bugs, and
largely written in Standard portable C. The style is generally clear,
straightforward, and easy to read - and consistent.

I'm not sure that diving unguided into such a big sea of code, some of
which is very complex, is a good approach for someone just learning C.
It may suit some people and not others. If you want to try, I'd start
off by finding the source of a small utility whose functionality you
already know, and looking at that.
 
P

Philip Potter

jacob said:
Yes. Under windows it means that your output is discarded.
Under linux it means the same: your output is not visible.

OF COURSE YOU KNOW THAT. But beginners don't!

Under linux your output appears in the tty you ran the program from.
Under windows you can generally access it: in the DOS box you ran it
from, or in MSVC you can view it in the Output window.
Yes
You are right. I added the sizes and size changes for the 64 bit version of
lcc-win. This makes it obvious that those sizes change.

Well that's a start. However when writing portable C it is important to
know the minimum sizes of the types - if you know that int may not be
able to store numbers >32767 you will know to use long for greater numbers.
Well, this is a disagreement. I hope you do not mind.

*shrug* I won't lose any sleep.
Why?

The operating system will cleanup after the program closes!
Why it is a bad practice?

I mention this as a valid memory allocation strategy in the chapter
about memory allocation!

If the programs makes only allocation and almost no free() then why
bothering with freeing the memory at all?

If you are sloppy with scratch code you will also be sloppy with
production code. If you are in the habit of free()ing every malloc() for
scratch code, you will also be in the habit of free()ing every malloc()
for production code.
Then he/she will have a hash function that works but it is not very
fast. There are hash functions for different purposes and
you can write BOOKS about the subject!

That is why good tutorials and textbooks do not define a hash function
when discussing hash tables but instead include references to find good
ones. A poor hash function negates the entire purpose of a hash table!
OK OK, but is this necessary in a tutorial? I mean why putting all the
complexity of the standard and the possibility of non floating point
implementations into new users of the language?

Why put all the complexity of IEC 559 into your tutorial when you can
keep to a general overview of floating point? And why confuse your users
into thinking IEC 559 is guaranteed?
I am using an old version of frame maker from adobe since I have no
money for the upgrade (US$ 350).

And it has bugs DAMM!!!
And I did not see that one.

I can recommend pdflatex. It's free. It's not buggy. And it has very
pretty output.
 
M

Mark McIntyre

.If you read the
responses throughout your thread you can see that the regular
contributors are more interested in slinging mud and arguing with each
other than participating in useful discussion

This is horsefeathers, but from someone posting from a throwaway email
address, we can expect no less. .
Stick with books,

This is good advice. Most web tutorials are garbage, written by the
sort of people who contribute articles to Wikipedia.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top