Reputation: 7534
I am learning the basics of C-language and I did not understand how this code is working, It should give an error because I am using an assignment operator instead of using equal to (==) in the if block
#include<stdio.h>
int main()
{
int i=4;
if(i=5){
printf("Yup");
}
else
printf("Nope");
}
Upvotes: 1
Views: 2258
Reputation:
I believe this question can be interpreted in two different ways. The first is the most literal: "Why does a C compiler allow this syntax?" The second is probably more vauge: "Why was C designed to allow such syntax to be legal?"
The answer to the first can be found in The C Programming Language (a highly recommend book if you do not already have it) and comes down too "because the language says so. It's just the way it is defined.
In the book you can refer to Appendix A to find a description of how the grammar is broken down. Specifically A7. Expressions, and A9. Statements.
A9.4 Selection Statements states:
selection-statement:
if ( expression ) statement
if ( expression ) statement else statement
switch ( expression ) statement
Meaning that any valid expression, of which assignment applies, is legal as the 'argument' to the selection with a minor cavet (emphasis is my own):
In both forms of the if statement, the expression, which must have arithmetic or pointer type, is evaluated, including all side effects, and if it compares unequal to 0, the first substatement is executed.
This might seem odd if you are coming from a language like Java, that requires the result of an expression used in a conditional to be expressly 'boolean' in nature, that attempts to lower runtime errors that are the results of typographical issues (i.e. using =
instead of ==
).
As for why C's syntax is like this I am not sure. A quick Google search returns nothing immediately but I offer this conjection (in which I stess I have found nothing to back up my claim and my experience with assembly languages is minimal):
C was designed to be a low level language that mapped closely to assembly level mechanisms; making it easier to implement a compiler for, and to translate assembly to.
In assembly level languages branches are the results of instructions that look at registers and decided to do. The work previously placed in the register is of no concern. Decrementing a counter is not a boolean operation but testing the resulting value in the register is. Allowing a general expression possibly made implementations of C easier to write. The original compiler written by Dennis Ritche simply spat our assembly files that needed to be assembled manually.
Upvotes: 1
Reputation: 141
The expression i=5 evaluates to a non zero value , hence the if() condition turns true.
Upvotes: 2
Reputation: 93162
In C, the assignment operator =
is just that: an operator. You can use it everywhere where an expression is expected,† including in the control expression of an if
statement. Modern compilers typically warn about this, make sure to turn on this warning.
† Except where a constant expression is expected as an expression involving the =
operator is not a constant expression.
Upvotes: 0
Reputation: 225362
While it might not seem intuitive, an assignment is actually a valid expression, with the assigned value being the value of the expression.
So when you see this:
if(i=5){
It is effectively:
if(5){
So why is this behavior allowed? A classic example is that it allows you to call a function, save the return value, and check the return value in one shot:
FILE *fp;
if ((fp = fopen("filename","r")) == NULL) {
perror("fopen failed");
exit(1);
}
// use fp
Here, the return value of fopen
is assigned to fp
, then fp
is checked to see if it is NULL
, i.e. if fopen
failed.
Upvotes: 4
Reputation: 12272
Assignment operator when used inside an if
statement will not give any error..
The assignent i = 5
will take place, and the if
statement will be evaluated according to the result of the expression on the right side of the =
. In this case that is 5
.
Upvotes: 2