[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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

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
hoosang
Asked:
hoosang
  • 3
  • 3
  • 3
  • +3
1 Solution
 
deightonCommented:
Nothing in your case?
0
 
hoosangAuthor Commented:
Sample 1 doesn't have any if or case statement.

It is just i = 3 assignment with {} around.
0
 
Ernest022699Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
hoosangAuthor Commented:
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
 
aperdonCommented:
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
 
PacmanCommented:
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
 
deightonCommented:
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
 
hoosangAuthor Commented:
I think I got answer from one of the comments.
0
 
ScottyDawgCommented:
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
 
deightonCommented:
please accept the comment that answere your question as an answer.
0
 
aperdonCommented:
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
 
PacmanCommented:
hoosang,

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

pacman
0
 
Ernest022699Commented:
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
 
aperdonCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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