Csound Csound-dev Csound-tekno Search About

[Cs-dev] C89 vs C99 vs C++ questions

Date2005-10-06 22:00
FromIain Duncan
Subject[Cs-dev] C89 vs C99 vs C++ questions
I understand that the following is not cool for pre csound99:

MYFLT tab_val = csoundTableGet( csound, 0, 0 );

But what about

int cs_perfoming = 1;

at the beginning of a function. I was under the impression that this is 
now considered good C style, is this not the case? I had also read that 
it was considered acceptable style to declare variables as above 
immediatelty before use instead of at the beginning of a {} block 
because of the fact that this is normal C++ usage. Is that bad style for 
strict C?

I figured it was better to make my examples in C thinking that any C++ 
coder should be able to convert them easily to C++ but not necessarily 
the reverse would be true. Thoughts?

Thanks
Iain


-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2005-10-06 22:45
FromIstvan Varga
SubjectRe: [Cs-dev] C89 vs C99 vs C++ questions
Iain Duncan wrote:

> I understand that the following is not cool for pre csound99:
> 
> MYFLT tab_val = csoundTableGet( csound, 0, 0 );

It depends on where this particular line is. If there are only
declarations before it, then it is valid C89, that is, the
following code is OK:

{
     int cs_performing = 1;
     MYFLT tab_val = csoundTableGet( csound, 0, 0 );

but this is allowed only in C99 and C++:

{
     int cs_performing = 1;
     printf("The table value is ");    /* not a declaration */
     MYFLT tab_val = csoundTableGet( csound, 0, 0 );
     printf("%f\n", tab_val);

however, this is OK again:

{
     int cs_performing = 1;
     printf("The table value is ");
     {
       MYFLT tab_val = csoundTableGet( csound, 0, 0 );
       printf("%f\n", tab_val);
     }
     /* cannot use tab_val here */

You may try compiling with the options -Wall -ansi -pedantic
to check for such problems. While this may not be guaranteed to
catch all possible errors, it should find many.

> But what about
> 
> int cs_perfoming = 1;
> 
> at the beginning of a function. I was under the impression that this is 
> now considered good C style, is this not the case? I had also read that 
> it was considered acceptable style to declare variables as above 
> immediatelty before use instead of at the beginning of a {} block 
> because of the fact that this is normal C++ usage. Is that bad style for 
> strict C?

The older (C89) standard does not allow that, unless you add an additional
level of braces as shown in the third example above. Of course, for C++
style syntax, you could just make the example C++, even if without actually
using the advanced features of that language.


-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net