473,408 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

assert(x) and '#define ASSERT(x) assert(x)'

Here are two programs.

--- foo1.c ---
#include <assert.h>
#define FOO 10
int main()
{
assert (15 < FOO);
return 0;
}
-------------

--- foo2.c ---
#include <assert.h>
#define FOO 10
#define ASSERT(x) assert(x)
int main()
{
ASSERT (17 < FOO);
return 0;
}
-------------

// gcc 3.3.3

$ gcc -W -Wall foo1.c -o a1
// No errors/warnings

$ gcc -W -Wall foo2.c -o a2
// No errors/warnings

$ ./a1
assertion "15 < FOO" failed: file "foo1.c", line 5
Aborted (core dumped)

$ ./a2
assertion "17 < 10" failed: file "foo2.c", line 6
Aborted (core dumped)

$ gcc -dM -E foo1.c | grep ASSERT
// No output

$ gcc -dM -E foo2.c | grep ASSERT
#define ASSERT(x) assert(x)
-------------------------------------
Why does a1 print "15 < FOO", but not "15 < FOO"?
Why does a2 print "17 < 10", but not "17 < FOO"?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Nov 14 '05 #1
5 3144
Alex Vinokur <al****@big-foot.com> wrote:
Here are two programs. --- foo1.c ---
#include <assert.h>
#define FOO 10
int main()
{
assert (15 < FOO);
return 0;
}
------------- --- foo2.c ---
#include <assert.h>
#define FOO 10
#define ASSERT(x) assert(x)
int main()
{
ASSERT (17 < FOO);
return 0;
}
------------- [snip]

[reordered] Why does a2 print "17 < 10", but not "17 < FOO"?
Macro call
ASSERT (17 < FOO)
is expanded at first to:
assert ( 17 < 10 )
and then the above is expanded further, so its obvious
you can't expect anything else.

Why does a1 print "15 < FOO", but not "15 < FOO"?

ITYM: "15 < 10"

Now I'm stuck here, perhaps someone else confirm this:

