473,748 Members | 7,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suffix allowed on expressions?

Hi!

Are suffixes allowed on expressions?

For example, is the following allowed:

#define SECONDS_PER_YEA R (60 * 60 * 365) UL

I found this in a C test at http://www.embedded.com/2000/0005/0005feat2.htm
,
however, I am not able to compile when using this macro (or just the
expression "a = (60 * 60 * 365) UL;").

Where in the standard does it say that it is allowed/not allowed?

It would also be interesting to hear if there are any faults in the
other questions and answers in the test.

BRs!
Jan 2 '08
41 3055
Richard Heathfield wrote:
dspfun said:
On 2 Jan, 19:07, dspfun <dsp...@hotmail .comwrote:
Hi!

Are suffixes allowed on expressions?

For example, is the following allowed:

#define SECONDS_PER_YEA R (60 * 60 * 365) UL
It should be:
#define SECONDS_PER_YEA R (60 * 60 * 24 * 365) UL

The result of the above calculation will be out by 86400 seconds this year.
On the originally referenced web site, the relevant question specified
that leap years are to be ignored. The macro name should have
reflected that fact, though I'm not sure of the best way to do so.
SECONDS_PER_NON _LEAP_YEAR?

Jan 2 '08 #11
On 2 Jan, 22:29, jameskuy...@ver izon.net wrote:
dspfun wrote:
What about question 12, isn't it implementation specific since the
representation of the basic types are not precisely defined by the
language?

This has nothing to do with the representation of the basic types, it
depends only upon the usual arithmetic conversions. While the results
of those conversions vary from implementation to implementation, the
particular values chose for a and b below are guaranteed to produce
the same final results on all conforming implementations of C.


---------------
12. What does the following code output and why?
void foo(void)
{
Â* unsigned int a = 6;
Â* int b = -20;
Â* Â* (a+b 6) ? puts("6") : Â*puts("<= 6");
}
Answer:
This question tests whether you understand the integer promotion rules
in C-an area that I find is very poorly understood by many developers.
Anyway, the answer is that this outputs "6." The reason for this is
that expressions involving signed and unsigned types have all operands
promoted to unsigned types. ...

Note that this is not the correct rule in C99, though I think it's a
correct statement of the C90 rule. In C99, the signed operand is
converted to the type of the unsigned operand only if the rank of the
unsigned type is greater than or equal to the rank of the signed type.
In this case, that happens to be true.
... Thus �20 becomes a very large positive
integer and the expression evaluates to greater than 6. ...

Specifically, (a+b) has a value of UINT_MAX-13. The value of that sum
depends upon the implementation, since UINT_MAX depends upon the
implementation. However, since UINT_MAX must be at least 65535, that
value is guaranteed to be at least 65522, and therefore greater than
6, and the routine is guaranteed to pass "6" to puts().- Dölj citerad text -

- Visa citerad text -
Ok, thank you for the explanation! I believe it is the following in
C99 that specifies this (that the result of a+b will be UINT_MAX + 1
-20 + 6 = UINT_MAX-13):

2
Otherwise, if the new type is unsigned, the value is converted by
repeatedly adding or
subtracting one more than the maximum value that can be represented in
the new type
until the value is in the range of the new type.49)
Jan 2 '08 #12
ja*********@ver izon.net wrote:
>
.... snip ...
>
On the originally referenced web site, the relevant question
specified that leap years are to be ignored. The macro name
should have reflected that fact, though I'm not sure of the
best way to do so. SECONDS_PER_NON _LEAP_YEAR?
Too long. Try SECS_PER_SQUAT_ YR. :-)

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Jan 2 '08 #13
CBFalconer <cb********@yah oo.comwrites:
ja*********@ver izon.net wrote:
>>
... snip ...
>>
On the originally referenced web site, the relevant question
specified that leap years are to be ignored. The macro name
should have reflected that fact, though I'm not sure of the
best way to do so. SECONDS_PER_NON _LEAP_YEAR?

Too long. Try SECS_PER_SQUAT_ YR. :-)
The term for a non-leap year is "common year".

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 2 '08 #14
Keith Thompson wrote:
dspfun <ds****@hotmail .comwrites:
>Are suffixes allowed on expressions?

No (unless the expression happens to be an integer constant).
The suffix is *part* of the constant. They form one token.
You can't have e.g. `12 UL`.
--
Army1987 (Replace "NOSPAM" with "email")
Jan 3 '08 #15
CBFalconer wrote:
Ben Pfaff wrote:
>dspfun <ds****@hotmail .comwrites:
>>Are suffixes allowed on expressions?
No. The suffixes that you are talking about are allowed only on
integer and floating-point constants.
>>For example, is the following allowed:

#define SECONDS_PER_YEA R (60 * 60 * 365) UL
No. To get the apparently intended effect, you can write
#define SECONDS_PER_YEA R (60UL * 60 * 365)
or
#define SECONDS_PER_YEA R (unsigned long int) (60 * 60 * 365)

The second will only work if the expression (60 * 60 * 365) is
within the range of an int. This is doubtful if running on a 16
bit system.
And SECONDS_PER_YEA R really is equal to (60 * 60 * 24 * 365).
^^^^

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Jan 3 '08 #16
How about question 7, is it correct (referring to the test in the link
at the top of the thread)? :

int const * a const;

The declaration above doesn't compile using gcc.
Jan 3 '08 #17
dspfun wrote:
How about question 7, is it correct (referring to the test in the link
at the top of the thread)? :

