Link to home
Start Free TrialLog in
Avatar of sunil_djoshi
sunil_djoshi

asked on

Is this legal in C Language

Hi is this legal in C ?

void foobar()
{
      int a = 10;

      {
            int b;
            b = a + 10;
            printf("a = %d\n", a);
            printf("b = %d\n", b);
      }
}
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dheerajniagm
dheerajniagm

Yes this is valid.

the scope for b is within its braces and for b, a becomes a global. So the output would be
a = 10
b = 20.

 
Try it out yourself (Compile and run)
You will see that it is valid.
Avatar of sunnycoder
Hi sunil_djoshi,

Yes this is legal C ... Do you have some specific query in mind?

I presume it might be due to extra braces in the function .. they are legal ... braces can be used for any block of statements and not just loops and conditions

Cheers!
Sunnycoder
yeah..it's okk to use this syntax
braces are used to identify a set of code which performs a specific task within a function
say if a function adds two integers then there can be code enclosed into the braces which takes user's input
there is nothing wrong in doing so
Avatar of sunil_djoshi

ASKER

Yes I compile and run and it ran fine.
Just curious if adding braces makes any difference.. I mean block declaration is allowed in C.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes.

Your declaration means variable b can only be accessible in the inner block.
Brackets specify the scope.
void foobar()
{
     int a = 10;

     {
          int b;
          b = a + 10;
          printf("a = %d\n", a);
          printf("b = %d\n", b);
     }

     /* OK */
     printf("a = %d\n", a);

     /* NOT OK */
     /*
     printf("b = %d\n", b);
     */
}
Thanks a lot for clearing my doubts.
I really appreciate such a quick and correct help
Yes. C language is a block scope language but it doesn't allow nested function declaration. You can uses a pair of curly bracket to declare a block and these blocks can be nested each other.
Have good time!
Phuoc H. Nguyen
Wow, so many great responses that this one is not needed. =)

Well Sunil, since your question was more one of intellectual curiosity, I think I'll answer it as such.

I think that adding unnecessary (but still logically correct) braces into your code would cause little difference in most situations.  If it were ever to cause a noticeable effect in performance, I'd start by blaming a bad compiler.  I'd guess that most modern compilers are sophisticated enough to see through clutter like that and make "safe" changes that will never effect the code flow.  Presumably a bad compiler would completely duplicate the "outer variable context" completely inside the new one.  Of course in C it seems like variables are always the size of an int or a char and with good use of pointers, duplicating a context is not going to involve copying large memory objects.

So I guess this is all moot since any difference would be nullified by a good compiler and even a really poor compiler (like one I might write) would still produce acceptable code simply because modern hardware is so fast.
SassyZ,

I strongly disagree ...

>I think that adding unnecessary (but still logically correct) braces into your code would cause little difference in most situations.
Is it fairly obvious that braces unlike code have very little impact on performance and that is so because braces are used for logical grouping and not for speeding up the code ...

>So I guess this is all moot since any difference would be nullified by a good compiler
Imagine having to write a function that has similar though not exactly same 50+ loops ... they are not as uncommon as you might like to believe .... Something like

variables
loop ( condition )
start
      manipulate variables
end
take some additional actions based on values in variables

If it were not for braces, you would need loop counter variables and in addition to that several more differently named variables for manipulation inside the loops ... Can you imagine the namespace clutter it would create? Add to it the fact that code evolves with time .... and evolution is almost never through a single developer ...

Simply adding a pair of braces around the logical group makes life whole lot easier for anyone who is maintaining or enhancing such code.

Your variable names do not clash and you can declare them close to where you use them. Code looks cleaner and is easy to maintain and enhance. I would not consider that to be trivial gain especially if it does not affect the performance.

sunnycoder
Sunnycoder,
I strongly Agree...
Not 100% sure but I *think* we're saying similar things here.  I probably should have said "cause little performance difference" and that was a mistake on my part.

>>I think that adding unnecessary (but still logically correct) braces into your code would cause little difference in most situations.
>Is it fairly obvious that braces unlike code have very little impact on performance and that is so because braces are used for logical grouping and not for speeding up the code…

Without getting into a discussion of best-practice programming, I (strongly) agree that using braces should be a key part of producing clean maintainable code.  Even code written solely for yourself should be written in this fashion since (as mentioned) it will group variables close to the statements they get used in and thus increase legibility and possibly even avoid namespace collision.

To me the question seemed very much like something out of a text book and probably only required a true or false response... therefore I considered it essentially answered despite still being open.  My intent in commenting was to accept the original foobar() function as legal C code and then go a step further by asking, "So then what is the difference created by adding those inner braces?"  I suppose this really relates more to the author's second comment.

Of course now we've probably gone too far by tackling the completely new question, "So when should I use these extra braces in my code?"  Personally I think good coding style is so important that it's always worth the occasional pause to stop and discuss it.
Hi Venabili!
I think you should split the points for SassyZ.
Phuoc
For a 50 points question, there can only be 2 split since the minimum per split is 20 points.
hi hongjun,
If didn't pay attion on how many points. Just 50, I think splitting: hongjun {http:#16665143} & sunnycoder {http:#16665210} is ok.
Phuoc