• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

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);
      }
}
0
sunil_djoshi
Asked:
sunil_djoshi
  • 5
  • 3
  • 3
  • +4
2 Solutions
 
hongjunCommented:
Yes
0
 
dheerajniagmCommented:
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.

 
0
 
hongjunCommented:
Try it out yourself (Compile and run)
You will see that it is valid.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sunnycoderCommented:
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
0
 
hiteshgupta1Commented:
yeah..it's okk to use this syntax
0
 
hiteshgupta1Commented:
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
0
 
sunil_djoshiAuthor Commented:
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.

0
 
sunnycoderCommented:
Hi sunil_djoshi,

> I mean block declaration is allowed in C.
Yes this is permitted in C ...

All usual restrictions with respect to a block apply ...
-limiting scope of variables
-sequence points
-permitting declaration of variables at start of block
etc.

Cheers!
Sunnycoder
0
 
hongjunCommented:
Yes.

Your declaration means variable b can only be accessible in the inner block.
Brackets specify the scope.
0
 
hongjunCommented:
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);
     */
}
0
 
sunil_djoshiAuthor Commented:
Thanks a lot for clearing my doubts.
I really appreciate such a quick and correct help
0
 
phuocnhCommented:
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
0
 
SassyZCommented:
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.
0
 
sunnycoderCommented:
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
0
 
SassyZCommented:
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.
0
 
phuocnhCommented:
Hi Venabili!
I think you should split the points for SassyZ.
Phuoc
0
 
hongjunCommented:
For a 50 points question, there can only be 2 split since the minimum per split is 20 points.
0
 
phuocnhCommented:
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now