LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2007, 08:36 AM   #1
indian
Member
 
Registered: Aug 2004
Posts: 137

Rep: Reputation: 15
A Small doubt about IF loop ???


I am running this code and the output is coming Hello. I checked with all possible combinations and it prints Hello unless all the variables becomes 0. Boat int and char. Now I am not able to understand what is going on in IF loop ? What is the standard C syntax of it ?

Code:
main()
{
      int a= 0;int b = 20;char x =1;char y =10;
      if(a,b,x,y)
            printf("hello");
}
 
Old 02-26-2007, 08:55 AM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
If you don't have K&R, go get it. It's got a nice explanition of the ',' operator.

Essentially, the ',' operator just evaluates both sides and returns the right side. It's rarely used.
 
Old 02-26-2007, 10:28 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I think you want the logical OR operator, ||
 
Old 02-26-2007, 07:24 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Wow. Talk about deja vu.

My very first program was written in BASIC, ##CLASSIFIED## years ago, and it was eight lines long and took six weeks to write and it had two bugs in it. One of them was this one...

The computer has "accepted" the program, because it "can be compiled," but the program as-written probably does not mean to the computer what you intended it to mean.

Let's look at your program as written and see what it says to the computer.

Code:
int a= 0;int b = 20;char x =1;char y =10;
You are declaring four variables here. Two of them are (probably) four bytes long; the other two are one byte long. All of the values will fit in the space provided. So the statement is "legal." But, what does it mean (to you)?

Code:
 if(a,b,x,y) printf("hello");
This is the nonsensical statement, although no doubt you didn't intend it to be so. An if statement needs to act upon an expression that is either True or False. "True" is nonzero; "False" is zero.

You have used an operator that is legal, but not appropriate to the present situation. It will evaluate, and throw away, the values of 'a', 'b', and 'x', then return the value of 'y', which as we see is '10'. This value is non-zero, so the expression is "True." The "printf()" statement will execute.

But still... what you've asked for, while legal, is nonsensical.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
a small (low priority) doubt about /home and /export/home crisostomo_enrico Solaris / OpenSolaris 6 05-01-2009 02:34 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
3 small problems in 1 small post vzzt! Linux - Software 10 02-27-2003 07:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration