Exforsys

Online Training

Query on Evaluation

This is a discussion on Query on Evaluation within the C and C++ forums, part of the Programming Talk category; My friend wrote a C program as below: #include <stdio.h> main() { int a=5,b=6,c=...


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

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-05-2007, 03:56 AM
Senior Member
 
Join Date: Apr 2006
Posts: 144
caradoc is on a distinguished road
Query on Evaluation

My friend wrote a C program as below:
#include <stdio.h>
main()
{
int a=5,b=6,c=6;
printf("%d",(c>=b>=a?300:400));
}

I told the program would give a error thinking that comparison given in printf cannot be evaluated. But the output was 400. Can someone explain me how the program get solved.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-06-2007, 07:43 AM
Senior Member
 
Join Date: Apr 2006
Posts: 153
cyrus is on a distinguished road
The program will not give error. It evaluates all expressions taking the precedence and associativity of operators into consideration. In fact you can give even more operations than this. In your program first c is compared to b and then the result of this is compared with a. Since you have assigned c=6=b the first condition of comparison of c and b gets satisfied and so you have 1 placed in it which reduces the expression to
(1>=a?300:400)
Now 1 is less than the value of a which is assigned as 5 and so condition becomes false and so value results in 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
How to find second max sal of employee with the co chandraismoon Oracle Tutorials 7 05-16-2008 04:08 AM
trial version winrunner garima Testing Tutorials 4 07-10-2007 02:11 AM
Performance of Query Ralph MySQL 2 05-08-2007 05:44 PM
SQL QUERY IN REPORTS 6i:Pls help nitinkoshymech Oracle Database 1 11-27-2006 10:34 AM
how to use the same form for query mode and input mode bhaskara avunoori Oracle Apps 1 06-23-2006 07:07 AM


All times are GMT -4. The time now is 06:39 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.