Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Good C programming style

Reply
Thread Tools

Good C programming style

 
 
Sensei
Guest
Posts: n/a
 
      10-07-2005
Hi!

I'm thinking about a good programming style, pros and cons of some
topics. Of course, this has nothing to do with indentation... Students
are now java-dependent (too bad) and I need some serious motivations
for many issues... I hope you can help me I begin with the two major
for now, others will come for sure!

- function variables: they're used to java, so no pointers. Personally
I'd use always pointers, but why could be better to use:
void myfun(int &i) or (int *i) or (int i)
given that the function can and cannot (the last case of course) modify
a parameter. I see performance issues (stack related)...

- defines: why they use function and variable defines? why they shouldn't?
#define MY_PI 3.1415
#define fun(x) ((x) * (x))


--
Sensei <(E-Mail Removed)>

The difference between stupidity and genius is that genius has its
limits. (A. Einstein)

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-07-2005
"Sensei" <(E-Mail Removed)> wrote in message
news:di6gc0$m52$(E-Mail Removed)...
> Hi!
>
> I'm thinking about a good programming style, pros and cons of some topics.
> Of course, this has nothing to do with indentation... Students are now
> java-dependent (too bad) and I need some serious motivations for many
> issues...


So you're teaching C?

> I hope you can help me I begin with the two major for now, others will
> come for sure!
>
> - function variables: they're used to java, so no pointers. Personally I'd
> use always pointers, but why could be better to use:
> void myfun(int &i)


but you don't appear able to distinguish C from C++

> or (int *i) or (int i)
> given that the function can and cannot (the last case of course) modify a
> parameter. I see performance issues (stack related)...


and you don't appear to be aware of the consensus about
premature optimization

>
> - defines: why they use function and variable defines? why they shouldn't?
> #define MY_PI 3.1415
> #define fun(x) ((x) * (x))


and you don't appear to be aware of the type-checking that a
real function can give, and that macros do not.

>



Running quickly in the other direction,
-Mike


 
Reply With Quote
 
 
 
 
Skarmander
Guest
Posts: n/a
 
      10-07-2005
Sensei wrote:
> I'm thinking about a good programming style, pros and cons of some
> topics. Of course, this has nothing to do with indentation... Students
> are now java-dependent (too bad) and I need some serious motivations for
> many issues... I hope you can help me I begin with the two major for
> now, others will come for sure!
>

The questions you're asking are elemental. Read a good book on C and
these topics should be treated in some detail, along with explanations
that will help you decide on what is appropriate and why.

> - function variables: they're used to java, so no pointers. Personally
> I'd use always pointers, but why could be better to use:
> void myfun(int &i) or (int *i) or (int i)


int &i is not C. You may be thinking of C++.

As for when to use pointers and when not, it's simple: pointers for
variable-sized data like strings, pointers also to implement
call-by-reference in C, that is, arguments you'll need to change.
Pointers also for structs, see below. And finally, pointers for
functions, since there's no other way to pass them.

> given that the function can and cannot (the last case of course) modify
> a parameter. I see performance issues (stack related)...
>

Mostly irrelevant. A C idiom is to always pass structure types by
pointer because passing most structures by value means copying, which is
indeed a waste of time in most cases, especially if the value isn't
modified. While a compiler could optimize this in some cases, it's not
expected to, and programmers don't count on it.

> - defines: why they use function and variable defines? why they shouldn't?
> #define MY_PI 3.1415


