top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why the output of below program is not as expected?

0 votes
536 views
#include<stdio.h>
main()
{
        int a=10;
        printf("%d\n",a++==a);
}

Output should be 1 but it is coming as 0,
Can anyone please explain?

posted Sep 14, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Mine is coming as 1 (http://codepad.org/CHaTPyJs )
So it may be compiler dependent behavior, if operation is executed from left to right then answer would come as zero else 1.

1 Answer

0 votes

Assume, compiler executes statement a++==a from left to right, then
a = 10
a++ becomes 11
so, a++(10) == a(now becomes 11)

answer Sep 15, 2017 by anonymous
Thanks for the answer,
But I don't think, this left to right explanation is right,
Because if this explanation is correct then,
If a++==a is giving o/p 0 then a==a++ should give o/p as 1
It's about precedence.++ Is higher precedence than ==. I think you got the ans
Similar Questions
+3 votes

input: 1 2 3 4 5
output: 5 4 1 2 3 or 1 2 3 5 4

Can any one tell?

+1 vote

I am running below script on sles 11 and getting different output.

#!/bin/bash
PID=$(/bin/ps aux | grep sro-rest | grep -v grep | awk '{print $2}')
echo "PID = $PID"
. /etc/rc.status
PID=$(/bin/ps aux | grep sro-rest | grep -v grep | awk '{print $2}')
echo "PID = $PID"

Output:

PID = 2453
PID =

After debugging, I get to know that once we run . /etc/rc.status then output of ps aux is getting short.
See below script for example,

#!/bin/bash
echo "Before ==================================> "
/bin/ps aux | grep java | grep -v grep
. /etc/rc.status
echo "After ==================================> "
/bin/ps aux | grep java | grep -v grep

Output :

Before ==================================>
root      2453  4.9  7.8 8496196 615020 ?      Sl   Jun05  60:31 /usr/java/jre1.8.0_77//bin/java -Djava.net.preferIPv4Stack=true -Djava.library.path=/opt/sro/lib -Dserver.port=50000 -Dspring.profiles.active=gal -jar /opt/sro/ui/lib/sro-rest-SNAPSHOT.jar
After ==================================>
root      2453  4.9  7.8 8496196 615020 ?      Sl   Jun05  60:31 /usr/java/jre1.8.0_77//bin/java -Djava.net.preferIPv4Stack=true -Djava.l

Can anyone explain why different behavior for the same command?

0 votes
#include<stdio.h>
int main()
{
   int n;
   for(n = 7; n!=0; n--)
     printf("n = %d", n--);
   getchar();
   return 0;
}
+3 votes

1.

main()
{
printf("%x",-1<<4);
}

2.

main()
{
int i=10;
i=!i>14;
Printf ("i=%d",i);
}
...