top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Clarification on behaviour of Logical OR in C?

+2 votes
419 views

I have been given an assignment to explain how the pieces of codes work. However, I have some doubts regarding it.

int i=-3,j=2,k=0,m;
m=++i && ++j && ++k;
printf("%d %d %d %d",i,j,k,m);

This piece of code returns -2 3 1 1, about which I have no confusion, but the problem arises when I try to run the second code:

int i=-3,j=2,k=0,m;
m=++i || ++j && ++k;
printf("%d %d %d %d",i,j,k,m);

I get -2 2 0 1 here, and how is that done? the ++j and ++k must return 3 and 1 respectively, according to the theory. Could anyone please explain me how it is executed?

posted Oct 13, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote
 
Best answer

op1 || op2
in case of || operator first op1 will be checked if it is non zero value op2 value not be checked or executed .
if op1 is zero then only next op2 will be checked .
So in your case op1 is ++j == -2 so op2 instruction will not be executed.
To verify this you can execute following instruction,
int i=-1,j=2,k=0,m;
m=++i || (++j && ++k);
printf("%d %d %d %d",i,j,k,m);

answer Oct 14, 2014 by Bheemappa G
+1 vote

My guess only (not tested)
if a||b and a is true is b is not executed so only ++i is executed and && is having higher precedence then || which means we can write the statement in the following way -
(++i) || (++j && ++k);

Others comment please

answer Oct 13, 2014 by Salil Agrawal
+1 vote

&& has higher precedence then || only in parse tree. But compiler optimizes the code as

if( !++i ) {
   ++j && ++k;
}

So here !++i is equal 0 in our case so, ++j && ++k is not evaluated.

answer Oct 14, 2014 by Arshad Khan
Similar Questions
0 votes

I've noticed the difference in gcc and llvm behaviour with the following code:

$ cat test.c

int main()
{
 for(int i = 0;; ({break;}))
 printf("Hello, worldn");
}

$ clang test.c -pedantic &; ({break;}))
1 warning generated.
Hello, world

$ gcc test.c -std=gnu11 -pedantic &; ({break;}))
test.c:5:21: warning: ISO C forbids braced-groups within expressions [-Wpedantic]
 for(int i = 0;; ({break;}))

So, llvm thinks that this is GNU extension (seems like it really is), but compiles it, and gcc does not, even if the standard is specified as gnu11. Is it a bug?

+2 votes
#include <stdio.h>
#include <stdlib.h>

void makeHeap(int heap[],int,int);
int delete(int heap[]);
int no;

int main()
{
    int heap[10],i,num,temp[10],n;

    printf("Enter the number\n");
    scanf("%d",&no);

    n=no;
    for(i=0;i<no;i++)
    {
        scanf("%d",&num);
        makeHeap(heap,num,i);
    }
    printf("The heap element are\n");

    for(i=0;i<no;i++)
        printf("%d\t",heap[i]);

    for(i=n-1;i>=1;i--)
       temp[i]=delete(heap);

    printf("\nThe sorted elements are\n");

    for(i=0;i<n;i++)
       printf("%d\t\t",temp[i]);

    return 0;
}
void makeHeap(int heap[],int data,int index)
{
    int parent,temp;
    heap[index]=data;
    while(index!=0)
    {
        parent=(index-1)/2;
        if(heap[parent]>heap[index])
        {
            temp=heap[parent];
            heap[parent]=heap[index];
            heap[index]=temp;
        }

        index=parent;

    }

}

int delete(int heap[])
{
    int i,left,min,m,c;
    int temp;
    int value=heap[0];

    heap[0]=heap[no-1];
    no--;
    i=0;

    //Problem is  coming in this while loop please help 
    while(i<no)
    {
        left=2*i+1;

        if(heap[left]<heap[i]&&left<=no)
        {
            min=left;
            temp=heap[left];
            heap[left]=heap[i];
            heap[i]=temp;

        }
        else
           min=i;

        if((heap[left+1]<heap[i])&&((left+1)<=no))
        {
           min=left+1;
            temp=heap[left+1];
            heap[left+1]=heap[i];
            heap[i]=temp;
        }
        i=min;
    }

    return value;
}
0 votes

I have a recursive function as below,

void func()
{
       if(counter>10)
              return;
       func();
}

Here, I need to come out of the function when counter reaches specific number(10 as per example).
Now, the condition is, I can't take this counter as global or static or can't pass this counter as parameter.

Any suggestion to solve this given above terms.

...