Experiment With C [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
#include<stdio.h>
int main() {
int x = 99;
printf("%d %d\n",1 > scanf("%d",&x) ? scanf("%d",&x): scanf("%d",&x),x);
}
So What is happening to the scanned values.
Lets say First Input is 11. so left most scanf is returning 1. So according to the rule the right most scanf will be executed. But The right most %d of printf is not printing the scanned value. It is showing 99.

You have unidefined behavior.
That is because you are modifying a value and accessing that value without an intervening sequence point. A simplified example with the same problem:
int x, y;
x = 1;
y = scanf("%d", &x) + x; /* <--- UB! */
Or also:
int x = 1;
printf("%d %d", x = 11, x); /* <--- UB! */
Note that while the ?: does insert a sequence point just after the condition, the comma that separates arguments in a function call does not. This measn, not only that the order of evaluation of function arguments is unspecified, but also that you must not modify the same value twice in the arguments of a function, or modify and use the same argument.
For example this other example is also UB:
int x = 0;
printf("%d %d", x++, x); /* <--- UB! */
Or this one:
int x[10] = {0}, i = 0;
printf("%d %d", x[i++], x[i++]); /* <--- UB! */
The solution for your problem is to separate the code in nice orderly sentences. A full sentence always ends with a sequence point!
int x = 99;
int y = 1 > scanf("%d",&x) ? scanf("%d",&x): scanf("%d",&x); /* ARGH! */
printf("%d %d\n", y, x);
Now, the line marked with the exclamation is quite nonsensical, but at least it is UB-free.

Function arguments may be evaluated in any order. In this case, it looks like x evaluates to 99 before scanf is called. The order of function argument evaluation is unspecified in C. This is C FAQ #3.7:
The comma operator does guarantee left-to-right evaluation, but the commas separating the arguments in a function call are not comma operators. [...] The order of evaluation of the arguments to a function call is unspecified.
Because there is no sequence point between the scanf that modifies x and the function argument that evaluates it, you are invoking undefined behavior, as explained in rodrigo's answer.

Related

How does it works? Sequence of computing variable into printf [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 2 years ago.
stumbled upon such a puzzle:
What will be shown on the screen?
#include <stdio.h>
void main()
{
int x = 10;
printf("x = %d, y = %d", x--, x++);
}
Curiously enough, but shown at the screen this: x = 11, y = 10;
But how??
Argument Evaluation Order is undefined in both C and C++. It's important to avoid any code that passes expressions dependent on each other that has to be evaluated before the function is called. It's a strict no.
int f1() { printf("F1") ; return 1;}
int f2() { printf("F2" ) ; return 1;}
printf("%d%d", f1(), f2()) ;
You can check out by adding several functions that contain a print statement and pass it to a function to observe this in action. You don't know what's coming, the C standard doesn't specify it, it depends on what compiler you use and how it optimizes your code.

Write a function receives two arguments and returns the difference [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Write a C-program that calls a function minus(). This function receives two arguments and returns the difference (regular subtraction, not absolute). This difference should be printed on screen.
My code is:
int minus(int a,int b)
{
int c = a - b;
return c;
}
int main()
{
int a = 4; int b = 5;
minus(a,b);
printf("%d", minus);
return 0;
}
I have two questions:
1.why a and b in
int minus(int a,int b)
are grey in Visual Studio? "int" is blue but a and b are grey.
2. I got this result but it should be -1.
Could someone help me please
a and b are grey because the editor is automatically coloring the code to help illustrate the program syntax. This may look funny at first, but, over time, your brain will become accustomed to it, and things that are the wrong colors will stand out. This will help you find mistakes in your program—when you make a mistake typing, something that should be a keyword will be colored like a parameter name, and you may notice it is the wrong color and take a closer look at what you typed.
In printf("%d", minus);, the minus is just the function. It is not the value returned by the function. To print the value returned by the function, use printf("%d", minus(a, b));.
You have undefined behavior here printing the function pointer using %d format specifier. (You have used the wrong format specifier that's why the Undefined behavior).
And the most probable way you would like is to printf("%d", minus(a,b));. You wanted to print the result of the subtraction not the function pointer itself.
You are missing an assignment
int minus(int a,int b)
{
int c = a - b;
return c;
}
int main()
{
int a = 4; int b = 5;
int d = minus(a,b);
printf("%d", d);
return 0;
}
you called the function minus() but you did not take the value returned by function in any variable so when you try to print minus then it will return the pointer value of function. so to get the correct answer hold the return value in variable and then print it int c = minus(a,b);
printf("%d", c); or you can call the minus function inside the print function like this printf("%d", minus()); as a beginner i will suggest you to implement the first suggestion it will increase the capacity to use statements in c.

C - Output explanation of printf("%d %d\n",k=1,k=3); [duplicate]

This question already has answers here:
Explain the order of evaluation in printf [duplicate]
(5 answers)
Closed 6 years ago.
How to explain the output of the below code:
include <stdio.h>
int main(void) {
int k;
printf("%d %d\n",k=1,k=3);
return 0;
}
Ideone Link
My thinking was that 1 will be assigned to k variable and then 1 would be printed. Similarly 3 will be assigned to k and output will be 3.
Expected Output
1 3
Actual Output
1 1
I am extrapolating from
int a;
if (a = 3) {
...
}
is equal to
if (3) {
...
}
Please let me know where am I going wrong?
The problem is, the order of evaluation of function arguments are not defined, and there's no sequence point between the evaluation or arguments. So, this statement
printf("%d %d\n",k=1,k=3)
invokes undefined behavior, as you're trying to modify the same variable more than once without a sequence point in between.
Once a program invoking UB is run and (if) there's an output, it cannot be justified anyway, the output can be anything.
I expect the reason you're seeing 1 1 is because the two assignment statements are happening control is passed to printf.
printf("%d %d\n",k=1,k=3);
So in response to the down-votes, yes, this this is undefined behavior, and therefore you shouldn't count on this behavior continuing. However, in terms of identifying why the output was 1 1 and not 1 3, we could infer that the assignment of 3 may have been stomped on by a subsequent assignment of 1.
When printf is invoked, the call stack contains two entries containing the final value of k, which is 1.
You can test this out by replacing those with a function that prints something when it executes.
Sample Code:
#include <stdio.h>
int test(int n) {
printf("Test(%d)\n", n);
return n;
}
int main(void) {
int k;
printf("%d %d\n",k=test(1), k=test(3));
return 0;
}
Output:
Test(3)
Test(1)
1 1

post increment about an expression [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was asked the output of following expression :-
I=10;
I++;
I++;
I++;
What will be the value of I at the end of this code. As per my knowledge, post increment in c means that first perform any other task like assignment ,printf etc and after it incteement the value of variable. Similarly in this case , first I should be 10, then I should be 10 and then it would be 11. But final answer came out to be 10 . how ?
The awnser should be 13.
int i = 10;
i++;
i++;
i++;
printf("%d", i);
test it urself
It would have been a nine-line program to demonstrate what happens to I in your question. In an expression containing I++;, I is used then incremented. In your example, since I is not used in any statement, you might have well used ++I to pre-increment it. But if a statement uses or tests I more than once, or contains a part that might not be executed, you must increment I afterwards.
#include<stdio.h>
int main() {
int I = 10;
I++;
I++;
I++;
printf ("%d\n", I); // prints 13
if (1 || I++) // I++ is not executed
printf ("%d\n", I); // prints 13
if (1 && I++) // I++ is executed
printf ("%d\n", I); // prints 14
return 0;
}
lets create an int i = 10;
i++;
is essentially the same as:
i = i + 1;
There is a slight difference though:
If you printf("%d", i++);
the printed value will be 10 since a ++ postfix will increment the value only after the value is used and i will equal 11 only on the next line, when:
printf("%d", i + 1);
will print 11 since it will be calculated before printf runs and i it self wont be changed since we didn't assign a value to it, we only used it to calculate a new value.
If you want behavior exactly the same as i = i + 1; you can use a ++ prefix like:
printf("%d", ++i);
in which case 11 will be printed and the value of i will increment by 1.
In your code you increment i using a ++ postfix 3 times without actually using i so all the code dose is increments i by one, there times. So at the end of the code i is equal to 13.
You can find more information on operators here:
http://www.tutorialspoint.com/cprogramming/c_operators.htm

Evaluation order of increment operations in c [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 5 years ago.
I executed following following code.
int main(void)
{
int c;
c=0;
printf("%d..%d..%d \n", c,c,c);
c=0;
printf("%d..%d..%d \n", c,c,c++);
c=0;
printf("%d..%d..%d \n", c,c++,c);
c=0;
printf("%d..%d..%d \n", c++,c,c);
return 0;}
I expected the output as
0..0..0
1..1..0
0..1..0
0..0..0
But the output(compiled with gcc) was
0..0..0
1..1..0
1..0..1
0..1..1
What is wrong with my expectation?
In gcc ,evaluation order is from right to left. Is It?
What is wrong with my expectation?
Evaluation order of function parameters is not specified - it is left up to the implementation. Moreover, there is no sequence point * between the parameters, so using a modified parameter again before a sequence point cannot give you a predictable result: it is undefined behavior (thanks, Eric, for providing a reference to the standard).
If you want a particular evaluation order, you need to evaluate your parameters as full expressions (that forces a sequence point between each of them):
int arg1 = c++;
int arg2 = c;
int arg3 = c;
// This: printf("%d..%d..%d \n", c++,c,c);
// Becomes this:
printf("%d..%d..%d \n", arg1, arg2, arg3);
* Sequence point is a fancy name for a place in code after which you can count on side effects, such as increments or decrements, of all preceding expressions to have been applied.

Resources