473,569 Members | 2,555 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 #1
41 3011
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
>
I found this in a C test athttp://www.embedded.co m/2000/0005/0005feat2.htm
,
however, I am not able to compile when using this macro (or just the
expression "a = (60 * 60 * 365) UL;").
It should be:
(or just the expression statement "a = (60 * 60 * 24 * 365) UL;")

Jan 2 '08 #2
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)
--
"All code should be deliberately written for the purposes of instruction.
If your code isn't readable, it isn't finished yet."
--Richard Heathfield
Jan 2 '08 #3
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
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)
Oh, and to get the apparently intended value, you need to
multiply by an additional factor of 24.
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
=b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
Jan 2 '08 #4
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
dspfun <ds****@hotmail .comwrites:
>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 latter can, as I am sure you know, overflow. Since the quiz was
on a site that referenced embedded systems, it would be safer to
assume a minimal max int (if you see what I mean).

To the OP: to illustrate using a cast (which *can* be used in
expressions unlike suffixes) you could write:

#define SECONDS_PER_YEA R ((unsigned long int)60 * 60 * 365)

* is left-associative so all the multiplications must produce unsigned
long int results.

--
Ben.
Jan 2 '08 #5
Keith Thompson <ks***@mib.orgw rites:
[...]
There's a contact address in the article; I'll send him pointers to
your article and my followup.
The e-mail address in the article is obsolete (I think the article is
several years old). I found what appears to be his current address by
searching the embedded.com web site. (I won't post it here; I'm sure
he gets more than enough spam already.)

--
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 #6
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.

--
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 #7
What about question 12, isn't it implementation specific since the
representation of the basic types are not precisely defined by the
language?

---------------
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. Thus ý20 becomes a very large positive
integer and the expression evaluates to greater than 6. This is a very
important point in embedded systems where unsigned data types should
be used frequently (see Reference 2). If you get this one wrong, you
are perilously close to not getting the job.
-------------
Jan 2 '08 #8
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.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 2 '08 #9
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().
Jan 2 '08 #10

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

Similar topics

0
1320
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
2126
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 =...
1
4985
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...
3
1940
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...
9
12913
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
1544
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
1189
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...
3
6046
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,...
0
7701
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...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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. ...
0
8130
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...
0
7979
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...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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...
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.