Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-26-2007, 09:36 AM   #1
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 ?

      int a= 0;int b = 20;char x =1;char y =10;
Old 02-26-2007, 09:55 AM   #2
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

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, 11:28 AM   #3
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

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

Rep: Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997Reputation: 2997
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.

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)?

 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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 04:49 PM
a small (low priority) doubt about /home and /export/home crisostomo_enrico Solaris / OpenSolaris 6 05-01-2009 03:34 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 04:50 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 06:17 PM
3 small problems in 1 small post vzzt! Linux - Software 10 02-27-2003 08:40 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 AM.

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