This question already has answers here:
What is the "-->" operator in C++?
(29 answers)
Closed 8 years ago.
I was going through K&R C book and got through this --> operator in the precedence table. So, I wondered if there was a similar operator i.e., <-- and wrote the following program:
#include<stdio.h>
void main()
{
int x = 5;
while(0 <-- x)
printf("%d",x);
}
It worked perfectly fine. So why isn't <-- is not considered as an operator? (as it is not in the precedence table!) and what is it's precedence?
--> is not one operator, it is two; (post) decrement and less than. C is whitespace agnostic for the most part, so:
x --> y
/* is the same as */
x-- > y
<-- is the same idea:
x <-- y
/* is the same as */
x < --y
Perhaps you are confusing -> with -->. -> dereferences a pointer to get at a member of the type it refers to.
typedef struct
{
int x;
} foo;
int main(void)
{
foo f = {1};
foo *fp = &f;
printf("%d", fp->x);
return 0;
}
<- is simply not an operator at all.
This is not an operator but two operators: < and --. The code is identical to
#include<stdio.h>
void main()
{
int x = 5;
while(0 < --x)
printf("%d",x);
}
As I see it, that is just a "less than" < operator followed by decreasing the variable x (--). It is not one operator, but two. And -- has precedence over <.
Related
This question already has answers here:
Is a += b more efficient than a = a + b in C?
(7 answers)
Closed 4 months ago.
I am reading K&R The C Programming Language, and in Chapter 2.10 it states:
"If expr1 and expr2 are expressions, then
expr1 op= expr2
is equivalent to
expr1 = (expr1) op (expr2)
except that expr1 is computed only once."
op= is referring to the binary operators you can use with assignment like +=, -= etc. (and in the 2nd line op just means a binary operator like +)
My first minor confusion is that expr1 must be only a variable (an "lvalue")? Or else how can we assign a result to a larger expression? But my main question is what is meant by "expr1 is computed only once"? Would something have been computed twice if we wrote:
expr1 = (expr1) op (expr2)
instead of
expr1 op= expr2
This feels contrived, but consider:
#include <stdio.h>
int x = 0;
int *f(void) { printf("f is called\n"); return &x; }
int
main(int argc, char **argv)
{
printf(" x = %d\n", x);
*f() += 1; /* Calls f once */
printf(" x = %d\n", x);
*f() = *f() + 1; /* Calls f twice */
printf(" x = %d\n", x);
return 0;
}
A less contrived example would be something like:
a[i++] += 1;
This question already has answers here:
Can't Mod Zero?
(9 answers)
Closed 4 years ago.
I get a floating point exception when running this program:
#include <stdio.h>
int check(int n)
{
int ii=0;
int jj=0;
int sum_of_dividors=0;
int sum_of_dividors2=0;
for (ii=0;ii<n;ii++){
for (jj=0;jj<ii;jj++){
if(ii%jj==0)
{
sum_of_dividors=sum_of_dividors+jj;
}
}
if(sum_of_dividors<n || ii<n){
for (jj=0;jj<sum_of_dividors;jj++){
if(sum_of_dividors%jj==0)
{
sum_of_dividors2=sum_of_dividors2+jj;
}
}
if (sum_of_dividors2==ii){
printf("%d and %d,",ii,sum_of_dividors);
}
}
sum_of_dividors=0;
sum_of_dividors2=0;
}
}
int main()
{
int n;
printf("Enter n ");
scanf("%d",&n);
printf("%d",check(n));
}
The code's purpose is to check the closest pair of amicable numbers to the number (n) that the user have entered.
C 6.5.5 [ISO/IEC 9899:2011] states:
The result of the / operator is the quotient from the division of the
first operand by the second; the result of the % operator is the
remainder. In both operations, if the value of the second operand is
zero, the behavior is undefined.
So, modulo by zero invoked undefined behaviour in C.
If you check with GDB, the problem is in the line
if(ii%jj==0)
You Can't Mod Zero?
You cannot mod % by 0 ! It will give undefined behavior !
You should apply for %, apart from the result, exactly the same constraints as /. Therefore, in C and C++, you can't mod by 0. If you want to have a behavior where X % 0 equals X, you'll have to check specifically for X to be equal to 0, and return it.
This question already has answers here:
some error in output in using macro in C
(3 answers)
Closed 9 years ago.
Why doesn't this math work with macros in C?
#include <stdio.h>
#define FOO 6
#define BAR 32
#define MULT FOO * BAR
main() {
int x = 28296;
int y = x / MULT;
printf("%d / %d = %d\n", x, MULT, y);
return 0;
}
The result of this is:
28296 / 192 = 150912
Why isn't it 147? If I set a variable " int mult" equal to MULT, and use the variable in the expression (int y = x / mult) it works as expected.
#define tells the preprocessor to replace the code before compilation, so your line actually says:
int y = x / 6 * 32;
since * and / operators have the same precedence, and are evaluated from left to right, you get (x/6) * 32. The compiler would probably do this calculation for you since x is known to it.
Instead, use parenthesis when defining macros like this
Put a bracket around the macro:
#define MULT (FOO * BAR)
Now, you'll get 147.
The reason getting 150912 is that after macro expansion the expression is equivalent to:
y = 28296 / 6 * 32;
and hence it's evaluated as 28296/6 and then multiplied by32.
As #kevin points out, it's better to put brackets around FOO and BAR as well in general case to avoid surprises like this.
#include<stdio.h>
#define x 4+1
int main()
{
int i;
i = x*x*x;
printf("%d",i);
return 0;
}
I would like to know how the expression is evaluated.
The C preprocessor will literally substitute all instances of x for 4+1, resulting in the following code:
i = 4+1*4+1*4+1;
Since * has precedence over +, this evaluates to:
i = 4+4+4+1;
and i gets the value 13.
You can also use parentheses in definition like this:
#define x (4+1)
then, this evaluates to:
i = (4+1)*(4+1)*(4+1)
the value of i is 125
Please explain me why it behaves differently.
int main() {
int p;
p = (printf("stack"),printf("overflow"));
printf("%d",p);
return 0;
}
This gives the output as stackoverflow8. However , if I remove the paranthesis , then :
p = printf("stack"),printf("overflow"); gives the output as stackoverflow5
The Comma Operator
The comma operator has lower precedence than assignment (it has a lower precedence than any operator for that matter), so if you remove the parentheses the assignment takes place first and the result of the second expression is discarded. So...
int a = 10, b = 20;
int x = (a,b); // x == 20
int y = a,b; // y == 10
// equivalent (in terms of assignment) to
//int y = a;
Note that the third line will cause an error as it is interpreted as a re-declaration of b, i.e.:
int y = a;
int b;
I missed this at first, but it makes sense. It is no different than the initial declaration of a and b, and in this case the comma is not an operator, it is a separator.