LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-17-2008, 02:33 PM   #1
cleopard
Member
 
Registered: May 2006
Posts: 39

Rep: Reputation: 15
comma operator in C


I was reading a UNIX book and in some sample code, I saw the comma operator used in a 'while' statement, like this:

while (gets(s), !feof(stdin))
{
if ((num = write(fd, s, strlen(s))) == -1)
<do something>
else
<do something else>
}

I googled for examples, but could only find examples with the 'for' statement, which I already knew about. In the Kernighan and Ritchie C book, it says that things are evaluated left to right, and the value of the left one is discarded. Maybe someone could clear up for me why this kind of expression in a 'while' statement would be useful.
 
Old 04-17-2008, 03:03 PM   #2
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
Hi,

Personally I never use it but from what I remember comma operator let's you evaluate more than one expression and return the value of the rightmost. Therefore in your case both - fgets and feof will be evaluated and the return value of feof will be passed as a parameter to while.

Chris

Last edited by krizzz; 04-21-2008 at 08:51 AM.
 
Old 04-17-2008, 04:48 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
In the example that you gave what goes on is that the file is read and the value is placed into the variable 's'. The variable 's' can now be used within the body of the loop. However, if this is the end of file character you don't want to process it hence the second check in the while loop.

The example code given highlights the use of the comma operator but since there can be two error conditions from gets(), one checked by feof() (end of file) and the other checked by ferror() (file error) you should be wary of using this code as is.
 
Old 04-17-2008, 11:32 PM   #4
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Smile

hai

comma operator can also be used in while loop statement.
in the case above, the while loop iterates only if both the expressions evaluate to non zero value. That means, it will behave like "&&" operator. Using comma is a terminology used by some people. but it will effect the ease of understanding the code to naive persons some times. So even comma operator is allowed, better to avoid its usage.

regards
 
Old 04-18-2008, 08:00 AM   #5
cleopard
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
I appreciate the input, folks. I was reading through some documentation from a website and found that bit of code among the author's samples. I hadn't seen a condition like that before in a 'while' loop and thought I'd come here and get a few opinions. I seriously doubt I'll use it myself.
 
Old 04-18-2008, 03:17 PM   #6
marquardl
Member
 
Registered: Apr 2008
Posts: 100

Rep: Reputation: 15
for loops

Using commas is more common within for loops:

Code:
for (i = 0, j = 22; i < 2000; i++, j += 22) {
   // do something
}
The example values are not very meaningful, but it shows the concept.

GWN

Last edited by marquardl; 05-01-2008 at 03:55 AM.
 
Old 04-18-2008, 04:32 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by chakka.lokesh View Post
in the case above, the while loop iterates only if both the expressions evaluate to non zero value. That means, it will behave like "&&" operator.
This is completely wrong. The comma operator will execute both statements unconditionally (in left-to-right order), and only the last statement will be checked for truth.

Note that this is different from using a comma as the argument separator in a function call. In that scenario, the order of evaluation of the parameters is unspecified, but in this case, the operator creates a sequence point so it is always left-to-right.
 
Old 04-19-2008, 07:09 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
it's generally only used as marquardl said.
it's not really an ultra useful thing.

it's (i think) simply a language construct to put multiple expressions into a place that expects one.
so you can do for(i=2,j=3



Comma operator[125]==

expression:
assignment_expression /
expression ',' assignment_expression
 
Old 04-19-2008, 07:26 PM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
I'm concerned that the UNIX book is actually using gets. If it doesn't tell you how dangerous that is, you need to read a newer book.
 
Old 04-21-2008, 09:17 AM   #10
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
Quote:
I'm concerned that the UNIX book is actually using gets. If it doesn't tell you how dangerous that is, you need to read a newer book.
In my opinion, fgets itself - like many other C functions is not dangerous at all. The danger is between chair and the keyboard The function works as described in the manual... However I agree that authors of popular programming books should be more concerned about security aspects of C programming. Know your tools I'd say. The beauty of C programming is that it gives you a freedom of screwing everything up as well as building a cool and efficient applications.

Chris
 
Old 04-21-2008, 04:39 PM   #11
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by krizzz View Post
In my opinion, fgets itself - like many other C functions is not dangerous at all.
The issue was with gets(), for which there are around zero ways to use “correctly” (the only safe scenario is if you have special knowledge or control over the program writing to stdin). The gets() function has even been officially “discouraged” by the POSIX standard, which renders any UNIX programming book encouraging its use outdated.

Last edited by osor; 04-21-2008 at 04:41 PM.
 
Old 04-22-2008, 07:44 AM   #12
cleopard
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
I've worked with C for a few years, though never had reason to use 'gets', mainly 'fgets' since the reading I did was normally from files. Out of curiosity, what's been the main problem with 'gets'?
 
Old 04-22-2008, 08:29 AM   #13
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
fgets/gets functions don't stop reading even if they catch \0 character. They were made sensitive to the \n and EOF. Now think about what could happen to your strings if you have some unintended NULL characters in the lines you're reading from the file... Of course, if you know what your doing and \0s are purposely put there and your program can deal with them - then no problem.
 
Old 04-22-2008, 01:19 PM   #14
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by cleopard View Post
Out of curiosity, what's been the main problem with 'gets'?
gets() will read an unlimited amount of data into a limited amount of memory, so you will have an overflow if the amount read exceeds the amount anticipated. This possible security breach happens whenever you have stdin connected to a terminal or an untrusted program. The prototype of fgets(), however, forces you to specify a limit on the amount of input.
 
Old 04-22-2008, 02:23 PM   #15
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
osor is definitely right. That's the main danger of using gets. However there are more commonly used functions that don't check if the data fits in the buffer - like sprintf() or strcat(). That's why it's always safer to use their "counting" equivalents like snprintf and strncat. It these cases though the data is not coming directly form the potentially dangerous source like stream in the gets case and could be already processed. But, you never know ... Or you better know where the data is coming from

Last edited by krizzz; 04-22-2008 at 02:31 PM.
 
  


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
Need a script to remove last comma in a file jgombos Programming 15 01-14-2008 01:30 PM
Comma key doesn't work mengkai Linux - Laptop and Netbook 1 03-05-2004 11:42 PM
Swedish keyboard + GNOME = no comma ziggamon Linux - Software 0 11-28-2003 02:38 PM
Comma-Delimited Website Filenames Apocalypse General 1 11-09-2003 09:05 AM
comma delimited file cdragon Programming 5 06-21-2002 07:55 PM

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

All times are GMT -5. The time now is 07:28 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