When does the while loop stop when given only a variable - c

int gcd(int x, int y) {
int t;
while (y) {
t = x;
x = y;
y = t % y;
}
return x;
}
Does it stop when y = 0? I thought the loop stops when y isn't defined.

Yes, it stops when y is 0, what you have
while (y)
is short hand for
while (y != 0)

While loops loop while a variable is not 0.

Related

Can someone explain me why I am getting two different answers for the below codes?

#include <stdio.h>
int main()
{
int x = 2, y = 0;
int m = (y |= 10);
int z = y && m;
printf("%d\n", z);
return 0;
}
Above program gives me output as 1. Below code is giving me output 0 but what is the reason for different outputs here?
#include <stdio.h>
int main()
{
int x = 2, y = 0;
int z = y && (y |= 10);
printf("%d\n", z);
return 0;
}
In
int z = (y |= 10);
y is masked with 10 so set to 10, so y && m is a boolean worth 1 because both y and m are non-zero, assigned to z
Now, in
int z = y && (y |= 10);
y == 0 so && short-circuits, not evaluating the right hand part and not changing the value of y. Therefore, z is set to 0.
Had you used:
int z = y & (y |= 10);
this would have depended on how/in which order the compiler evaluates the operands (implementation defined behaviour to get 0 or 10)
note that && short-circuiting doesn't evaluate the second parameter if the first is zero for a very good reason:
if ((pointer != NULL) && pointer->value == 12) { do_something(); }
this condition checks if the value is 12 but only if the pointer is non-NULL. If the second expression was evaluated first, this could crash.

How to add Loop invariant to limit or bound integers in frama-c? (preconditions to bound integers)

Using frama-c, I need to analyse Euclid's algorithm for computing greatest common divisor between two numbers with Bezozut coefficients. The algorithm takes two positive integers x and y as input and calculates two integers s and t such that
s · x + t · y = gcd(x, y)
When I run the code using
frama-c-gui -val euclidEVA.c
to analyse, I have some unsigned overflow & out of bounds write alarms generated.
///*#
//requires ...
//*/
int main(int x, int y, int* pp, int* qp)
{
int s = 1, t = 0, u = 0, v = 1;
///*#
//loop invariant ...
//*/
while(y > 0)
{
int r = x % y;
int q = x / y;
x = y;
y = r;
int w = u;
u = s - u * q;
s = w;
w = v;
v = t - v * q;
t = w;
}
*pp = s; *qp = t;
return x;
}
What precondition can I add to validate the memory access?
What loop invariant can I add to limit s and t?
What loop invariant can I add to bound u and v?
What precondition can I add for the bounds of x and y at the beginning?
Any comments on the alarms (signed overflow & out of bounds write) generated please?
screenshot of Alarms generated
Thanks.

How does 'if((x) || (y=z))' work?

I don't quite understand how the if statement in this case works. It evaluates the x != 0 statement and when that is not true anymore, it assigns z to y and then breaks the if statement?
int main()
{
int x, y, z, i;
x = 3;
y = 2;
z = 3;
for (i = 0; i < 10; i++) {
if ((x) || (y = z)) {
x--;
z--;
} else {
break;
}
}
printf("%d %d %d", x, y, z);
}
Let's decompose that into smaller bits.
if (x) is the same as if (x != 0). If x != 0, then you know the condition is true, so you don't do the other portion of the if.
If part 1. was false, then y = z assigns z into y and returns the final value of y.
From point 2., we can understand that if (y = z) is equivalent to y = z; if (y != 0)
Thus, from points 1. and 3., we can understand that :
if ((x) || (y = z)) {
doSomething();
}
else {
doSomethingElse();
}
Is the same as :
if (x != 0) {
doSomething();
}
else {
y = z;
if (y != 0) {
doSomething();
}
else {
doSomethingElse();
}
}
It's true it's not particularly readable code though.
No. if ((x) || (y = z)) {
in C-English is basically:
if x is nonzero, evaluate the following code.
if x is zero, set y to z.
if y is nonzero, evaluate the following code.
otherwise, break out of the loop.
If x is zero or y is zero, it breaks out of the loop.
int main()
{
int x = 3;
int y = 2;
int z = 3;
unsigned int i;
for (i = 0; i < 10; i++)
if (x != 0) {
x = x-1;
z = z-1;
}
else {
y = z;
if (y != 0) {
x = x-1;
z = z-1;
}
else {
break;
}
}
}
printf("%d %d %d", x, y, z);
}
In C, there is short-circuiting, so the statement y=z will not be evaluated until x becomes zero.
When x == 0, since z also decrements the same way, z == 0. Hence y will also be zero at that time due to the assignment. The statement y=z also returns y at this point which will be evaluated as a condition, and since that is also 0, the else break will be hit.
Hence I believe the answer should be 0 0 0.
When you use assignment in an if statement, the result of the assignment is returned. so when you write :
if (x = y)
It will be always true unless the value of y is 0, so 0 is returned as the result of assigning and the if statement is not executed.(anything except 0 is considered as true.)
So when you write :
if ( x || (x = y))
The if statement doesn't execute only if x is 0 & y is 0.
Here
if ((x) || (y = z))
there are two condition
one condition is
if ((x)) and another condition is if ((y = z))
if one of them is true then if portion is execute otherwise else condition work
only and only when both condition are false then else execute.