Established C practice is to use #define for constants since C is a bit
puzzling when it comes to constants. Declaring a variable "const" does
not make its value a constant expression for purposes of array sizes,
for example. Also, memory is allocated for const variables (great
oxymoron, incidentally when it typically isn't necessary.

There's no such thing as a "function" or "variable" define, however.
Macros just establish textual replacement rules.

> #define fun(x) ((x) * (x))
>

Use functions when you can, macros when you have to. The example above
is a case of when you don't have to.

As for when you need macros, that's not a matter of coding style.

S.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-07-2005
Sensei <(E-Mail Removed)> writes:
> I'm thinking about a good programming style, pros and cons of some
> topics. Of course, this has nothing to do with indentation... Students
> are now java-dependent (too bad) and I need some serious motivations
> for many issues... I hope you can help me I begin with the two
> major for now, others will come for sure!
>
> - function variables: they're used to java, so no pointers. Personally
> I'd use always pointers, but why could be better to use:
> void myfun(int &i) or (int *i) or (int i)
> given that the function can and cannot (the last case of course)
> modify a parameter. I see performance issues (stack related)...


The term "function variables" is misleading; you mean parameters (the
things declared between the parentheses in a function declaration) or
arguments (the actual values passed in a function call).

The declaration "void myfun(int &i)" is not valid C. (I think it's
C++; see comp.lang.c++.)

The choice between "void myfun(int *p)" and "void myfun(int i)" isn't
really a matter of style; it's just a matter of what the function
needs to do. (Note that I've changed the name of the pointer
parameter; calling a pointer "i" is misleading.) If myfun needs to
change the value of an object specified by the caller, or if you want
to point to an array of integers, use a pointer. If you just want to
pass an integer value, use an int.

Note that if myfun's parameter is a pointer, you can only pass the
address of an ojbect (or a null pointer); you can't pass 42 or x+3.

If the parameter is of a struct type, it often makes sense to pass a
pointer even if you don't want to modify the struct object. Passing
an argument means making a copy of the argument and storing it in the
parameter; for large structures, this can be significantly expensive.
This consideration doesn't apply to ints, since there's no significant
performance difference between passing a copy of an int and passing a
copy of its address.

> - defines: why they use function and variable defines? why they shouldn't?
> #define MY_PI 3.1415
> #define fun(x) ((x) * (x))


For MY_PI (BTW, everybody else's PI is closer to 3.1416), C doesn't
provide a mechanism for defining a constant floating-point value. You
can declare
const double my_pi = 3.14159265358979323848264;
but that creates a write-only variable, not a real constant. Macros
are useful for this kind of thing.

As for function-like macros, they're sometimes the best way to create
inline functions. (C99 has inline functions, but not all compilers
support them.) There are also things you can do in a macro that you
can't easily do in a function, such as using a type name as an
argument. But try not to be too tricky. Macros can be dangerous,
since they're expanded in a very early stage of compilation. They can
*look* like function calls or variable declarations, but they don't
necessarily act like them; for example, they pay no attention to
scope.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      10-07-2005
Keith Thompson wrote:
<snip>
> You can declare
> const double my_pi = 3.14159265358979323848264;
> but that creates a write-only variable, not a real constant. Macros
> are useful for this kind of thing.
>

You know, a write-only variable is about the only thing C's missing.

S.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-07-2005
Sensei wrote:
> Hi!
>
> I'm thinking about a good programming style, pros and cons of some
> topics. Of course, this has nothing to do with indentation... Students
> are now java-dependent (too bad) and I need some serious motivations for
> many issues... I hope you can help me I begin with the two major for
> now, others will come for sure!
>
> - function variables: they're used to java, so no pointers. Personally
> I'd use always pointers, but why could be better to use:
> void myfun(int &i) or (int *i) or (int i)


void myfun(int &i) is an error in C. The two options that are C that you
list are
void myfun(int *i)
void myfun(int i)

> given that the function can and cannot (the last case of course) modify
> a parameter. I see performance issues (stack related)...


C does not require a stack, but assuming a stack implementation in
either case something will be pushed on to it, either the value of an
int or a pointer to an int. Do you *really* think either will have a
significant performance difference?

Write what you mean, don't try to micro-optimise.

> - defines: why they use function and variable defines? why they shouldn't?
> #define MY_PI 3.1415


How else would you define MY_PI? Apart from more accurately, that is

> #define fun(x) ((x) * (x))


In this instance fun will work on any arithmetic type which can
sometimes be an advantage. I would generally recommend using functions
rather than function like macros *except* where you have a specific
reason to need a function like macro (e.g. if you wanted fun above to
operate on any arithmetic type and not have it convert everything to
double or long double).

Macro's can have unexpected effects such as given the above definition
if you do
fun(a++)
you actually invoke undefined behaviour because it will expand to
((a++) * (a++))
which attempts to modify a twice without an intervening sequence point.
This is why you should not use function like macros except where you
need them. It is also why you should follow the convention of spelling
macros in UPPER CASE to give people an immediate warning.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      10-07-2005
Skarmander wrote:
> Keith Thompson wrote:
> <snip>
>
>> You can declare
>> const double my_pi = 3.14159265358979323848264;
>> but that creates a write-only variable, not a real constant. Macros
>> are useful for this kind of thing.
>>

> You know, a write-only variable is about the only thing C's missing.
>
> S.


The write-only characteristic is priceless. In a former life I designed
a ram-disk subsystem, memory which should 'look' like a disk drive.
Until I got it working, I referred to it as write-only memory.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Gregory Pietsch
Guest
Posts: n/a
 
      10-07-2005

Sensei wrote:
> Hi!
>
> I'm thinking about a good programming style, pros and cons of some
> topics. Of course, this has nothing to do with indentation... Students
> are now java-dependent (too bad) and I need some serious motivations
> for many issues... I hope you can help me I begin with the two major
> for now, others will come for sure!
>
> - function variables: they're used to java, so no pointers. Personally
> I'd use always pointers, but why could be better to use:
> void myfun(int &i) or (int *i) or (int i)


int &i is not standard C.

> given that the function can and cannot (the last case of course) modify
> a parameter. I see performance issues (stack related)...
>
> - defines: why they use function and variable defines? why they shouldn't?
> #define MY_PI 3.1415


You might want to extend pi a few more decimal places. The SUSv3
standard has a macro M_PI in <math.h>, but it isn't ISO-99 C.

> #define fun(x) ((x) * (x))


This is considered bad style for a macro because the macro argument is
evaluated twice. Imagine what would happen if the argument to the macro
contained an increment or decrement operator.

Gregory Pietsch

>
>
> --
> Sensei <(E-Mail Removed)>
>
> The difference between stupidity and genius is that genius has its
> limits. (A. Einstein)


 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      10-07-2005
Joe Wright wrote:
> Skarmander wrote:
>
>> Keith Thompson wrote:
>> <snip>
>>
>>> You can declare
>>> const double my_pi = 3.14159265358979323848264;
>>> but that creates a write-only variable, not a real constant. Macros
>>> are useful for this kind of thing.
>>>

>> You know, a write-only variable is about the only thing C's missing.
>>

>
> The write-only characteristic is priceless. In a former life I designed
> a ram-disk subsystem, memory which should 'look' like a disk drive.
> Until I got it working, I referred to it as write-only memory.
>

I have actually worked on machines that had write-only memory, typically
memory-mapped hardware (the addresses corresponding to "out" ports of
some sort, of course). Aside from the curiousity value, it's really not
that different from regular memory -- you won't use it to store
temporaries or read back what you've written, after all.

S.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-07-2005
Keith Thompson wrote:

> const double my_pi = 3.14159265358979323848264;


Where the hell did you get the idea that the next digit after
3.1415926535897932384 was 8?!!!



--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Good idea or gimmick: Go-style OO-programming in C++ ? jeti789@web.de C++ 52 04-06-2013 03:30 AM
Good programming style Astley Le Jasper Python 5 09-15-2008 02:41 PM
good style guides for python-style documentation ? Fredrik Lundh Python 4 04-07-2006 06:19 AM
Could you tell me if this is good meta programming style? Vincent Foley Ruby 2 04-29-2005 10:31 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM



Advertisments