PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Re: Identifying variables: what process do you follow?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Identifying variables: what process do you follow?

Réponse
 
LinkBack Outils de la discussion
Vieux 20/10/2007, 19h40   #1
Tor Rustad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Identifying variables: what process do you follow?

Kevin Walzer wrote:

> Generally, when developing a program in a scripting language, I follow
> an interative development process, starting with some core
> functionality, adding functions and variables as the program evolves. In
> other words, I don't know in advance what variables I will need to declare.


Neither do I in C, with local variables. We are not supposed to use
global variables anyway.

> With C, such an approach doesn't seem practical. You need to identify
> some variables ahead of time just to have a program that will compile!


Nope, just watch this:

int main(void)
{
return 0;
}

it compile! Now you can build on that, and put in functionality:

void do_task_1(void)
{
}
void do_task_2(void)
{
}
int main(void)
{
do_task_1();
do_task_2();

return 0;
}

and guess what, the above compile too! Note there are still no variables
there. This is the real power in C, by the technique of
split-and-conquer, you get the ability to handle very complex tasks.

Instead of one massive script, you split the C program in a number of
modules (i.e. source files).


> So, I'm wondering what approach seasoned C programmers use for larger
> programs, i.e. ones larger than the simple single-function ones I'm
> learning from my C textbook. Is there a design process that identifies
> such things?


Yes. Sometimes you have a well-designed system to make, all the specs
are there, all the interfaces are set. In such a case, we use a top-down
approach, start with the inter-face, and work my way down to all those
nasty variables.

When prototyping a solution, a more iterative approach may be more
useful, than the top-down. When interfacing HW, I sometimes use
bottom-up, before doing the top level design.

--
Tor <torust [at] online [dot] no>

"I have stopped reading Stephen King novels. Now I just read C code instead"
  Réponse avec citation
Vieux 20/10/2007, 22h23   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Identifying variables: what process do you follow?

Tor Rustad said:

> Kevin Walzer wrote:
>

<snip>
>
>> With C, such an approach doesn't seem practical. You need to identify
>> some variables ahead of time just to have a program that will compile!

>
> Nope, just watch this:
>
> int main(void)
> {
> return 0;
> }
>
> it compile! Now you can build on that, and put in functionality:
>
> void do_task_1(void)
> {
> }
> void do_task_2(void)
> {
> }
> int main(void)
> {
> do_task_1();
> do_task_2();
>
> return 0;
> }
>
> and guess what, the above compile too! Note there are still no variables
> there. This is the real power in C, by the technique of
> split-and-conquer, you get the ability to handle very complex tasks.


<snip>

This is broadly the approach that I was hoping to find time to outline for
the OP. Well, I thought I'd found the time this evening to expand Tor's
approach with a real-world example, and then spent about an hour trying to
think of an example that was (a) complex enough to be a useful
demonstration of functional decomposition and (b) simple enough to be not
too confusing. Alas, I didn't manage to think of anything!

So I'm going to fold. It's an article that does need to be written, but it
isn't going to happen any time soon, at least not from this desk. (Sigh.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Vieux 21/10/2007, 09h03   #3
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Identifying variables: what process do you follow?

Richard Heathfield wrote:
> Tor Rustad said:
>

.... snip ...
>>
>> int main(void) {
>> return 0;
>> }
>>
>> it compile! Now you can build on that, and put in functionality:
>>
>> void do_task_1(void) {
>> }
>> void do_task_2(void) {
>> }
>> int main(void) {
>> do_task_1();
>> do_task_2();
>> return 0;
>> }
>>
>> and guess what, the above compile too! Note there are still no
>> variables there. This is the real power in C, by the technique
>> of split-and-conquer, you get the ability to handle very complex
>> tasks.

>
> <snip>
>
> This is broadly the approach that I was hoping to find time to
> outline for the OP. Well, I thought I'd found the time this
> evening to expand Tor's approach with a real-world example, and
> then spent about an hour trying to think of an example that was
> (a) complex enough to be a useful demonstration of functional
> decomposition and (b) simple enough to be not too confusing.
> Alas, I didn't manage to think of anything!
>
> So I'm going to fold. It's an article that does need to be
> written, but it isn't going to happen any time soon, at least
> not from this desk. (Sigh.)


This is basically top-down programming, and what I generally
recommend. I suggest that the example will be clearer if you
develop a general purpose filter (stdin to stdout). This will
require sections for interpreting commands, via the command line
options, giving , and processing a complete file. The action
on the file can be confined to a function, possibly on a per line
basis.

At any rate the result is a compilable 65 line program that I keep
about for modification to the desired filter:

/* Generic filter program with redirection detection 2004-04-04 */

#include <stdio.h>
#include <stdlib.h>

/* ------------------- */

/* This is very likely to be non-portable */
/* DOES NOT check fp open for reading */
/* NULL fp is considered a keyboard here! */
static int akeyboard(FILE *fp)
{
#ifndef __TURBOC__ /* Turbo C is peculiar */
# ifdef __STDC__
/* This dirty operation allows gcc -ansi -pedantic */
extern int fileno(FILE *fp);
extern int isatty(int fn);
# endif
#endif
return ((fp != NULL) && isatty(fileno(fp)));
} /* akeyboard */

/* ------------------- */

void (void)
{
puts("Got here because no input redirection detected");
} /* */

/* ------------------- */

int initialize(int argc, char *argv[])
{
if (akeyboard(stdin)) {
();
return 0;
}
return 1;
} /* initialize */

/* ------------------- */

void cleanup(void)
{
} /* cleanup */

/* ------------------- */

int filter(FILE *in, FILE *out)
{
puts("Input is redirected, carry-on");
return 0;
} /* filter */

/* ------------------- */

int main(int argc, char *argv[])
{
if (!initialize(argc, argv)) return EXIT_FAILURE;
else {
(void)filter(stdin, stdout);
cleanup();
}
return 0;
} /* main */

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 21h25.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10376 seconds with 11 queries