On 25/04/13 19:00, Johannes Bauer wrote:
> On 25.04.2013 18:00, David Brown wrote:
>
>>> "main" has a return parameter which is an int. Always.
>>
>> No, "main" does not always have a return parameter
>
> Well, if the code conforms to the C standard, it should return int.
>
>> - "main" does not
>> always return, and therefore a return parameter is meaningless.
>
> It's mandated by the C standard. So if you want to have a standard
> compliant program, it is very meaningful. Not in a way that transports
> any information (because the return value itself may be discarded).
>
>> In most
>> (small) embedded systems, main() never exits.
>
> I disagree completely. Is a 4 bit microcontroller small enoguh to
> qualify as "small"? Because these are the smallest I've programmed (in
> C). And nowhere, ever, have I encountered an environment that has no
> main somewhere (sure, maybe hidden from the application developer, but
> it is indeed there when you look for it).
>
I wrote "main() never /exits/", not "main() never /exists/", which I
think is what you thought I wrote.
I certainly cannot remember ever having written a main() in a small
embedded system that ever exited, except by a re-boot or re-start to a
different program (such as to enter "bootloader" mode).
Most of my programs have had a main() function, but I've also written a
few that don't. Occasionally I've written my own startup code to
establish the C run-time environment (clear out bss, copy initialised
data, set up the stack, etc.), and there is no need for any function
called "main" in that case. Of course, it would no longer be strictly C
standard :-) I haven't used any 4-bit micros, but I have used an 8-bit
micro with no ram and only a three-entry hardware return stack - I
certainly didn't want to waste part of that stack on calling a main()
function that doesn't return.
>> Including a return
>> parameter may mean wasted code, and will mean your compiler and/or
>> static code checker will warn about unreachable code (if you include a
>> return value) or missing return values (if you don't).
>
> Any smart compiler will optimize that away. And standard compliance is
> more important than that one instruction (even on a 8 bit MCU!) that
> sets the return value.
Why? What is the point of having a "return 0;" statement at the end of
main() that cannot return? It's true that the compiler will usually
omit it in the object code, but you are trading compliance with a
meaningless (in this context) rule in the C standard for breaking common
rules in embedded programming coding standards. You are including code
that is never run, and never tested - that's always bad. You are
generating warnings from your compiler - also contrary to common rules
that require warning-free compiles.
Strict compliance with the C standards is often important, just as
writing portable code is often important, but not if it gets in the way
of writing good quality code for the job in hand.
>
>> Is such a "void main(void)" declaration valid according to the strictest
>> C conformance? Perhaps not - but then, most (probably all) embedded
>> compilers and embedded programs are non-conforming in at least a few
>> small ways. They often have C libraries with limitations, floating
>> point implementations that are not fully IEEE, extra keywords for
>> special memory handling, etc.
>
> Actually, many deeply embedded systems have no floating point support at
> all (because the sheer soft-float library is so large that it would
> completely fill the ROM. I'm actually not sure if float support is
> required by the standard, but would guess that it might be the case.
>
Of course most small embedded systems don't /use/ floating point (and
certainly not the whole standard library), but the standards require the
compiler and library to support it.
>> I don't know whether this is from an official standards document, but
>> point 167 clearly says that "main" can be defined "in some other
>> implemention-defined manner".
>
> Hm, interesting. Would be interesting to know what is meant by that.
> Because "implementation-defined manner" can also mean that nothing
> (neither the prototype nor the name 'main' itself is required).
>
My reading of the page is that "main" is required, but that forms such
as "void main(void)" are valid.
>> Do I think that most C programs are written for embedded systems? No, I
>> don't - that's why I did not say so.
>
> Gotcha. Misunderstood you there, sorry about that.
>
No problem. Misunderstandings are part of the fun of newsgroups.
>> But if someone tells me he wants to learn to program and asks what
>> language to use, I would only say "C" if it was for embedded systems.
>> If someone asks me what language is appropriate for a given project,
>> then the answer is seldom "C" - unless it is an embedded system, a very
>> low-level program, a high-speed library, or a very time-critical program
>> (even then C is often not the best choice).
>
> I agree.
>
>> I think this difference is particularly apparent in newer, younger
>> programmers. There was a time when people used C for everything from
>> small scripts or utilities to large applications - and people who are
>> used to that, continue to do so. But for a new developer, you would be
>> better off with Python, Java, Ruby, C++, etc.
>
> Couldn't agree more.
>
>> Of course, being good at C programming is an essential skill for /any/
>> expert programmer, even if you do most of your work in other languages.
>> It's like having a good understanding of assembly is essential for
>> being an expert C programmer, especially on embedded systems.
>
> Couldn't agree more here as well.
>
> Best regards,
> Johannes
>