473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Unexpected result of condition statement

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int a=10,b=10,c=10;
  5. if(a==b==c)
  6. printf("hi...");
  7. else
  8. printf("hello...");
  9. getch();
  10. return 0;
  11. }
  12.  
this is my code..why am i getting hello...as the output?

please help..

thank you.
Ayan Dasgupta
May 21 '11 #1
3 1477
whodgson
542 512MB
You have omitted the format specifiers....i think
May 21 '11 #2
donbock
2,426 Expert 2GB
a==b==c will be accomplished as either (a==b)==c or a==(b==c). I don't know which, and I don't care. You shouldn't care either. It is much less work to use parentheses than it is to accurately remember such arcana.

A boolean expression is either true or false. In C, true is represented by the integer value 1 and false is represented by the integer value 0.

Expand|Select|Wrap|Line Numbers
  1. a==b==c
  2. (a==b)==c       // or it might be a==(b==c)
  3. (10==10)==10    // replace variables with their values
  4. 1==10           // because parenthesized expression is true
  5. 0               // because final expression is false
If your intent is to print "hi" if (a==b and a==c) then that's what you have to say in your code:
Expand|Select|Wrap|Line Numbers
  1. if ((a==b) && (a==c))
Aside to whodgson: those string arguments to printf are by definition the format strings. They just don't happen to contain any conversion specifiers.
May 21 '11 #3
whodgson
542 512MB
yes donbock i now realize that what I said above was not relevant to the problem. It just looked weird compared to what my books show.
May 22 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike Pemberton | last post by:
I'm sure there's a good explanation for this effect, but I get rather a strange output from this little test: #include <iostream> #include <list> int main() { std::list<int> int_list;
8
by: John J | last post by:
Thankyou to those people in this newsgroup that have answered my questions and helped me to learn whilst developing my yacht race classes. I have now completed the three classes (Yacht, Race and...
16
by: Sunil Varma | last post by:
Hello, Please see the following code. int main () { double da = 0.01, db; db = foo (da); } double foo (double a)
62
by: ashu | last post by:
hi look at this code include <stdio.h> int main(void) { int i,j=2; i=j++ * ++j * j++; printf("%d %d",i,j); return 0;
3
by: Don | last post by:
I've added a Setup and Deployment project to my Visual Studio 2005 Windows Application solution and have several OCX and DLL files that will be installed. But I can't seem to find any examples in...
4
by: alacrite | last post by:
#include <iostream> #include <vector> using namespace std; int binarySearch(vector<int>, int, int, int); int main() { vector<intarrInt;
3
by: G | last post by:
Hi ,you guys: look at the code below //////////////////////////////////////////////////////////////////////////////////////////////// #include<iostream> using namespace std; int main() {
5
by: =?Utf-8?B?anVzdGluc2FyYWNlbm8=?= | last post by:
I'm using .net 2.0 and am having a problem adding doubles: double x = 63881.97 + 34297.98; The result of this addition in Visual Studio is 98179.950000000012 This is obviously the incorrect...
11
by: Peter Holschbach | last post by:
Hi, I've the following line of code: result = DateTime.ParseExact("1999-12-01T23:59:59Z", "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture); where I get in result "result" "02.12.1999...
10
by: nisp | last post by:
Hi all ! I'm trying to capture stderr of an external module I use in my python program. I'm doing this by setting up a class in my module overwriting the stderr file object method write. The...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.