LinuxQuestions.org
Help answer threads with 0 replies.
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 07-03-2015, 01:32 PM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
ternary operator


is there any way to parse the following code snippet using a ternary operator format?
Code:
        for (int i = 0;i < 12; i++)
        {
        if (current->chbits[i] != '\0')
            {
            printf("%c", current->chbits[i]);
            }
        else
            {
            break;
            }
        }
 
Old 07-03-2015, 02:57 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by atlantis43 View Post
is there any way to parse the following code snippet using a ternary operator format?
To parse means to break into parts according to some rule or convention. It's difficult to offer advice if you don't stipulate how you want something parsed.

Daniel B. Martin
 
Old 07-03-2015, 04:52 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Assuming you are asking how to rewrite that if construct as a ternary expression, no. Since break does not return a value, it cannot be used in an expression.
 
1 members found this post helpful.
Old 07-03-2015, 05:45 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Although obvious to some, it may have even been helpful to mention the language being used
 
Old 07-03-2015, 07:33 PM   #5
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Although obvious to some, it may have even been helpful to mention the language being used
corrections appreciated.
 
Old 07-04-2015, 04:16 PM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Um, why not just print the characters as a string?
Code:
printf("%s", current->chbits);
fflush(stdout);
There's no need for the for-loop and no need to check for the terminating null-character.

-----

Edit: For some reason, LQ is not accepting my percent sign in the printf() statement above; the format string should be "%s".

Last edited by dwhitney67; 07-05-2015 at 05:25 AM. Reason: Fixed printf() format string to be "�s"
 
1 members found this post helpful.
Old 07-04-2015, 08:56 PM   #7
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
Um, why not just print the characters as a string?
Code:
printf("s", current->chbits);
fflush(stdout);
There's no need for the for-loop and no need to check for the terminating null-character.
Actually, this code was just for viewing what I'll want to go into a Huffman coding table. It'll be changed to something like fprintf()for placement into the Huffile. I'll have to check on fflush(). Not yet familiar with that.

Last edited by atlantis43; 07-04-2015 at 09:01 PM.
 
Old 07-05-2015, 05:22 AM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by atlantis43 View Post
Actually, this code was just for viewing what I'll want to go into a Huffman coding table. It'll be changed to something like fprintf()for placement into the Huffile. I'll have to check on fflush(). Not yet familiar with that.
My format string in my printf() example should have been "s", not just merely "s".

As for the fflush(), it is used to flush the buffer associated with the given stream. Another way to automatically flush the (stdout) buffer is to specify a newline character in the printf() format string (e.g. "%s\n").

Last edited by dwhitney67; 07-05-2015 at 05:26 AM.
 
1 members found this post helpful.
Old 07-05-2015, 08:21 AM   #9
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
My format string in my printf() example should have been "s", not just merely "s".
I presume you meant "should have been "%s", not just merely "s".
 
Old 07-05-2015, 09:47 AM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by atlantis43 View Post
I presume you meant "should have been "%s", not just merely "s".
Yes, I have been having trouble with my keyboard; somehow my system got the notion that it was a "gb" (Great Britain?) style keyboard.
 
Old 07-05-2015, 11:03 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by dwhitney67 View Post
Yes, I have been having trouble with my keyboard; somehow my system got the notion that it was a "gb" (Great Britain?) style keyboard.
Looks more like the LQ % eating bug. A workaround is to use "advanced" mode (both editing and normal posting).
 
  


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
Control flow problem using conditional ternary operator in C Blackened Justice Programming 6 06-25-2012 11:49 PM
[SOLVED] Python equivalent of C ternary operator. pr_deltoid Programming 6 06-12-2010 04:31 PM
Ternary operator for strings in BASH tifkat Programming 3 10-13-2009 07:40 PM
if else vs ternary operator hottdogg Programming 2 01-16-2007 11:21 AM
ternary search nimishabhatia Programming 1 02-11-2005 10:09 AM

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

All times are GMT -5. The time now is 08:56 AM.

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