This question already has answers here:
Is scanf("%d%d", &x, &x) well defined?
(3 answers)
Closed 2 years ago.
#include <stdio.h>
int main(void)
{
char b[5];
scanf("%4s%4s", b, b);
//My input: "qwer<Enter>sgsh<Enter>"
printf("%s", b);
//Output: sgsh
}
C99:
Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression.
In this case, I am modifying the value of b twice. Isn't it undefined behavior?
From this scanf reference:
There is a sequence point after the action of each conversion specifier; this permits storing multiple fields in the same "sink" variable.
So what you're doing is defined and should work well.
Related
This question already has answers here:
What does 1[d=b] mean?
(3 answers)
Closed 5 years ago.
I came across a code as in below
#include <stdlib.h>
int main(){
char a[]="0123456789";
printf("%s\n",a+6[a]-2[a]);
return 0;
}
Output
456789
How does the calculation of a+6[a]-2[a] happens in printf?
Why giving just 6[a] in printf doesn't work?
printf("%s\n",6[a]);
Well, a statement like
a+6[a]-2[a]
can be re-written as
&(a[ a[6] - a[2] ])
which is simply,
use the value of a[6] (type, int) as the index in the first case
use the value of a[2] (type int) as the RHS.
The result, is a pointer, is passed to printf() as an argument to %s conversion specification.
This question already has answers here:
multiple assignment statements in printf in c [duplicate]
(2 answers)
Closed 8 years ago.
Suppose i have the following code:
#include <stdio.h>
main()
{
int a,b,c;
b=1;
c=2;
printf("%d\n",10,b=20,b=30,c=50,c=100);
printf("%d\n",b);
printf("%d\n",c);
}
o/p-10,20,50
how did the value of b became 20,not 30 ..and also the same for c?
The order of evaluation of argument expressions and their pushing on the stack are different things.
The order of evaluation of argument expressions are unspecified in C. So it might be that at first b = 20 will be evaluated and then b = 30 or vice versa.
The order of placing arguments in the stack is the following: the right most argument is placed first.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
For the testing code as follow:
#include <stdio.h>
int addTen(int x, int b[])
{
b[2] = x + b[2];
return b[2];
}
void main(void)
{
int a[3] = {4,5,6};
int i = 2;
printf("%i %i %i \n", a[i], addTen(10,a), a[i]);
}
Why is the output is 16, 16, 6? I know that even if the compiler processes the order from right to left like a[i] <- addTen(10,a) <-a[i]. After calling addTen(10,a), a[i] is already 16 (not 6). So why the output is not 16, 16,16? THANKS!
It's undefined behavior, you should read about sequence points. You're modifying a and simultaneously reading it in a same expression.
In addition the order of evaluating is not defined.
There's no order defined for evaluating arguments. The compiler is free to evaluate arguments in any order, and will normally choose the most convenient order. So, you can't define any expected output.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
When I compile the following code
#include<stdio.h>
#define CUBE(x) (x*x*x)
int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
It gives 27 , 6
But shouldn't the expression a=b++*b++*b++; be calculated as a=3*4*5 and should give 60?
Your expression causes undefined behaviour, so you could get any answer. Trying to modify the same value more than twice between sequence points is bad news.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Sequence points when calling functions in C and undefined/unspecified behaviour
(2 answers)
Closed 9 years ago.
I am decrementing an integer while passing it to a function. But am seeing some unexpected behavior. I'm new to C, and not sure what to make of it:
#include <stdio.h>
void func(int, int);
int main(){
int i=3;
//func(i--, i); //Prints a=3, b=2
func(i, i--); //Prints a=2, b=3 ??
func(i, --i); //Prints a=2, b=2 ??
}
void func(int a, int b){
printf("a=%d\n", a);
printf("b=%d\n", b);
}
The first call to func works as expected, but what is the deal with the second and third calls?
The order of calculation of function arguments is not specified. E.g. GCC likes to calculate the argument values from right to left. When you have an operator that modifies a variable (or have any other side effect), there must be a sequence point between that operator and any other expression that uses that variable.
This is undefined bahavior, you can't expect either result.
Since parameters passes in a function from right, so in first call where you used post increment operator- it passes the same value (3) and update the value for 'i' left parameter (2).
while in second call, you used pre increment operator which changes itself for right parameter and sends updated values for left parameter.