473,804 Members | 3,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what's wrong with the macro


#include<stdio. h>
#include<stdlib .h>
#include<string .h>

#define SECONDS_PER_YEA R (60*60*24*365)U L
int main()
{
unsigned long int u_i = SECONDS_PER_YEA R;
printf("%lu\n", u_i);

return 0;
}
cc -g -c -Wall -std=c99 -DDEBUG define.c
define.c: In function 'main':
define.c:17: error: expected ',' or ';' before 'UL'
make: *** [define.o] error 1

Oct 22 '06
38 2526
"Giorgio Silvestri" <gi************ **@libero.itwri tes:
"Keith Thompson" <ks***@mib.orgh a scritto nel messaggio
news:ln******** ****@nuthaus.mi b.org...
[...]
>You want parentheses around the second defintion:

#define SECONDS_PER_YEA R ((unsigned long)(60UL * 60UL * 24UL * 365UL)(

Probably:

#define SECONDS_PER_YEA R ((unsigned long)(60UL * 60UL * 24UL * 365UL))

is better :-)
Um, yeah, thanks for catching that.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 23 '06 #11
Frederick Gotham <fg*******@SPAM .comwrites:
Keith Thompson posted:
>You want parentheses around the second defintion:

#define SECONDS_PER_YEA R ((unsigned long)(60UL * 60UL * 24UL * 365UL)(

Consider "sizeof SECONDS_PER_YEA R".

I don't understand... what could that have parsed to?
Frederick, I don't want to leave you waiting for a response from me,
so I'll remind you of this:

<http://groups.google.c om/group/comp.lang.c/msg/a03c532b26664ed 4>

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 23 '06 #12
Keith Thompson wrote:
>
"Giorgio Silvestri" <gi************ **@libero.itwri tes:
"Keith Thompson" <ks***@mib.orgh a scritto nel messaggio
news:ln******** ****@nuthaus.mi b.org...
[...]
You want parentheses around the second defintion:

#define SECONDS_PER_YEA R ((unsigned long)(60UL * 60UL * 24UL * 365UL)(
Probably:

#define SECONDS_PER_YEA R ((unsigned long)(60UL * 60UL * 24UL * 365UL))

is better :-)

Um, yeah, thanks for catching that.
What is the cast for?

--
pete
Oct 23 '06 #13
In article <45***********@ mindspring.com> ,
pete <pf*****@mindsp ring.comwrote:
>"Giorgio Silvestri" <gi************ **@libero.itwri tes:
#define SECONDS_PER_YEA R ((unsigned long)(60UL * 60UL * 24UL * 365UL))
>What is the cast for?
Once the constant (without the cast) is computed by the parser, it
becomes a numeric value of indeterminate width -- through some unspecified
mechanism the parser knows the minimum width needed to represent it,
but it is not specifically that minimum width and not specifically UL
and so on. If the cast-less constant were determined
to be too large to match the other operand of the expression, then
widening is going to take place. In some contexts, if the constant
were determined to be able to fit within the *signed* version of
a larger type, it would be widened to *signed* rather than to
unsigned [remember, the system knows it as a numeric value at that point,
not specifically as an unsigned numeric value.]. The (unsigned long) cast
stops any uncertainty about exactly what type is going to be there.
Now try printf("%d\n", CASTLESS_SECOND S_PER_YEAR); -- is that right?
It is if the CASTLESS_SECOND S_PER_YEAR is accomedated as a signed int,
but not if CASTLESS_SECOND S_PER_YEAR requires long on that system.
60UL * 60UL * 24UL * 365UL as a numeric value takes about 25 bits.
On a system with 32 bit int and 64 bit unsigned long,
the cast-less version it would fit within a signed int on that system
and %d would be correct and %ld would be wrong. So cast-less, the
value is not as easily portable. Add the cast and you -know- the type
and can always use %lu.
--
Programming is what happens while you're busy making other plans.
Oct 24 '06 #14
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <45***********@ mindspring.com> ,
pete <pf*****@mindsp ring.comwrote:
>>"Giorgio Silvestri" <gi************ **@libero.itwri tes:
>#define SECONDS_PER_YEA R ((unsigned long)(60UL * 60UL * 24UL * 365UL))
>>What is the cast for?
I believe it's superfluous.
Once the constant (without the cast) is computed by the parser, it
becomes a numeric value of indeterminate width -- through some unspecified
mechanism the parser knows the minimum width needed to represent it,
but it is not specifically that minimum width and not specifically UL
and so on. If the cast-less constant were determined
to be too large to match the other operand of the expression, then
widening is going to take place. In some contexts, if the constant
were determined to be able to fit within the *signed* version of
a larger type, it would be widened to *signed* rather than to
unsigned [remember, the system knows it as a numeric value at that point,
not specifically as an unsigned numeric value.]. The (unsigned long) cast
stops any uncertainty about exactly what type is going to be there.
I don't think so. C99 6.6p11 says:

The semantic rules for the evaluation of a constant expression are
the same as for nonconstant expressions.

The expression:

(60UL * 60UL * 24UL * 365UL)

is a constant expression, but not a constant (i.e., a literal).

A single integer literal with a UL suffix is of type unsigned long int
or unsigned long long int, depending on the value; all the constants
in the above are well within the guaranteed range of unsigned long, so
that's their type. Multiplying an unsigned long by an unsigned long
yields an unsigned long, so the entire expression is of type unsigned
long.

As it happens, the final result, 31536000, is also within the
guaranteed range of unsigned long, so there's no possibility of
overflow. But even if there were (say, if you wanted the number of
seconds in a millenium), an overflow wouldn't change the type of the
expression.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 24 '06 #15
Keith Thompson said:
Frederick Gotham <fg*******@SPAM .comwrites:
>Keith Thompson posted:
>>You want parentheses around the second defintion:

