Solved

What is the difference between having {} or not having it?

Posted on 2000-02-15
14
279 Views
Last Modified: 2010-04-15
This is very simple question for you.

I have two sample codes below here:

  Sample 1
  .
  .
  {
    i = 3;
  }
  .
  .

  Sample 2
  .
  .
  i = 3;
  .
  .


  What is the difference between having  blocks and not having blocks?

0
Comment
Question by:hoosang
  • 3
  • 3
  • 3
  • +3
14 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 2523606
Nothing in your case?
0
 

Author Comment

by:hoosang
ID: 2523646
Sample 1 doesn't have any if or case statement.

It is just i = 3 assignment with {} around.
0
 
LVL 1

Expert Comment

by:Ernest022699
ID: 2523848
Following a conditional expression such as
    if (42 > iDA)
with a single line such as
        i = 3;
will cause the value of i to become 3 when iDA is less then 42.  If you want more than one thing to happen as a result of the condition being true, then you must enclose all of the lines you wish to have execute in curley braces (aka curley brackets).  For example,
    if (42 > iDA)
        {
        i = 3;
        sprintf(szTemp,
          "He's %d years old!", iDA);
        printf("Holey smoke!  %s\n",
            szTemp);
        }

If the { } were omitted, then the sprintf and printf would always be invoked.

Does that answer the question?
0
 

Author Comment

by:hoosang
ID: 2524651
I am not talking about {} with any switch or if statement.

Does C-compiler treat differently if I mistakenly put {} for several statements?

For example,

        {
        i = 3;
        sprintf(szTemp,
          "He's %d years old!", iDA);
        printf("Holey smoke!  %s\n",
            szTemp);
        }

If you put {} around several statements like the sample above and compile it, you don't get any error.

If you take out {} from the example above and compile it, you don't get any error, either.

But for the memory management side, does C-compiler behave differently?
Does it generate same exes?


0
 
LVL 1

Expert Comment

by:aperdon
ID: 2525077
There is no difference between the two samples. But however, if you place curly brackets around the statements you can declare new variables. This is the one and only purpose in C.

In C++ the brackets are not needed since you can declare variables at any place within the function.

To answer your question. The code is treated the same, with or without curly brackets. Except in the case variables are declared within the brackets. In that case the variables are added to the stack at entrance and removed from the stack after the end of the block.
0
 
LVL 9

Expert Comment

by:Pacman
ID: 2526234
hoosang,

the difference is the SCOPE of i.

look at this:

int i = 5;

{
   int i = 7;
   printf("%d\n", i);
}

printf(%d\n", i);

the result you will get is:
7
5

that means if you make new brackets, then you have new variables
(to say it in simple words).
0
 
LVL 18

Expert Comment

by:deighton
ID: 2526721
Ther's a limit to { } nesting so unless you reach the limit by using too many nested {  (254 I believe)  then there will be no difference.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:hoosang
ID: 2527195
I think I got answer from one of the comments.
0
 
LVL 1

Expert Comment

by:ScottyDawg
ID: 2527338
By introducing a new set of curly braces you are creating a new stack frame within your function. This can be useful if you want objects to be "cleaned up" before your function ends. More appropriate in C++ though
0
 
LVL 18

Expert Comment

by:deighton
ID: 2527944
please accept the comment that answere your question as an answer.
0
 
LVL 1

Accepted Solution

by:
aperdon earned 50 total points
ID: 2528336
There is no difference between the two samples. But however, if you place curly brackets around the statements you can declare new variables. This is the one and only purpose in C.

In C++ the brackets are not needed since you can declare variables at any place within the function.

To answer your question. The code is treated the same, with or without curly brackets. Except in the case variables are declared within the brackets. In that case the variables are added to the stack at entrance and removed from the stack after the end of the block.
0
 
LVL 9

Expert Comment

by:Pacman
ID: 2531354
hoosang,

you can accept a comment as an answer by clicking on the right part of the title ...

pacman
0
 
LVL 1

Expert Comment

by:Ernest022699
ID: 2533734
No, no, no!  aperdon is incorrect.

Yes, { } will limit the scope of variables; in that aperdon is correct.  What is NOT correct is "... the one and only purpose in C".  The example given for the two variables having different local scope is a good one.  BUT!  The other purpose (delimiting a block of code) is just as important, otherwise how would you cause execution of more than one statement following a condition (other than calling a function, of course)?

Extra curley braces which serve to make your logic more clear to the human reader are unneccessary but harmless.  Some of us use { } after every if just so we can add debug printfs, etc., and so there will not be any danger of forgetting to add them when we change from a single line of code to several.

Extra { } do not cause your executables to be bigger.  Period.  They may, however, serve to make your code easier to read.

I'm sorry I was away for a couple of days and could not respond sooner.
0
 
LVL 1

Expert Comment

by:aperdon
ID: 2536880
Well Ernest, i think you mix context. It is clear that there is no statement like if, while etc before the block. In that case you would be right. But the question is in case of only the curly brackets. And in that case there is only purpose in C and that is to be able to declare new variables.
And ofcourse i now about multiple statements in a block. And for your knowlegde, i always use curly brackets after a conditional statement, even when there is only one statement in the block.
So my answer is completely correct, and i dont agree the conditional statement must be added in the answer, coz this makes it all more complex than it realy is. It is just about ONLY curly brackets.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now