C help please - C newbie - Page 4

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Re: C help please - C newbie
Quoted text here. Click to load it

Not this one either :-(
(Yes I do have a copy)

Quoted text here. Click to load it

Nothing could be further from the truth.  K&R 2nd ed was published
before the  US C89 or the ISO C90

Most C books cover the C90+TC's and A1 (ie C93 or C95) which is what
most compilers use as a base. Compilers are moving (slowly) towards C99
as are most books. This leaves K&R very out of date.

It is an interesting book to have once you have learnt C in much the
same way I would like to drive a model T Ford but I would not recommend
learning to drive on the freeway in one.


Quoted text here. Click to load it

Strange since I review books on that and we dissagree :-)



/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England    /\/\/\/\/\
/\/\/ snipped-for-privacy@phaedsys.org       www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Re: C help please - C newbie

Quoted text here. Click to load it

Why don't you tell us all exactly which features of C90-C99 are
misrepresented or left out of K&R2? I'm especially interested in
learning what things that would be of interest to the typical
embedded systems programmer are left out of K&R2. Please enlighten
me.



Re: C help please - C newbie
 
Quoted text here. Click to load it

Right now I am working on a eZ8 system.  This will have an LCD 20x4,
I2C, key pad interface, Serial port (USB may come after) some other
I/O doing miscellaneous stuff.

Quoted text here. Click to load it

Re: C help please - C newbie
Quoted text here. Click to load it

K&R is probably not a good idea then. I would look for a book on C that
uses the Z8 as a target. This will probably be based on the compiler you
will end up using. See what the compiler vendors for the z8 recommend.

Whilst it will not be portable C neither is any one else's C. Most
"standard" C books use M$ Windows as a platform and the C you will use
on that is going to be different in to the C on an embedded target.




/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England    /\/\/\/\/\
/\/\/ snipped-for-privacy@phaedsys.org       www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Re: C help please - C newbie
Quoted text here. Click to load it

C is just high-level assy. You should do well when you get this syntax
figured out.

the "static" keyword means two totally differnt things in C (and C++ added
yet a third meaning :-(  ).

1) static int foo(void) is an example of a function that can only be
referenced (called) by code in the same C file (translation unit) that foo
is in. The linker will not be able to resolve foo called by other C file
code to this version of foo. This form of static applies to variables too.
(e.g. static float x is invisible to other C modules)

2)Inside a function, static means that the variable retains its value
between calls of the function. As you know, normally (non static) automatic
varaibles are allocated on the stack and can have any old garbage in them
when a function begins execution. static allocates the variable elsewhere in
RAM.

corollary to 1) linkage (the ability of the linker to resolve a reference to
function or variable) is global by default (works across all source files).
The static keyword *prevents* the linker from resolving a reference. This
makes static good for hiding code and data from modules that shouldn't be
messing with them. Think of it as an early form of encapsulation.

The "extern" keyword tells the compiler about a function or variable that is
not defined (coded or allocated ) in the currrent C file. Using extern, you
tell the compile that foo is a function returning int that takes no
arguments. The compiler can then call foo with the appropriate boilerplate
(e.g. don't push anything onto the stack but do allow the returned int to be
assigned).

Good practices:
1) read K&R and keep it handy. bookmark the precedence table and the
declaration syntax pages.
2) turn the compiler warning level up to full (use lint too)
3) encapsulate functional units in separate C files. Each C file should have
an H file that defines the functions and variables that other code needs to
use the C file AND NOTHING ELSE. If you find that you need to put an extern
into a C file to refernce a different C file then take a little time to
ponder why your H file doesn't provide an adequate interface.
4) step through some of your code on the target system in assy mode. If you
can't do that, then at least look at the assy output of the compiler
(usually a command line arg will generate an assy listing for you). The assy
code never lies!

good luck,
Bob

Quoted text here. Click to load it



Re: C help please - C newbie
-Hi, I am an Assembly language programmer of 12 years, now starting to
-code in C. I am still a C newbie and have some confusion.
-
-I am finding that I am getting bogged down with uses of static /
-extern for functions and variables. I am also getting a bit confused
-with function prototyping and header files. I have searched the
-internet quite a bit but I am still confused.
-
-I think that I know the following;  (Please clarify)
-
-Static variables are local to the .c file.

Global statics are. Local statics are local to their block
(typically a function).

- Static variables also can stay around after the function is exited.

Correct. This applies specifically for local statics, because global varaibles
both static and external always have this property.


-A variable declared outside of Main is global

Outside of any function.

- and a .c file

other than the file the global is declared in...

- that wants to use it must extern it.

Correct.

-
-Static functions are visible to the .c file.  My problem here is why
-declare them static?  Isn't static assumed?

Nope. Actually extern is the default for all global entities. So if you
don't want outsiders to the file to use global functions/variables you must
attach the static tag.

-A function can be called by other .c files if its prototype is
-External.

Yes, which again is the default.

-
-I have read that it is good C practice to code prototypes for each
-function.

Absolutely. Want an example? try this:

-------- file1.c ---------------
main()
{
   f1(3);
}
--------------------------------

--------file2.c-----------------
#include <stdio.h>

void f1(double val,int junk1,int junk2)
{
   printf("val = %f junk1 = %d, junk2 = %d\n",val,junk1,junk2);
}
--------------------------------