#define SECONDS_PER_YEA R ((unsigned long)(60UL * 60UL * 24UL * 365UL)(

Consider "sizeof SECONDS_PER_YEA R".

I don't understand... what could that have parsed to?

Frederick, I don't want to leave you waiting for a response from me,
so I'll remind you of this:

<http://groups.google.c om/group/comp.lang.c/msg/a03c532b26664ed 4>
A waste of time, Keith. If he were interested in responses from you, he'd
have apologised a long time ago.

Personally, whenever I see a question from Mr Gotham, I simply don't bother
to reply. When I see others giving him wrong answers, I don't bother to
correct them. If he wants expert assistance, he should not alienate
experts.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 24 '06 #16
Walter Roberson posted:
>>What is the cast for?

Once the constant (without the cast) is computed by the parser, it
becomes a numeric value of indeterminate width -- through some unspecified
mechanism the parser knows the minimum width needed to represent it,
but it is not specifically that minimum width and not specifically UL
and so on.

Incorrect.

Multiply a UL by a UL and you get a UL.
--

Frederick Gotham
Oct 24 '06 #17
Keith Thompson posted:

<snip tripe>

Richard Heathfield posted:

<snip tripe>

Since the two self-proclaimed masters of this newsgroup won't reply to me,
I'd like to ask any other programmers to explain to me why:

sizeof (unsigned long)(60UL * 60UL * 24UL * 365UL)

would have been undesireable.

Actually, don't bother, because I already know the answer: Keith was wrong.

The expression in question has only one possible parse, because the operand
of "sizeof" cannot be a cast expression. Therefore, the following two
expressions are exactly equivalent:

sizeof (unsigned long)(60UL * 60UL * 24UL * 365UL)

sizeof ((unsigned long)(60UL * 60UL * 24UL * 365UL))

--

Frederick Gotham
Oct 24 '06 #18
Frederick Gotham posted:
The expression in question has only one possible parse, because the
operand of "sizeof" cannot be a cast expression.
And even if it could be, there would be a syntax error.

--

Frederick Gotham
Oct 24 '06 #19


Frederick Gotham wrote On 10/24/06 11:23,:
Keith Thompson posted:

<snip tripe>

Richard Heathfield posted:

<snip tripe>

Since the two self-proclaimed masters of this newsgroup won't reply to me,
I'd like to ask any other programmers to explain to me why:

sizeof (unsigned long)(60UL * 60UL * 24UL * 365UL)

would have been undesireable.

Actually, don't bother, because I already know the answer: Keith was wrong.

The expression in question has only one possible parse, because the operand
of "sizeof" cannot be a cast expression. Therefore, the following two
expressions are exactly equivalent:

sizeof (unsigned long)(60UL * 60UL * 24UL * 365UL)

sizeof ((unsigned long)(60UL * 60UL * 24UL * 365UL))
It's a funny thing, but when I tried these out on three
completely different C compilers, all three treated the two
"exactly equivalent" expressions differently. Specifically,
they all rejected the first with complaints about "syntax
error" or some such, but they all accepted the second.

Three out of three identical mistakes from independent
compiler authors -- that's too much for coincidence, don't
you think? There must be an enormous shadowy conspiracy
at work to retard progress in programming by producing and
promoting faulty C compilers! They're probably taking steps
to silence us even now; I hope I can get this posted befor

--
Er*********@sun .com

Oct 24 '06 #20

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

Similar topics

220
19190
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
4
6270
by: Martin Magnusson | last post by:
I'm using a matrix and vector library, that won't compile. When running g++ I get the error message "macro "minor" passed 5 arguments, but takes just 1" The definition of "minor" looks like below, and it takes 3 arguments. All calls to minor that I have found in the code also pass it three arguments, so I really don't understand this error. Does anything look suspicious with the following definition, or must it be that there is some...
8
3554
by: sandwich_eater | last post by:
I get compiler error "cerr undeclared first use this function." #define err_ret(e) cerr << e; return -1 .... err_ret("who ate my muffy?");
7
23560
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
4
3224
by: Louly | last post by:
Hi everybody, I got the code for dimming a menu item from this group after looking for it for a long time thanks to those who share their experiences with the others :) The problem I'm facing is that it dims the wrong menu item!! Example, if I write "DoCmd.SetMenuItem 0, 2, , acMenuGray", it dims the 3rd command in the 2nd menu!! It's driving me crazy!
14
2451
by: raghu | last post by:
Hello I have a doubt plz clarify that #ifndef SYSTEM_H #define SYSTEM_H what will be the value of SYATEM_H after this #define statement and before that statement. Thanking you all Bye
4
2136
by: Gestorm | last post by:
Hi all, I found a macro "USE_VAR" in the code of bash-3.2 as follows: /*file: bash-3.2/shell.c*/ 344 USE_VAR(argc); 345 USE_VAR(argv); 346 USE_VAR(env); 347 USE_VAR(code); 348 USE_VAR(old_errexit_flag); 349 #if defined (RESTRICTED_SHELL) 350 USE_VAR(saverst);
31
2452
by: Tommy | last post by:
struct stat *stats IF_LINT (= 0); I don't know why "IF_LINT (= 0)" is needed ? Source is df.c of df program on Linux coreutils-5.2.1\coreutils-5.2.1\src Thanks!
32
2747
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template issue. A noddy mixin layer example should illustrate the issue... class Base { protected: int m_Field;
0
10583
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
10337
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...
0
9160
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
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
5525
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
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.