CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Question about "X = (X > 200) ? 199 :Y ;"

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

Question about "X = (X > 200) ? 199 :Y ;"
PostPosted: Sat Apr 30, 2016 5:39 am     Reply with quote

Hy
I found on net this paragraph:

Code:
static unsigned int8 X;
static unsigned int8 Y;
X = (X > 200) ? 199 :Y ;


I modified to work on CCS.
My questions are:
#1.Can some explain to me how is working this code?
#2.Is faster than: "if(X>200){X=199;}"?
#3.X must be the same as Y?
I tested with:
X = (X > 200) ? 199 :0;
and do the same work.
Thank you in advance.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Sat Apr 30, 2016 7:35 am     Reply with quote

This is standard C. Every C textbook will have it. It's called a 'conditional expression'

You shouldn't have to change it to work with CCS, it'll work as it is.

The statement (test)?expr1:expr2

Evaluates 'test'. If test is true, it returns expr1, if not, it returns expr2.

In some cases, it can be done slightly smaller/faster than an 'if, since there only has to be one set of code to 'write' to the target variable.

So in your example, if x is > 200, the code returns 199, otherwise it returns Y.
This return is then put into X.

The actual syntax used seems slightly 'odd'. I'd have expected (for example), as a way of limiting a return value to 199:

Code:

    x=(y>200)?199:Y;

X will then get set to Y, if Y is less than 200, or 199 otherwise.

Key difference to your #2 code, is it does not change what is in the variable being tested. This may matter, depending on what the code is doing....
amcasi



Joined: 19 Jun 2015
Posts: 21
Location: Banned - Spammer

View user's profile Send private message

Thanks
PostPosted: Mon Dec 17, 2018 8:45 am     Reply with quote

Many many thanks for answering.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group