C coding guidelines

T

Tech07

Nobody said:
ELF shared libraries can also be executables:

And DLLs are basically executables. I'm not the one to ask about the details
of such though (I'll cross that bridge when I reach it).
 
T

Tech07

Richard Heathfield said:
Sure - at a system level. But at the level of C source, which is what
we're actually talking about (or, at least, what I'm actually talking
about, and which is topical here), the function simply isn't there.
Whether or not the system adds it as part of its internal startup
code is neither here nor there from a C source perspective.



I just created a DLL to test your theory. It has no
_DLLMainCRTStartup. It does have a lot of extraneous junk, but that
isn't one of it, so to speak.

"Extra! Extra! Read all about it!":
http://msdn.microsoft.com/en-us/library/988ye33t.aspx

Would _you_ expect such code that calls your DLL to be in the DLL? Surely it
gets linked to the calling executable via the provided system libraries.
 
A

arnuld

Interestingly, the Linux kernel coding guidelines use 8 column tabs to
enforce code simplicity: if it spills over 80 columns it probably needs
to be split into a new function anyway. (Normally I'd object to this
kind of crazy blanket rule, but it seems to have served quite well over
the years.)
With 8 spaces, I always feel like my eyes have to travel too far. (It
makes it tougher to fit code in reduced margins, too, which is probably
one of the reasons for 4-space indents in K&R2.)


I too use 4 space indent (default on emacs IIRC). All of my colleagues
use 8 tab indent and trust me 30% of their code has one expression
overlapped over 2 lines which make me very difficult to read and
understand their code.

2nd, little OT though, I wondered on this aspect Linux kernel coding
style http://www.kernel.org/doc/Documentation/CodingStyle :


"First off, I'd suggest printing out a copy of the GNU coding standards,
and NOT read it. Burn them, it's a great symbolic gesture."


I was really put off by this. I have read some of GNU coding guidelines
and did not find anything wrong with them that I have to burn or neglect
them. 2nd, though GNU is quite popular for its free software, free
society philosophy, I think their Softwares created a decade ago,
technically, are quite superior to what MNCs create today.
 
A

arnuld

...SNIP..
Just my opinion, mind you. MISRA-C is a better starting point for
certain types of C coding than the alternatives, mainly because there
really are no alternatives. But it is far from perfect on its own.

While searching for MISRA-C, I came across "The New C Standard: An
Economic and Cultural Commentary" : http://www.knosof.co.uk/cbook/
cbook.html


What is this exactly and what are opinions from CLC on it ?
 
G

Guest

|>
|> > On Mon, 31 Aug 2009 08:07:04 +0000, Richard Heathfield
|>
|>
|> <snip>
|>
|> >>> My own opinion is that (a) tabstops should be 8 columns
|>
|> >>Two!
|>
|> > Two or four. I admit to having been a fourist in the past, although
|> > I've converted to twoism.
|>
|> Likewise, although I confess to having flirted briefly with threeism.
|> That was a bad move, and I abandoned it almost immediately.
|
| I've nearly always used one space. Last time I used actual tabs, my
| code would rapidly disappear off the right edge of the teletype paper
| (which used 72 columns iirc).

Some would argue your lines are too long. Others would argue your teletype
is too narrow. Some might say you are destroying trees. Others would say
trees are for recursing.
 
G

Guest

| On 31 Aug 2009 05:13:33 GMT, (e-mail address removed) wrote:
|
|>
|>| then how do people think about this one: http://www.kernel.org/doc/Documentation/CodingStyle
|
| [snip]
|
|>I don't think 3 levels of indentation means you are screwed. And the limit
|>on 80 columns is completely outdated. I'm using a 142 column display. Any
|>that can't use at least 120 columns is still using old technology. Upgrade.
|>80 columns is for punch cards.
|
| And printed copy. And usenet postings. And DOS windows. Usw.

I've been printing way more than 80 columns since the mid 1980's when working
on IBM mainframes. I had a graphics printer that did 270 columns so I split
output into 2 flow columns of 132 characters each.

DOS isn't limited to 80 columns. Usenet isn't, either, though courtesy does
urge limiting text to less than 80 (preferrably less than 72). But for code,
especially when referenced by URL, I'm back to my 132+ column formats.

Just because there are things around that some people might still use which
have no more than 80 columns, this should not limit everyone to format code
to no wider than that.

Here is what my screen currently looks like:
http://phil.ipal.org/usenet/clc/2009-09-01/screen-0.jpg
 
G

Guest

| I think "arnuld" knows that. Remember he is describing the poor
| education system of his native country, and is not telling you that
| Schildt is the best, but that his /teachers/ said Schildt was the
| best. There's a difference.

The difference between him and his teachers. But he did not say one way
or the other. I was making sure.
 
G

Guest

| (e-mail address removed) wrote:
| ...
|> | who use void main() or other stupid things. Herbert Schildt was one of
|> | the popular authors to learn from during my graduation. My teachers
|> | told me that he was the best selling author on C and C++ and Wikipedia
|> | conforms that.
|>
|> Best selling != best. A friend of mine, whom I taught C programming to,
|> actually lived across the street from Herbert Schildt in Mahomet, IL for
|> a while. The stories I've heard cannot be repeated online.
|
| Stories from a neighbor that "cannot be repeated online" sound unlikely
| to be relevant to his competence (or lack thereof) as a author of C
| programming books. Are they?