Compile and link together. Fails miserably but still runs. A big mess.
However if you include the prototype in file 1:

-------- file1.c ---------------
void f1(double val,int junk1,int junk2);

main()
{
   f1(3);
}
--------------------------------

It magically generates an error because the prototype gives the compiler enough
info to know that the call is incorrect. Also if f1 had the correct number
of parameters, the int to double conversion of the 1st parameter also
automagically happes because of the prototype.

Definitely prototype before you use. I wish there was a way to force the
compiler to require prototypes before use.

-  Should I code the prototypes in a .h file associated with
-the .c file?

There are different ways of doing it. It depends on usage. Generally I'll
dump all of the prototypes into a single project .h file then include it
into all the source files. However if it's a bit of code, like a hash table
for example, that has multiple uses in multiple projects, then I'll give it
its own header file.

There isn't necessarily one perfect has to do it. But there is no doubt that
it needs to be implemented somehow.

-  Where do I extern the prototype if I plan to use the
-function in multiple .c files ?

See above.

BAJ

Re: C help please - C newbie
To everyone that has posted a reply, I will try to put your
suggestions into practice.

thank you,

Bradley



snipped-for-privacy@canoemail.com (Bradley) wrote in message
Quoted text here. Click to load it

Re: C help please - C newbie
Quoted text here. Click to load it

Never, ever read anything by Herbert Schildt.

pete
--
snipped-for-privacy@fenelon.com "there's no room for enigmas in built-up areas."

Re: C help please - C newbie
writes
Quoted text here. Click to load it

AGREED!!!


/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England    /\/\/\/\/\
/\/\/ snipped-for-privacy@phaedsys.org       www.phaedsys.org \/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Re: C help please - C newbie
Sorry I can't help with the original question, I have a question of my own.

You may laugh at the next bit but....

In 1982 in had a BBC micro (people in the UK will know what I mean).

I played with it for ages and could write in BBC basic pretty much what I
wanted, if I needed more speed I just dropped in to assembler BBC basic
would let you do that.

I could talk down the serial bus, centronics etc. bus with absolute ease.

Then I thought I'd try some other languages such as Forth and C.

C amazed me. I looked 20 years out of date 20 years ago. The syntax looks
to the untrained eye like somebody's tried to press as many shifted character
keys as possible i.e. $%& etc. And it was so slow in comparison to what I
been used to. I seems to me you have to include so much junk to do a
simple job like talk to the serial port your programme ended up being
bigger than the BBC Basic interpreter just to do 1 simple thing.

Surly by now there must be something better because its look to me like
there have been no advances in programming since 1982. If anything it
looks worse. Is this why so much modern software is in all honesty, junk,
is it written in C.

Re: C help please - C newbie

Quoted text here. Click to load it

But that's because C is lower level than Basic.

Quoted text here. Click to load it

"Better" is in the eyes of the beholder.  Some people want to write a
one line program to do everything, other people want control over the
intermediate stages.  It's a single command to delete all the files on
your hard disk, but does that mean the command line language is "better"
than assembler?

Do you want a language where you can just drag a text GUI window onto
a titlebar window in order to create a functioning word processor, do
you want a language where you can fit your device driver into 4K of
RAM, or do you want a language where file operations can be abstracted
into database operations?

--
Darin Johnson
    "Floyd here now!"

Re: C help please - C newbie

Quoted text here. Click to load it



Well I ended up taking to hardware route I mainly design PCBs these days.

I think using C on a day to day basic would have driven me nuts.

I'm sure your right about the best and worst software.

I would like to have a play with higher level languages again though
so if have any suggestions.

I also work with some programmable logic which gives me the option
to write the code in a C type language or I can draw a schematic
and then convert that into code for the device. As you can probably
guess I prefer the schematic method.

Is there any software analogy to this, i.e. text or graphical input.



 










Re: C help please - C newbie

Quoted text here. Click to load it

I don't suppose you use ruby tape and mylar these days, eh?

Quoted text here. Click to load it

Python: www.python.org

Quoted text here. Click to load it

Sort of. But the graphical stuff never seems to get very far,
since it usually has to be problem-domain-specific. I did a MS
project where you draw ladder diagrams and they get converted
into source code (C, asm, whatever). Initially, I did the
ladder diagrams using the old Daisy CAD system and ran the
net-lists through my SW.  Later I wrote a diagram editor to go
along with it.  If you're really interested, the paper is here:
http://www.visi.com/~grante/ladder.pdf and the source code is
here: ftp://ftp.visi.com/users/grante/stuff/led.tar.gz

--
Grant Edwards                   grante             Yow!  - if it GLISTENS,
                                  at               gobble it!!
We've slightly trimmed the long signature. Click to see the full one.
Re: C help please - C newbie

Quoted text here. Click to load it

That sounds very interesting I'll download the pdf.

Sometimes I wish I still did route by hand instead of thinking up ways
to trick powerpcb into letting me do things it thinks are wrong!
I would miss my autorouter though.

Thanks for python link as well.

Cheers

Paul.





Re: C help please - C newbie

Quoted text here. Click to load it

Some of the worst software around is written in C.
Some of the best software around is written in C.

It ain't the language.

--
Al Balmer
Balmer Consulting
We've slightly trimmed the long signature. Click to see the full one.

Site Timeline