Passing parameters evaluation direction in macro - c

As I know in C, passing of actual parameters of a function evaluation starts from rightmost and directed to left.
What is the case for a macro definition with parameter? I made a code to make the sense clear but output confused me...
Here is the code.,
#define parsing(a,b) a*b
int parsefun(int a, int b)
{
return a*b;
}
int main()
{
int i=10;
printf("%d\n",parsing((i++),i));
i=10;
printf("%d\n",parsing(i,(i++)));
i=10;
printf("%d\n",parsefun((i++),i));
i=10;
printf("%d\n",parsefun(i,(i++)));
system("PAUSE");
return 0;
}
This code outputs,
100
100
100
110
I hoped same output for macros as function. But where is the crucial point here?

parsing of actual parameters of a function starts from rightmost and directed to left
I think you mean "evaluation" rather than "parsing". But that's not true, the C standard does not specify an order.
So the behaviour you're getting for the functions is unspecified by the C standard.
I hoped same output for macros as function
Macro arguments are not evaluated, they're simply substituted. So you end up with this:
int i=10;
printf("%d\n", (i++) * i);
i=10;
printf("%d\n", i * (i++));
After which, you're simply seeing undefined behaviour, as explained in this question: Why are these constructs (using ++) undefined behavior?.

Related

Function returns correct value when i use conditional operator or if statement without return statement

#include<stdio.h>
int fact(int);
int main()
{
int a, b;
printf("enter a number : ");
scanf("%d",&a);
b=fact(a);
printf("\n%d",b);
}
int fact(int y )
{
int d=1;
for(int i = 1;i<=y;i++)
d*=i;
d= d>0 ? d : 0;
}
If I remove the last statement , O/P is a+1.
I have checked this with other functions and the function returns correct values if I use if statement or conditional operator.
I want to know why this happens.
Thank You.
6.9.1 Function definitions
...
12 If the } that terminates a function is reached, and the value of the function call is used by
the caller, the behavior is undefined.
Simply put, the behavior you're seeing is purely accidental. There's no good reason why you should get that particular result or any other.
Undefined means that the code is erroneous, but neither the compiler nor the runtime environment are required to handle it in any particular way. What's likely happening in your case is that the register used to return a value from a function is also being used to store the value of d, but that doesn't have to be true. If you change up the code, you may get a different result.

C function variable changing

I have a basic code to learn function call. But I did not understand something in this code. I got confused when I compare to with my answer and expected output.
My code is below:
#include <stdio.h>
void f(int a, int b, double c){
printf("%d \n", a);printf("%d \n", b);printf("%f \n", c);
}
int main(void){
int i = 0, x = 7;
float a = 2.25;
f (x=5, x-7, a);
printf("\n\n");
f (x = 6, x-7, a);
printf("\n\n");
printf("%d %d\n",i, i++ );
printf("%d %d\n",i, ++i );
return 0;
}
At the last 2 printf statements, My answer was as:
0 0
1 1
But the output as:
1 0
2 2
Can you explain why?
It is undefined behavior in C. It may vary as per the execution or many other things. The order of evaluation of function arguments are unspecified. You can never explain the behavior you see by any standard rule. It would have given different result when you run it in front of teacher in a different machine.
Better you write the code which avoids all these sort of ambiguity.
The example which is explicit about this from standard 6.5.2.2p12
In the function call
(*pf[f1()]) (f2(), f3() + f4())
the functions f1, f2, f3, and f4 may be called in any order. All side effects have to be
completed before the function pointed to by pf[f1()] is called.
Same way when you pass the arguments - their evaluation order may vary. And your example of printf is also another such example.
Check the slide from which you got to know about this - must be a slide one Undefined behavior
Can you explain why?
Because the compiler had calculated the arguments from right to left. It was allowed to do it either way, and it finally produced the code which did it like that. You know, it didn't want to die like Buridan's donkey ;-)
You may say that C compilers have no free will. Yes, they don't, but the generated code depends on many different things, such as compiler brand, version, command-line options etc. And C standard doesn't impose any limitations on C compilers in that particular case. So it's officially called "undefined behaviour". Just never do this.

Does a change of value of a global variable by a function affect the order of addition? [duplicate]