7.2.1.1 The assert macro
[...] the assert macro writes
information about the particular call that failed (including
the text of the argument, [...]

IMO the argument to the *macro* is: `17 < FOO', and it's stringified
version is: "15 < FOO". I think in this case assert() *must not*
expand it's argument for print-out.

But in my GNU C libraries (in two versions; this is a library, not a
compiler version issue) in /usr/include/assert.h in the definition of
assert() the argument is stringified _indirectly_ via another function-like
macro (__STRING()), hence it is expanded before strigification. In both
cases it printed "15 < 10" or "17 < 10" on my system, which I consider
incorrect. Could somebody valuate my supposition?

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #2
Neo

"S.Tobias" <si***@FamOuS.BedBuG.pAlS.INVALID> wrote in message
news:30*************@uni-berlin.de...
Alex Vinokur <al****@big-foot.com> wrote:
Here are two programs.
--- foo1.c ---
#include <assert.h>
#define FOO 10
int main()
{
assert (15 < FOO);
return 0;
}
-------------

--- foo2.c ---
#include <assert.h>
#define FOO 10
#define ASSERT(x) assert(x)
int main()
{
ASSERT (17 < FOO);
return 0;
}
-------------

[snip]

[reordered]
Why does a2 print "17 < 10", but not "17 < FOO"?


Macro call
ASSERT (17 < FOO)
is expanded at first to:
assert ( 17 < 10 )
and then the above is expanded further, so its obvious
you can't expect anything else.

Why does a1 print "15 < FOO", but not "15 < FOO"?

ITYM: "15 < 10"

Now I'm stuck here, perhaps someone else confirm this:

7.2.1.1 The assert macro
[...] the assert macro writes
information about the particular call that failed (including
the text of the argument, [...]


Stan, could U plz. tell me from where do U quote these specification
and from where can I get the same?
-Neo
IMO the argument to the *macro* is: `17 < FOO', and it's stringified
version is: "15 < FOO". I think in this case assert() *must not*
expand it's argument for print-out.

But in my GNU C libraries (in two versions; this is a library, not a
compiler version issue) in /usr/include/assert.h in the definition of
assert() the argument is stringified _indirectly_ via another
function-like
macro (__STRING()), hence it is expanded before strigification. In both
cases it printed "15 < 10" or "17 < 10" on my system, which I consider
incorrect. Could somebody valuate my supposition?

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

Nov 14 '05 #3
On 25 Nov 2004 10:58:49 GMT, S.Tobias
<si***@FamOuS.BedBuG.pAlS.INVALID> wrote:
Now I'm stuck here, perhaps someone else confirm this:

7.2.1.1 The assert macro
[...] the assert macro writes
information about the particular call that failed (including
the text of the argument, [...]

IMO the argument to the *macro* is: `17 < FOO', and it's stringified
version is: "15 < FOO". I think in this case assert() *must not*
expand it's argument for print-out.

But in my GNU C libraries (in two versions; this is a library, not a
compiler version issue) in /usr/include/assert.h in the definition of
assert() the argument is stringified _indirectly_ via another function-like
macro (__STRING()), hence it is expanded before strigification. In both
cases it printed "15 < 10" or "17 < 10" on my system, which I consider
incorrect. Could somebody valuate my supposition?


Borland C on Windows prints "15 < FOO" for the first, as does GCC 3.3.3
under Cygwin (no extra 'stringize' macros in assert.h for either).
Perhaps this is something which is specific to Linux headers? My Linux
versions (2.2.x kernels, Debian 'woody' release) have the incorrect
definition. FreeBSD 5.1 has a correct one. I don't have access at the
moment to a Solaris system...

Chris C
Nov 14 '05 #4
Neo <ti***************@yahoo.com> wrote:
"S.Tobias" <si***@FamOuS.BedBuG.pAlS.INVALID> wrote in message
news:30*************@uni-berlin.de...
[snip 40 lines of *completely unrelated* quotes]
7.2.1.1 The assert macro
[...] the assert macro writes
information about the particular call that failed (including
the text of the argument, [...]

Stan,
Since you called me by name, I'll try to answer you:
could U plz.
I have no idea whether U could plz. I swear, I have never
seen U plzing anywhere. I guess I couldn't do it either.
tell me from where do U quote these specification
I'm sure U didn't quote them, because actually it was I.
Does it really look as if U did it?
and from where can I get the same?


I will tell you, and I think many others here would too, when
you learn to write nice English; I'm sure you could. :)

[snip another block of *noise*]

Please, try again.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #5
"Neo" <ti***************@yahoo.com> writes:
[...]
Stan, could U plz. tell me from where do U quote these specification
and from where can I get the same?
-Neo


Neo, you need to learn some of the conventions we use when we post
here. It's "you", not "U", and "please", not "plz". Abbreviations
like that are ok when you're entering messages on a phone keypad (or
whatever), but they're frowned on here. We'll make allowances for
grammar and spelling if English is not your native language (or even
if it is), but thinks like "could U plz." just make your articles more
difficult to read. You only need to write it once; it's likely to be
read hundreds of times. Take the time to make our job a little
easier.

Also, it's rarely necessary to quote the entire article to which
you're responding. Delete anything not relevant to your response, as
I've done here.

The specification Stan was referring to is the C99 standard, ISO/IEC
9899:1999 (E). It's not freely available, but you can buy a PDF copy
from ANSI for $18 (but watch out for the licensing terms). Other
national bodies can probably also sell you a copy.

The latest public draft is N869; a Google search should easily turn up
a copy of it. It's very close to the final standard.

--
Keith Thompson (The_Other_Keith) 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.
Nov 14 '05 #6

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

Similar topics

7
by: Stephen Tyndall | last post by:
I know the preprocessor is evil, but I'd like to know what's going on in the following code. The problem is when the num variable is used in the ASSERT macro inside main(). When running the...
4
by: Leo | last post by:
Hi @ all. I looked up the implementation of the assert macro of my compiler (MinGW), because I wanna write my own assert. I found this: #define assert(x) ((void)0) #define assert(e) ...
3
by: Tony Johansson | last post by:
Hello Experts! I'm reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don' work. The text in the book says "To offer the...
36
by: Martin | last post by:
Can anyone help with a quick query... I've seen the ASSERT macro defined as: #define ASSERT(f) \ do { \ if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \ FatalExit (0); \ } while...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
13
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could...
12
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I saw a couple of form of assert in code on Windows, 1. ASSERT; 2. assert; 3. _ASSERT; 4. _assert.
11
by: Francois Grieu | last post by:
Hi, I'm using an assert()-like macro to test a constant expression at compile time #define ASSERT(condition) struct{char assert_failure;} The idea is that this macro cause a compilation...
9
by: pereges | last post by:
Ok, so once I'm done debugging my code(split across multiple modules) using the assert macro, I would want to switch off all the assert macros ued in the program. Does this mean I have to include:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...
0
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,...
0
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...
0
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...

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.