开发者

Why do C languages require parens around a simple condition in an if statement?

It sounds stupid, but over the years I haven't been able to come up with a use case that would require this. A quick google search didn't reveal anything worthwhil开发者_运维知识库e.

From memory there was a use case mentioned by Bjarne Stroustrup but i can't find a reference to it.

So why can't you have this in C languages:

int val = 0;
if val
  doSomehing();
else
  doSomehinglse();

I can accept the "we couldn't be bothered adding support to lexer" reason, I just want to figure out if this syntax breaks other language constructs. Considering how many whacky syntax features there are in C/C++, i hardly think this would have added much complexity.


If there are no brackets around expressions in if constructs, what would be the meaning of the following statement?

if x * x * b = NULL;

Is it

if (x*x)
    (*b) = NULL;

or is it

if (x)
    (*x) * b = NULL;

(of course these are silly examples and don't even work for obvious reasons but you get the point)

TLDR: Brackets are required in C to remove even the possibility of any syntactic ambiguity.


Tell me how to interprit the following:

if x ++ b;

Looks silly but...

if( x ) ++b;

or

if( x++ ) b;

or perhaps "x" has an overloaded operator then...

if( x ++ b){;}

Edit:

As Martin York noted "++" has a higher precedence which is why I've had to add the operator overloading tidbit. He's absolutely right when dealing with modern compilers up until you allow for all that overloading goodness which comes with C++.


I think a better question would be "why would we want such a thing?" than "why don't we have it?". It introduces another otherwise unnecessary edge case into the lexer, just so you can avoid typing 4 extra characters; and adds complexity to the spec by allowing exceptions to a simple syntax that already supports all possible cases just fine. Not to mention toes the line of ambiguity, which in a programming language doesn't have much value.


One other possible thing to keep in mind: C was created at a time when tape storage was common, so random seek or going backwards through the current file or even other files was not really feasible (which also explains why you have to put some stuff (i.e. forward declarations) before other stuff (i.e. usage of functions) even though the compiler should be able to figure it out on it's own).


It's simply a means of ending the condition clause of the statement. Other languages use other means such as a "then" or in Python "if condition :(Followed by a return)" (The : always get me in python I keep leaving them out and get errors than aren't immediately obvious as a result.)

Computer grammars typically like some unique mechanizism to identify the different parts of a statement or expression. It's no better or worst in this respect than any other language. The dangle else issue with C/C++ is an example of ambiguity from a gramatic standpoint and has to be handled as a special case to insure correct operation. (i.e. It adds extra work for the compiler implementor.)


i would love that syntax too, but to avoid parsing ambiguties, must be of this form:

for multiple statements:

if val 
{ 
    doSomething();
    doOthers();
}

for one statement:

if val: 
   doSomething();


unless you want significant white space you need to be able to tell when the condition ends in some languages it's () around the condition in some it's the keyword then an if like the one given in the answer

if x * x * b = NULL could be one condition always evaluating to false, one assigning null to b in the case of (xx) the compiler has no way of knowing when the condition ends and the following operation begins. So there needs to be an explicit "back marker" for the compiler to know when the condition ends. In C(++), Java, c# and other languages it's () marking the condition in F# it's the keyword 'then' and in still other languages it's a newline/indentation


The fundamental reason is that C and friends don't have a 'then' keyword so they need another way to delimit the condition-expression. Languages that do, like Pascal and PL/1, don't need the parentheses. Same applies to 'while', where Pascal, PL/1, etc have a 'do'.


It is because it helps programmer to make compiler understand in which sequence an expressions( including a sequence of types of operators like arithmetic,logical,assignment,etc) need to be solved in order to get desired output.


I can give at least one example why in C there are paranthesis around conditions,

For example consider the following

while(1) do_something();

The above will call do_something() for ever in an infinite loop

The reason the parenthis are required is because the following would be ambiguous

while 1 ; do_something(); does that mean, evaluate do_something and do nothing in the while loop? or does it mean the same as the code sample above?

I guess that showes one way why in the C syntax parenthis are required around condition evaluations, and by extending the same syntax (for the sake of consitency) it is extended to the if statements.

Discalaimer: I could be completely wrong and the reason behind having the pranthesis could be completely different, but at least I've shown one example of how it can be useful


I think you are confusing "brackets" with "parenthesis"

This is possible in C:

void doSomething(){}
void doSomethingElse(){}

void main() {
    int val = 0;
    if( val ) 
        doSomething();
    else
        doSomethingElse();
}

Not all "C" languages force the parethesis usage and and leave braces as optional. Go ( Issue) programming language does exactly the opposite.

The correct way to write that same sentence in Issue9 would be:

if val {
    doSomething();
} else {
    doSomethingElse();
}

That way the following is not valid:

if x * x * b = NULL; 

It should be one of:

if x*x {
    *b=NULL;
}

or if x { xb=NULL; }

Here the braces remove the ambiguity created by the lack of parenthesis.


I can't think of any specific cases where omitting the parens would actually cause any ambiguity, but there is probably a corner case somewhere.

It might be related to locally declared variables (as in if (bool b = foo())... where without the parentheses, it'd be harder to determine if bool is a type declaration or the the entire condition. In the latter case, b would then be the first token of the if-statements body, rather than part of the condition.


Because C allows one to do :

if (x) { // As opposed to java where you need to do if (x == something)
   // your code
}

As you can see above flexibility gives rise to ambiguity as others have shown.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