Sample 1 doesn't have any if or case statement.
It is just i = 3 assignment with {} around.
Main Topics
Browse All TopicsThis 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?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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?
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?
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.
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.
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.
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.
Business Accounts
Answer for Membership
by: deightonPosted on 2000-02-15 at 08:57:39ID: 2523606
Nothing in your case?