This question already has answers here:
subexpressions evaluation order
(2 answers)
Closed 5 years ago.
I have tried the following code and got confused:
#include <stdio.h>
int m=3;
int f(void)
{
m--;
return m;
}
int main()
{
m=f()+m; //as + operator is executed from left to right, it is okay that m=2+2
printf("%d\n", m); //m=4, as expected
m=m+f(); //as + operator is executed from left to right, it should be like m=4+3
printf("%d\n", m); //but m=6
m+= f();
printf("%d\n", m);
return 0;
}
plus (+) operator is commutative, but here it seemed that the order of the summands matters!!! (according to the rule of associativity)
Why m is equal to 6 after the execution of m=m+f();?
It looks like the function call will always get preference!!!!
I am not sure whether it's an undefined behavior. m=m+m-- is an undefined behavior, as far as I know, but here the decrement task has been performed in an indirect manner- by a function.
In the question "subexpressions evaluation order", the answer was that "each of func1 and func2 can, if desired, interact with some shared data without having that data change under it in an unexpected way", but here the value of m has been changed by the function f.
I tried this code later and found another interesting thing. The function evaluation precedence is not the matter here- as this shows
#include <stdio.h>
int m;
int f()
{
m--;
return m;
}
int main()
{
m=4;
int x;
x=m+f()+f(); //if f() is evaluated first, then it should be like m=3+2+2
printf("%d\n",x); // but m=8
return 0;
}
m=m+f(); //as + operator is executed from left to right, it should be like m=4+3
printf("%d\n", m); //but m=6
This is correct because function call has highest precedence over + operator.
You can have a look at tbis
I hope this would help you.

variable 1 = ({statement 1;statement 2;}) construct in C

main()
{
int a=10,b=30,c=0;
if( c =({a+b;b-a;}))
{
printf("%d",c);
}
}
why the construct ({;}) is legal in C and why it returns the last statement value as the result of the expression ( why it work similar to comma operator)?
It is not legal standard C99, but it is a very useful GCC extension, called statement-exprs (a parenthesized brace compound statement ending by some expression).
IIRC, some other compilers support that extension, e.g. Clang/LLVM
Statement expressions are much more useful when they contain control flow changes and side-effects, like:
c = 2*({while (a>0) a--,b--; a+b;});
However, in your particular case, you could just use the comma operator
if (c=(a+b,b-a))
Since a+b does not have any side effect, I guess that an optimizing compiler could handle that as
if (c=b-a)
GCC provides other useful extensions, notably local labels using __label__ and label as values with computed gotos (very useful in threaded interpreters ...). I don't know exactly why they have not been standardized. I wish that they would.
main()
{
int a=10,b=30,c=0;
if( c =({a+b;b-a;}))
{
printf("%d",c);
}
}
Here,{a+b;b-a;} is one scope.In this you have written 2 statements.This is actually treated as
{
c=a+b;
c=b-a;
}
Initially c value is 40 because of a+b. Again c is modified by b-a. To prove this consider following three cases..
(1).
if(c=({(a=a+b);;}))
{
printf("%d\n",c);
printf("%d\n",a);
}
Here o/p is c=40 and a=40;Because at end of scope (i.e) in last statement is dummy (;).
so,c=a+b is o/p.
(2)
if(c=({(a=a+b);b-a;}))
{
printf("%d\n",c);
printf("%d\n",a);
}
Here o/p is c=-10, a=40. Because last statement is b-a. this value is assigned to c.
(3) main()
{
int a=10,b=30,c=0;
if(c=({(a=a+b);0;}))
{
printf("%d\n",c);
printf("%d\n",a);
}
printf("%d\n",c);
}
Here o/p is c=0 only.If is not executed ,Because of last statement is 0;
C follows procedure oriented.And associativity of () is left to right.

Lvalue required error with macro

Why does the following code report an Lvalue required error?? And how can we write a macro that receives an array and the number of elements in the array as arguments and then print out the elements of the array??
#define arr(b) printf("%d",b++);\
printf("%d",b);
int main()
{
arr(5);
}
If you expand the macro, you get the following:
int main()
{
printf("%d",5++);
printf("%d",5);
}
You cannot postincrement the constant 5, so you get an error.
Remember, macros aren't functions. If you want it to act like a function, simply make a function:
void arr(int b) {
printf("%d",b++);
printf("%d",b);
}
Because part of that macro expands to 5++, which is not valid C. Consider using b+1 instead of b++.
The first l in lvalue stands for left.
Only left values can be assigned.
when you write x ++ you mean x = x + 1 (also you get a value from it).
So the problem is it does not make sense to write 5 = 5 + 1
maybe you would like to do this:
int x = 5;
arr(x);

Resources