extra { } in the middle of nowhere 
Author Message
 extra { } in the middle of nowhere

In ANSI C, of what use are a set of bracketed statements appearing smack
dab in the middle of a function, and not attached to any sort of flow
control?  Why bracket the statements?

Thanking you in advance,

        Carlo Martino



Mon, 08 Jun 1998 03:00:00 GMT  
 extra { } in the middle of nowhere

Quote:

>In ANSI C, of what use are a set of bracketed statements appearing smack
>dab in the middle of a function, and not attached to any sort of flow
>control?  Why bracket the statements?

It introduces a scope where you can declare new variables. e.g. (untested
code):

main()
{
  int i;

  do_something();

  int j;  /* this is illegal */

  do_something_else();

Quote:
}

but

main()
{
  int i;

  do_something();

  {
    int j;  /* this is okay */

    do_something_else();
  }

  /* but you can't use j here */

Quote:
}


Witty .sig under construction.


Mon, 08 Jun 1998 03:00:00 GMT  
 extra { } in the middle of nowhere

Quote:

>In ANSI C, of what use are a set of bracketed statements appearing smack
>dab in the middle of a function, and not attached to any sort of flow
>control?  Why bracket the statements?

It introduces a scope where you can declare new variables. e.g. (untested
code):

main()
{
  int i;

  do_something();

  int j;  /* this is illegal */

  do_something_else();

Quote:
}

but

main()
{
  int i;

  do_something();

  {
    int j;  /* this is okay */

    do_something_else();
  }

  /* but you can't use j here */

Quote:
}


Witty .sig under construction.


Mon, 08 Jun 1998 03:00:00 GMT  
 extra { } in the middle of nowhere
|>In ANSI C, of what use are a set of bracketed statements appearing smack
|>dab in the middle of a function, and not attached to any sort of flow
|>control?  Why bracket the statements?
|>

It allows you to introduce block-scope variables used only by the bracketed
statements.

--

Chris Volpe                             Phone: (518) 387-7766 (Dial Comm 8*833
GE Corporate R&D                    Fax:   (518) 387-6560



Mon, 08 Jun 1998 03:00:00 GMT  
 extra { } in the middle of nowhere

Quote:
>In ANSI C, of what use are a set of bracketed statements appearing smack
>dab in the middle of a function, and not attached to any sort of flow
>control?  Why bracket the statements?

You might want to introduce variables with a limited scope, as in

int foo()
{
    double sum = 0.0;
    /* some things */
    {
        int i; double aux = 1.0;
        for (i=0;  ) {
            aux = aux / i;
            ...
        }
        sum = sum + aux;
    }
--

The joy of engineering is to find a straight line on a double
logarithmic diagram.



Mon, 08 Jun 1998 03:00:00 GMT  
 extra { } in the middle of nowhere

Quote:

> In ANSI C, of what use are a set of bracketed statements appearing smack
> dab in the middle of a function, and not attached to any sort of flow
> control?  Why bracket the statements?

     My guess is that you will see some sort of variable declaration
right after the '{'. The purpose of putting {} without being related
to control is to create a new scope as in C you can only define
variables at the beginning of scope.

--

                   THE WORLD'S FIRST 3D SIGNATURE!!!  To VIEW:
Display THIS signature on 2 580x400 pixel (8 plane) screens whilst crossing
your  eyes  and striking yourself about the head with a billard queue ball.
See it?  Neither did I the first time.  But now I see it all the time......



Sun, 14 Jun 1998 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. end in the middle of nowhere

2. BN_CLICKED notification comes from nowhere!

3. Strange error which appeared out of nowhere.

4. code seems to drop out in the middle of a streamwriter.WriteLine()

5. question on web service accessing middle tier functions

6. Middle-tier caching of static values

7. inseting text into the middle of a file

8. help: adding lines to the middle of an ascii file

9. Insert text in the middle of a file

10. Writing To Middle of File

11. String in the middle ...

12. Using free() in the middle of dynamically allocated space

 

 
Powered by phpBB® Forum Software