Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

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

Posted on 2000-02-15
Medium Priority
288 Views
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
Question by:hoosang
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 3
• 3
• 3
• +3

LVL 18

Expert Comment

ID: 2523606
Nothing in your case?
0

Author Comment

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

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

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

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

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

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

Author Comment

ID: 2527195
I think I got answer from one of the comments.
0

LVL 1

Expert Comment

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

ID: 2527944
0

LVL 1

Accepted Solution

aperdon earned 100 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

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

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

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ouâ€¦
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
###### Suggested Courses
Course of the Month10 days, 3 hours left to enroll

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

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