Exforsys

Online Training

Different Output

This is a discussion on Different Output within the C and C++ forums, part of the Programming Talk category; My C program is as below: #include <stdio.h> main() { int x=900,y=400,z=0; if(!...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-26-2007, 10:50 PM
Senior Member
 
Join Date: Apr 2006
Posts: 153
cyrus is on a distinguished road
Different Output

My C program is as below:
#include <stdio.h>
main()
{
int x=900,y=400,z=0;
if(!x>=1000)
y=500;
z=100;
printf("y=%d z=%d",y,z);
}

I thought the output would be
y=500 z=0
But to my surprise the output was
y=400 z=100

Why is it so?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-31-2007, 12:56 AM
Junior Member
 
Join Date: May 2007
Posts: 12
Mukhtar Ahmad is on a distinguished road
#include <stdio.h>
main()
{
int x=900,y=400,z=0;
if(!x>=1000)
y=500;
z=100;
printf("y=%d z=%d",y,z);
}

I thought the output would be
y=500 z=0
But to my surprise the output was
y=400 z=100


It the place where operator precedence and associativity come to play its part. in the if condition ((!x>=1000) !x is evaluated first then ( result of !x>= 1000) eventually resulted as false .
as condition is applied to only one statement i.e. y=500; which will not be processed in the very case.
You have the result as it displayed.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-01-2007, 02:58 PM
Member
 
Join Date: May 2006
Posts: 38
zsk_00 is on a distinguished road
#include <stdio.h>
main()
{
int x=900,y=400,z=0;
if(!x>=1000) // condition is true
y=500; // ok as this is part of if statement and contion is true
z=100; // this will always be executed as its not part of if statement.
printf("y=%d z=%d",y,z);
}

so
y=500 and z=100 is what you should get.

The following will give what you were expecting.

#include <stdio.h>
main()
{
int x=900,y=400,z=0;
if(x>=1000) { // note that ! have been removed
y=500;
z=100;
}
printf("y=%d z=%d",y,z);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-02-2007, 02:04 AM
Senior Member
 
Join Date: Apr 2006
Posts: 144
caradoc is on a distinguished road
The evaluation of the statement
if(!x>=1000)
takes place as follows:
Since the precedence of ! is greater than >= it gets evaluated first and so !x is evaluated first. Since x=900 !x results in 0 and 0>=1000 becomes false and so statement jumps to z=500 and your y value remains the same as 400.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Puzzled on Output Adrian C and C++ 2 05-24-2007 04:30 PM
To get Output Rahulbatra C and C++ 1 05-10-2007 09:23 AM
Control Output caradoc DB2 3 04-09-2007 08:59 AM
To achieve Output Ralph Linux 0 12-12-2006 03:58 AM
C Programming - Managing Input and Output Operations JobHelper Career Advice 0 04-15-2006 08:30 AM


All times are GMT -4. The time now is 12:32 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.