Helping C students ...

Writing C code in a style ... that will compile

Page 1 of 1

6 Replies - 594 Views - Last Post: 20 February 2010 - 01:14 AM Rate Topic: -----

#1 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Helping C students ...

Posted 07 February 2010 - 08:54 PM

Recently, a DIC member ... but I can't seem to find the article just now ... added some comments, to my comments, to a C student, mocking my suggestion to keep the variables to be declared at the top of each block.

I do myself prefer the modern C++ style of declaring variables just before use, i.e. with enough scope needed only ...

Many, if not most all, modern C++ compilers will tolerate this ... in a C program ... that they compile.

But I have come across C compilation that doesn't. So when helping students in C ... I try to keep to the below C89 format ...

http://lwn.net/Articles/285585/

... C89 only requires to declare variables at the top of a block ...

i.e. ... unless I forget and revert back, a little, to declaring my var's before use, like I do in C++


I just now did a little research/rechecking to see if my 'working understanding' was on target. Here is just one ref. to be aware of ... to help manage the cross-compatibility issue with students ... so that their C code will compile on most, if not all, presently used C compilers.

http://en.wikipedia.org/wiki/C99

Most C compilers now have support for at least some of the features of C99. However, there has been less support from vendors such as Microsoft and Borland that have mainly focused on C++.

I hope this helps students of C coding ... and gives reasons why to code in a more restricted way than in C++.

Shalom,

David
Be ready
Be encouraged

Is This A Good Question/Topic? 1
  • +

Replies To: Helping C students ...

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Helping C students ...

Posted 07 February 2010 - 10:04 PM

Well I don't know who was mocking you but I myself put declarations at the top of the scope in C because my compiler requires it... perhaps I am a little behind the times in C but I thought that ALL C compilers required declarations to be at the top of the current block.

I did some testing and while some compilers DID allow variables to be declared in-code most still required the declaration at the beginning of the block (including Microsoft VC 2010 -- pretty much the latest compiler out there). THe only true C compiler I tested was Pelles C which did compile the test program with no problems -- so I suppose that some compilers will work and other will not. It is generally best to go with what works across most platforms.

I think that it is probably BETTER to teach what will actually compile. So I think you are probably doing things right.
Was This Post Helpful? 0
  • +
  • -

#3 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Helping C students ...

Posted 07 February 2010 - 11:21 PM

Much depends on the situation, there being advantages and disadvantanges to both declaring variables at the top of a procedure or function, and when they are needed.

It has often been very helpful to have declarations at the top of a routine, as it can reinforce the intent of the routine seen from a self-documenting perspective but is wateful in terms of resources, as stack space is reserved for items that may or may not be used.

On the other hand creating scope between curly braces just to introduce variables ensures variables are only there when needed, bit this approach can make the program look messy and possibly more difficult to maintain.

This post has been edited by Martyn.Rae: 07 February 2010 - 11:21 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_c.user*


Reputation:

Re: Helping C students ...

Posted 08 February 2010 - 12:51 AM

agree with Martyn.Rae
when you have a long source (it is not rare in C++) it is not very comfortable to scan the code when you create a new variable to know have you created it before somewhere above or not
because they have a common scope
but when you have all variables in one place (in the top and every is commented) then you see all variables at once
but for those which are not using everywhere in the function, they can be used locally, like in sortings, swaps or other quick operations
Was This Post Helpful? 0

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Helping C students ...

Posted 08 February 2010 - 05:47 AM

The original thread for this discussion was here:
http://www.dreaminco...wtopic63164.htm

The statement was:
"Note: When using a C compiler, your variable need to all be declared at the top of each function ..."

David W's own post above shows this statement is not the truth, the whole truth and nothing but the truth.

Whether you or don't like the style is not relevant to the discussion at hand.
It's a worthwhile discussion but is not what is being discussed here.