int const * a const;

The declaration above doesn't compile using gcc.
'const' is a type-qualifier, and therefore a declaration-specifier.
'a' is parsed as a direct-declarator, and therefore as a declarator,
and therefore as an init-declarator-list. 6.7p1 requires that
declarations-specifiers preceed the init-declarator-list, so this is
an incorrect declaration.
Jan 3 '08 #18
On 3 Jan, 21:18, jameskuy...@ver izon.net wrote:
dspfun wrote:
How about question 7, is it correct (referring to the test in the link
at the top of the thread)? :
int const * a const;
The declaration above doesn't compile using gcc.

'const' is a type-qualifier, and therefore a declaration-specifier.
'a' is parsed as a direct-declarator, and therefore as a declarator,
and therefore as an init-declarator-list. 6.7p1 requires that
declarations-specifiers preceed the init-declarator-list, so this is
an incorrect declaration.
Thanks for a great answer!

How did you do the mapping type-qualifier -declaration-specifier and
direct-declarator -declarator -init-declarator-list ?

I found something more suspicuous in the anser to question 2:

"Knowledge of the ternary conditional operator. This operator exists
in C because it allows the compiler to produce more optimal code than
an if-then-else sequence. "

Is this really true?
Jan 3 '08 #19


dspfun wrote:
On 3 Jan, 21:18, jameskuy...@ver izon.net wrote:
dspfun wrote:
How about question 7, is it correct (referring to the test in the link
at the top of the thread)? :
int const * a const;
The declaration above doesn't compile using gcc.
'const' is a type-qualifier, and therefore a declaration-specifier.
'a' is parsed as a direct-declarator, and therefore as a declarator,
and therefore as an init-declarator-list. 6.7p1 requires that
declarations-specifiers preceed the init-declarator-list, so this is
an incorrect declaration.

Thanks for a great answer!

How did you do the mapping type-qualifier -declaration-specifier and
This is just a matter of reading and correctly interpreting the
productions listed in section 6.7p1.
direct-declarator -declarator -init-declarator-list ?
I left out init-declarator, between the last two items there. See
sections 6.7.5p1 and 6.7p1.
I found something more suspicuous in the anser to question 2:

"Knowledge of the ternary conditional operator. This operator exists
in C because it allows the compiler to produce more optimal code than
an if-then-else sequence. "

Is this really true?
I sincerely doubt that any modern compiler produces different code
depending upon whether if-then-else or ?: is used, and I'm quite sure
that any such minor efficiencies have nothing to do with the reason
why it exists. The ?: operator is syntactic sugar, which makes certain
construct much simpler to write. It's not necessary, but it is
occasionally convenient, and convenience should not be underrated as a
feature of a computer programming language.
Jan 3 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1327
by: p.kosina | last post by:
Its just for my convienience: When saving new file in IDLE I HAVE to always manually add suffix .py, otherwise it is saved without ANY suffix. Can it be set somewhere? Thank you Pavel
12
2137
by: Iain Downie | last post by:
Dear list, we are getting a few folk trying to register for our birdwatching surveys with emails of the form: aname@phonecoop.coop, in other words with a 4 character ending (other examples are .info). Currently, I use a regexp to check on 'normal' emails with 3 chars..... function checkEmail() { var emailVar = document.shorter.email.value;
1
5015
by: Endif | last post by:
I am tring to execute the following SQL statements through the Iseries Navigator for DB2/V8.2, But i come up with an error saying recursion is not allowed in common table expression. This is a example i picked up from SQL cook book. I am not sure where i am wrong. Any help is appreciated WITH TEMP ( SUPV_ID,EMPID, FIRSTNAME) AS ( SELECT TV.SUPV_ID,TV.EMPID, TV.FIRSTNAME
3
1948
by: Green | last post by:
Hi, I have a question that when i surf the internet, i found out that using ..aspx suffix means using asp.net, using .jsp means using javaserver page, etc. But some don't have any suffix. For instance, http://daniel.gallery.whitelands.com/photos/newyorktrip , there is no such suffix. Can anyone tell me what kind of technology they are using? I appreciate!
9
12941
by: Hamish M | last post by:
Hi I am interested in opinions on this topic. I have heard that a suffix is not a good solution and type casts are much better for example. ----------------------------------------------------------------- #define MAX_UWORD (T_UWORD)65535
5
1568
by: Casey Hawthorne | last post by:
Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python" The and-or conditional expression trick from page 41 of "Dive into Python" Wrap the arguments in lists and then take the first element. ''
0
1198
by: Lamy | last post by:
I am trying to set a culture in my skin file, I had used the following code in my skin file: <asp:Label ID="UserNameLabel" runat="server" Text='<%$ Resources: CreateNew, Label1UserName %>' AssociatedControlID="UserName" style="FONT-WEIGHT: bold;FONT-SIZE: 11px; COLOR: #000000"></asp:Label> from the globalResources and I get the following error: Expressions are not allowed in skin files.
3
6061
by: Ahmedhussain | last post by:
I am writing this query but didnt understand why isnt it working... Its giving me the error : INSERT INTO (, , , , , , , , , , , , , , , , , , , , ) VALUES ((select (.User_Id) as UserID from Job inner join Users ON (.User_Id = .User_Id) where .User_Id = '1') , @Cat_id, @Job_Description, @Job_Status, @JobTitle, @NumberOfPositions, @JobTenure, @ManagementLevel, @JobDuration, @EducationRequired, @Experience, @RequiredTravel, @Salary,...
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.