Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 5 > The purpose of u

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



The purpose of u
Moderator:
ANCY

Post Reply
Follow this topic



The purpose of u

r2park posted on 01 Mar 2012 1:31 AM PST
Member
5 Forum Posts

I have seen this expression in the sample code usually used for variables which act as sentinels:

 

uint8 foo = 1u

what is the purpose of the u following the 1?




Re: The purpose of u

Gautam Das posted on 01 Mar 2012 03:13 AM PST
Cypress Employee
742 Forum Posts

Hi r2park,

 

"u" just indicates that it is an unsigned integer.



Re: The purpose of u

r2park posted on 01 Mar 2012 03:23 AM PST
Member
5 Forum Posts

thanks, dasq

That was my best guess at what purpose it served. In the example I posted, I believe the u would not be required because the variable foo was declared as unsigned. Is the u used in that instance for consistency/stylistic reasons?



Re: The purpose of u

Gautam Das posted on 01 Mar 2012 04:07 AM PST
Cypress Employee
742 Forum Posts

According to my understanding, since you are declaring the variable as an unsigned integer, using "u" as a suffix will not make any difference.

In case of arithmetic operation such as multiplication or division with negative numbers, using "u" will typecast the number to unsigned.

For example,

1) -100 / 10 = 0xFFF6 (-10d), which is a negative number (-10)

2) -100 / 10u = 0x198F (6543d). This is because, it will consider 0xFF96 as a positive integer (65430) and divide it by 10.



Re: The purpose of u

ole posted on 09 Mar 2012 12:09 AM PST
Top Contributor
26 Forum Posts

At

uint8 foo = 1u;

the 'u' isn't really required since the data type is already unsigned. This makes mostly a sense for use with the preprocessor, e.g.

#define FOO 1u

to abuse misuse (means you will get an warning from compiler)

or to get implicit a cast, e.g.

#define BAR 1f

for float where BAR is a float type for now.

 






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20