Some are.
 
J

James Kuyper

Bart wrote:
....
I've nearly always used one space. Last time I used actual tabs, my
code would rapidly disappear off the right edge of the teletype paper
(which used 72 columns iirc).

With identifiers that are long enough to avoid confusion, you'll always
have to wrap many of your lines. Howevever, if you feel a need to use an
indent of 1 in order to minimize the wrapping, I suspect you're nesting
your blocks too deeply. Take some of your blocks and make them separate
functions.
 
J

James Kuyper

I've been printing way more than 80 columns since the mid 1980's when working
on IBM mainframes. I had a graphics printer that did 270 columns so I split
output into 2 flow columns of 132 characters each.

DOS isn't limited to 80 columns. Usenet isn't, either, though courtesy does
urge limiting text to less than 80 (preferrably less than 72). But for code,
especially when referenced by URL, I'm back to my 132+ column formats.

Just because there are things around that some people might still use which
have no more than 80 columns, this should not limit everyone to format code
to no wider than that.

Just because you can do something doesn't mean it's a good idea to do
it. Tiny print is hard on your eyes. Long lines are also hard on your
eyes - your eyes have to jump from the end of one line to the beginning
of the next. Thats why newspaper columns are as narrow as they are.
 
J

James Kuyper

Tech07 said:
I don't know what the standard says about implementations at that level (std
library initialization and the like).

What it says is that "In a freestanding environment (in which C program
execution may take place without any benefit of an operating system),
the name and type of the function called at program startup are
implementation-defined."(5.1.2.1) but that otherwise, "The function
called at program startup is named main." (5.1.2.2.1p1). It then imposes
requirements on the values of the parameters of main(). Finally, it says

"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."
(5.1.2.2.3).

It goes into no more detail than that, about the code that an
implementation must generate that sets up the call to main(), and reacts
when that call returns. Therefore, the nature of such non-user code is
highly implementation-specific, and is best discussed in a forum
specializing in the particular implementation you're interested in.
While such code is, in a very important sense, the real entry point for
the program, it is not the entry point from the point of view of C
programming.
 
A

arnuld

The difference between him and his teachers. But he did not say one way
or the other. I was making sure.


Well, I did mean what exactly Richard Heathfield said. Its not even about
C, it has more to do with "feeling for truth". No one told me that CLC is
right place to learn C. I have shown CLC to all of my friends and guess
how many agree with that CLC is a genuine, unbiased and purely technical
place to learn C ? only one friend in 4 years . Almost all of them
still use C books that were recommended in their graduation. If I tell
you about the comments they have given me you will ROTFL:

1) No one learns from K.N.King.
2) Richard Heathfield does not know much of C ... :-O
3) what the heck are C FAQs ? A Waste of resources ...
4) Beej Jorgensen does not know much about programming
5) If Bjarne Stroustrup created C++, it does not mean he is good at C++
6) If someone wrote errata of Herbert Schildt's books and criticizes
his work , it does not qualify him to be technically competent as
compared to Schildt.
7) This books says void main(), CLC says int main(). Do you any proof
which is right ? nope... so you can't tell me that int main() is the
right way to write a C program.
8) There are even some jokes about "arnuld" and "his newsgroups"

I did not read in "Geeta" that CLC teaches C. Its just you feel that
people here are hanging from more than 20 years. Barack Obama did not
make me phone call to tell me that int main() is right. Its just common-
sense to distinguish between right and wrong. These comments hurt me not
because they made fun of regular folks and me, they hurt me because these
people were(are) my friends and they don't want to see/hear the right
thing. I never understood the reason behind this. End result: I have
stopped advising people on C.

NOTE: there is no exaggeration in what I have written.
 
B

Bart

"Extra! Extra! Read all about it!":http://msdn.microsoft.com/en-us/library/988ye33t.aspx

Would _you_ expect such code that calls your DLL to be in the DLL? Surely it
gets linked to the calling executable via the provided system libraries.- Hide quoted text -

This seems to be about link-loaders specific to the C language. They
can do what they like regarding the entry points they need to
initialise the CRT, and I would expect they are all a little
different.

The link you provided was concerned with the C++ language, and
presumably MS's version of it.

I wouldn't trust the linkers I use at the minute (lcc-win and gcc for
Windows) not to put in any C-specific gummph in the executables. Maybe
there are complicated options to omit all C-related setup code and
entry points, and produce a pure, language-agnostic executable, but
I've no idea what they might be.

(With 16-bit Windows however, I did make my own arrangements for
creating executables, and those *were* much purer (I wasn't using the
CRT then). IIRC they included a list of imported Dlls and functions,
to allow OS routines to be called.)

So I doubt DLLMainCRTStartup would be an entrypoint meaningful to
Windows, it sounds like something created by your specific C
implementation. The clue is in that "CRT", while presumably DLLs can
have been created in any language.