To say that MS2010 is "pretty much the latest compiler out there" is one thing.
To assume that anything MS puts out is standards compliant is quite another thing (and a very very very brave assumption given MS's track history on such things).

Change the statement made by David W to:
The statement was:
"Note: When using some C compilers, your variable declarations need to all be at the top of each function ..."
and I have no problem with it.

However I would suggest another, better, answer would be to get a modern, standards compliant, compiler.

To suggest that all C compilers require "top of block" variable declaration (as the original statement did) is, as I said before, tosh.

To suggest that defensive programming may be served by writing in a certain style to maximise portability across a range of compilers is a fine concept and can be usefully argued for.

But what is nice and what is required should not be confused.

This post has been edited by janotte: 08 February 2010 - 05:47 AM

Was This Post Helpful? 0
  • +
  • -

#6 Guest_c.user*


Reputation:

Re: Helping C students ...

Posted 08 February 2010 - 06:24 PM

there are two standards of C
C89
C99

there was no such feature in C89
and it was added by C99

C89 corresponds to C++ as its subset

if you compile a C89 source in a C++ compiler it will be compiled fine, because C++ permits mixed declarations (declaration and code mixing)

but when you compile the C89 source by a C89+C99 compiler with the C89 flag
it will print you that you broke the rule

[guest@station tmp]$ cat t.c

#include <stdio.h>

int main(void)
{
    int a;

    a = 1;

    int b;

    b = 2;

    return 0;
}
[guest@station tmp]$ gcc -Wall -pedantic -std=c89 t.c -o t
t.c: In function ‘main’:
t.c:10: предупреждение: ISO C90 forbids mixed declarations and code
[guest@station tmp]$ gcc -Wall -pedantic -std=c99 t.c -o t
[guest@station tmp]$



as for the style, I offer to give a distributed assign to few students
every student does his part of this assignment
then, when they will have done, you try to integrate it
then you will see how it matters where they declare
Was This Post Helpful? 0

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Helping C students ...

Posted 20 February 2010 - 01:14 AM

View Postjanotte, on 08 February 2010 - 06:47 AM, said:

The original thread for this discussion was here:
http://www.dreaminco...wtopic63164.htm

The statement was:
"Note: When using a C compiler, your variable need to all be declared at the top of each function ..."

David W's own post above shows this statement is not the truth, the whole truth and nothing but the truth.

Whether you or don't like the style is not relevant to the discussion at hand.
It's a worthwhile discussion but is not what is being discussed here.

To say that MS2010 is "pretty much the latest compiler out there" is one thing.
To assume that anything MS puts out is standards compliant is quite another thing (and a very very very brave assumption given MS's track history on such things).

Change the statement made by David W to:
The statement was:
"Note: When using some C compilers, your variable declarations need to all be at the top of each function ..."
and I have no problem with it.

However I would suggest another, better, answer would be to get a modern, standards compliant, compiler.

To suggest that all C compilers require "top of block" variable declaration (as the original statement did) is, as I said before, tosh.

To suggest that defensive programming may be served by writing in a certain style to maximise portability across a range of compilers is a fine concept and can be usefully argued for.

But what is nice and what is required should not be confused.



I want to thank Janotte for prompting me to update my understanding on this issue ...

This is my updated understanding:

Some C compilers, especially ones that use C89 standards, will not compile C code, unless the variables used are declared (if not declared at a higher scope) at the top of a block where they are used ...
{ 
   declare vars; 

   /* rest of code */...
}



This next snippet is for illustrative purpose only ...

/* top of block example ... see 'j' below ... */
int isSubStr( char* str, char* sstr )
{
    int i;
    if( str[0] == 0  && sstr[0] == 0 ) return 1; /* special case of both empty */
    
    for( i = 0; str[i] != 0; ++i )
    {
        /* for each i, see if sstr is substr of str beginning at i */
        int j;
        for( j = 0; str[i+j] !=0 && sstr[j] !=0; ++j )
        {
            if( str[i+j] != sstr[j] )
                break;
        }
        /* test this comparison ... */
        if( sstr[j] == 0 ) return 1;
    }
    return  0;
}



Formerly, when coding in C, I would have always declared i and j together ... but now, I will be looking for places where it makes, perhaps better sense than in this example, to wait in my C code, to declare variables (at the top of each block) ... before use.

Thank you Janotte,

Shalom,

David
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1