Unreachable statement in c

I am now learning C programming language and try to understand the functions.
The code below does not give error in Visual Studio and returns the first argument sent to that function.
I think it should give an error but it does not.
How does the given code work and how to return the first argument which is 3 in the code?
#include <stdio.h>
#include <stdlib.h>
int hesapla(int x, int y);
void f();
int main()
{
int a, b, sonuyc;
int s;
printf("\n");
printf("sonuc = %d", hesapla(3, 10));
system("pause");
return 0;
}
int hesapla( int x, int y) {
if (x > y )
return y;
if (x == y)
return y + 1;
}
Thank you..
Your function hesalpha does not return anything when x < y , therefore, it exhibits undefined behaviour .
Either you can handle this case with an if else or just return a value as default when both the previous conditions are false .
Toy need to ensure that all pathes explicitly return a value, otherwise you'll get whatever happens to be left in whatever register is used to return values from a function which is non-deterministic.
int hesapla( int x, int y)
{
if (x > y )
return y;
if (x == y)
return y + 1;
return x ;
}
Though for your own sanity when your code becomes more complex, and to avoid the issue altogether I suggest:
int hesapla( int x, int y)
{
int ret = x ;
if (x > y )
{
ret = y;
}
else if(x == y)
{
ret = y + 1;
}
return ret ;
}
For simple functions such as this, it is possible to return the result in a single expression thus:
int hesapla( int x, int y)
{
return (x > y) ? y : (x == y) ? y + 1 : x ;
}
Succinct perhaps, but I am not sure I'd recommend it.
Neither if-statement is true, so hesapla () will return 0, the default return value for int functions.
It's unclear exactly what you're trying to do, but maybe you're after this?
int
hesapla (int x, int y)
{
if (x == y) {
return y + 1;
}
if (x > y) {
return y;
}
return x;
}
Though, this this better...
int
hesapla (int x, int y)
{
if (x == y) {
return x + 1;
}
return min (x, y);
}

Control Instructions in C

I am not understanding for loop statement and expression following it. Please do help me understand.
#include<stdio.h>
int main()
{
int x = 1;
int y = 1;
for( ; y ; printf("%d %d\n",x,y))
y = x++ <= 5;
return 0;
}
And the output I got
2 1
3 1
4 1
5 1
6 1
7 0
y = x++ <= 5; ==> y = (x++ <= 5); ==> first compare x with 5 to check whether x is small then or equals to 5 or not. Result of (x++ <= 5) is either 1, 0 assigned to y,
As x becomes > 5, (x++ <= 5) becomes 0 so y = 0 and condition false and loop break,
Basically the for syntax is:
for(StartCondition; Test; PostLoopOperation) DoWhileTestPasses;
In this case:
StartCondition == None
Test == (y != 0)
PostLoopOperation == do some printing
DoWhileTestPasses == set y to zero if x > 5 otherwise to non-zero THEN increment x.
Which is all rather bad practice because it is confusing.
Would be better written as:
int x=0;
int y=0;
for(y=0; y = (x <= 6); x++)
{
printff("%d %d\n",x,y);
}
return(0);
In y = x++ <= 5;, y stores the value that is output by the condition x++ <= 5 (here x++ is post increment). If the condition is true then y = 1 else y = 0.
for( ; y ; printf("%d %d\n",x,y))
In the for loop you are printing the values of x and y after executing the for loop body.
Initialize your variables:
int x = 1; int y = 1;
There are 3 statements for the for loop: -1. Initialize, 2. Condition, 3. Iteration:increment/decrement
In your case, you did not provide the initialize condition, however, you have the part of condition and incrementation. I do not think your for loop is used in the correct way.
You should swap the part of incrementation with your body like this:
for(; y; y = x++ <= 5;)
printf("%d %d\n", x, y)
First, you check whether the condition is true or not, y is true or not. Then, you print x and y out. Then, the part of incrementation is executed, x++ <= 5 or not. The result is assigned to y. It does so until your condition is false, y == false.
NOTE: For the good programming, you should enclose your body with a curly braces.
similar to this
int x = 1;
for( int y = 1; y!=0 ; )
{
if (x++ <= 5)
{
y = 1;
}
else
{
y = 0;
}
printf("%d %d\n",x,y);
}
Perhaps this slightly transformed (but functionally equal) code will help:
int x = 1;
int y = 1;
while (y) {
y = (x <= 5);
x = x + 1;
printf("%d %d\n", x, y)
}

Resources