One of these days I might create my own loader for creating PE-format
executables, and I'll be able to back up my claim. But don't hold your
breath.
 
B

Beej Jorgensen

arnuld said:
4) Beej Jorgensen does not know much about programming

I know enough to get terrifying glimpses of the amount I don't know.
And, as time approaches infinity, the fraction of the computing field in
which I am proficient approaches zero. :)

Sooner or later, all the young quantum computing whippersnappers are
going to come eat my lunch. :)

But at least there's always something new to learn, and that's the fun
part!

-Beej
 
B

Beej Jorgensen

Richard Heathfield said:
Beej has only come to (returned to?) comp.lang.c relatively recently

Actually, it's my third trip. My first Google-archived posts in the
group are from 1993.
Anyone who thinks Beej doesn't know much about programming doesn't
know much about Beej.

I must have a pretty good con running. ;)

-Beej
 
T

Tech07

Just because there are things around that some people might still use
which have no more than 80 columns, this should not limit everyone to
format code to no wider than that.

I like 80 column lines because I like to see more source windows across the
screen in the IDE with enough lines showing to be able to see the context of
the code well.
 
T

Tech07

Richard said:
Clearly we are disagreeing over the meaning of the term "entry point".

Not really. I was just pointing out the existence of something else called
"entry point" (and, yes incidently, it's what I consider the entry point). I
noted that the terminology is overloaded: I don't have a problem with that.
The whole WinMainCRTStartup-like think is not nearly as esoteric as I made
it seem: I would expect every programmer building DLLs on Windows to know
the underlying mechanics of startup code and such.
For me, the entry point into code is within the code itself, just as
the front door of a house is in the actual house. Right on the edge?
Sure. That's where entry points have to be. But it is actually in the
house, nonetheless. Go to the house, see the door(s). Go to the code,
see the entry point(s). If I look carefully all around the code and,
despite finding several ways into it (i.e. the entry points), can't
find the function you mention, then the function you mention is not
an entry point as far as I'm concerned.

"No skin off my nose": I understand your use of the terminology, it's no big
deal, but when _I_ say "entry point" I mean the real one (hehe ;)). The
thing is though, using "entry point" to mean "main()" could "hide"
interesting (I found the stuff interesting when it was published in MSJ,
(yes, back when that journal was a blast to read, 199x-ish)) stuff for those
who like to get to the source of things. Back then, I never ever thought I'd
actually be using the info! Anyway, I recommend learning assembly language
to all aspiring C/C++ (and similar level languages) developers cuz, well, I
know a little bit of it now, and knowing the underpinnings makes me design
better code (OK, so sometimes I do too much premature optimization... I'm
currently in counseling for it ;) ).
 
T

Tech07

Bart said:
This seems to be about link-loaders specific to the C language. They
can do what they like regarding the entry points they need to
initialise the CRT, and I would expect they are all a little
different.

The link you provided was concerned with the C++ language, and
presumably MS's version of it.

Well of course, the other poster was talking about Win32 programming.
Windows is largely C (probably entirely). You don't see any classes in the
Win32 API do you? Well, sure, now that there is C++ the startup code has to
address those requirements, but like I already noted, if you're just doing
C, the task of replacing the startup code (if you have such a bent) is a bit
easier.
I wouldn't trust the linkers

Me neither!! I bought a car from a linker once (live and learn!). Never
again! Fool me once, shame on you. Fool me twice, shame on me! I'm with you
on that one: gotta keep an eye on those linkers, or they'll take you for
everything you've got!
I use at the minute (lcc-win and gcc for
Windows) not to put in any C-specific gummph in the executables. Maybe
there are complicated options to omit all C-related setup code and
entry points, and produce a pure, language-agnostic executable, but
I've no idea what they might be.

I don't know what your point is in that passage, but I assure you that on
Windows, with the MS linker it's simple a switch (as in "light switch") to
send all startup code related to C library initialization to la la land. So
not complicated at all. Now if you want to REPLACE that code, that gets a
bit more involved. I'll probably be building my code that way some day,
today though, I don't have all the info of what the startup code does: only
the basics. I think startup code should be provided in source code form.
Separate the proprietary OS code from the general language code. (Have to
stop here on that, or I'll start cursing the money-mongers).
(With 16-bit Windows however, I did make my own arrangements for
creating executables, and those *were* much purer (I wasn't using the
CRT then). IIRC they included a list of imported Dlls and functions,
to allow OS routines to be called.)

So I doubt DLLMainCRTStartup would be an entrypoint meaningful to
Windows,

Well maybe then you should go talk to Bill Gates and tell him to REMOVE it!
You go man!
it sounds like something created by your specific C
implementation.

Did you even go to the MSDN link I posted and read what was there?
Apparently not.
The clue is in that "CRT", while presumably DLLs can
have been created in any language.

What language do you think Windows is written in? Visual Basic? Bzzzzt!
Wrong! Try again.
One of these days I might create my own loader for creating PE-format
executables, and I'll be able to back up my claim. But don't hold your
breath.

Oooops! I seemed to have missed "your claim". So, what are you "claiming"?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top