473,499 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stringizing a hex value in the preprocessor

Consider the little program below. It simply attempts to determine what
version of what compiler it was built with via documented preprocessor
macros and print out the result. When the compiler is gcc it works fine,
because __VERSION__ is supplied as a string. But identifying the Sun
compiler is harder because it provides __SUNPRO_C as a numerical value
(equal in my case to 0x580). My attempt to stringify it produces an error:

% cc -g -o foo foo.c
"foo.c", line 13: invalid source character: '#'
"foo.c", line 13: syntax error before or at: 0x580
cc: acomp failed for foo.c

Is there a way to do this?

Thanks,
HT

--------------------------------------------------------------
#include <stdio.h>

#if defined(__GNUC__)
#define COMPILER_STRING "gcc " __VERSION__
#elif defined(__SUNPRO_C)
#define COMPILER_STRING "SunStudio " #__SUNPRO_C
#else
#define COMPILER_STRING "unknown compiler/version"
#endif

int main(void)
{
printf("Compiled with %s\n", COMPILER_STRING);
return 0;
}
Dec 2 '05 #1
14 6600

Henry Townsend wrote:
Consider the little program below. It simply attempts to determine what
version of what compiler it was built with via documented preprocessor
macros and print out the result. When the compiler is gcc it works fine,
because __VERSION__ is supplied as a string. But identifying the Sun
compiler is harder because it provides __SUNPRO_C as a numerical value
(equal in my case to 0x580). My attempt to stringify it produces an error:


Here's my try:

#include <stdio.h>

#define str(x) #x
#define xstr(x) str(x)
#define paste(x, y) x ## y

#define __SUNPRO_C 0x580
#if defined(__IGNUC__)
#define COMPILER_STRING "gcc " __VERSION__
#elif defined(__SUNPRO_C)
#define COMPILER_STRING xstr(paste(SunStudio, xstr(__SUNPRO_C)))
#else
#define COMPILER_STRING "unknown compiler/version"
#endif

int main(void)
{
printf("Compiled with %s\n", COMPILER_STRING);
return 0;
}

and this was compiled with gcc -std=c99 -Wall.

Dec 2 '05 #2

Suman wrote:
Henry Townsend wrote:
Consider the little program below. It simply attempts to determine what
version of what compiler it was built with via documented preprocessor
macros and print out the result. When the compiler is gcc it works fine,
because __VERSION__ is supplied as a string. But identifying the Sun
compiler is harder because it provides __SUNPRO_C as a numerical value
(equal in my case to 0x580). My attempt to stringify it produces an error:


Here's my try:


a tad improved:
--------------------------------------><----------------------------------------------------------------
#include <stdio.h>

#define str(x) #x
#define xstr(x) str(x)
#define paste(x, y) x ## y

#define __SUNPRO_C 0x580
#if defined(__IGNUC__)
#define COMPILER_STRING "gcc " __VERSION__
#elif defined(__SUNPRO_C)
#define COMPILER_STRING xstr(SunStudio xstr(__SUNPRO_C))
#else
#define COMPILER_STRING "unknown compiler/version"
#endif

int main(void)
{
printf("Compiled with %s\n", COMPILER_STRING);
return 0;
}
--------------------------------------><----------------------------------------------------------------
gcc -std=c99 -Wall -pedantic -ansi. And, excuse the ignominious
__IGNU__.
I don't have a Sun compiler to play with :)

HTH

Dec 2 '05 #3
Suman wrote:
Here's my try:


a tad improved:
[snip]


Yes, that works. Actually it prints:

Compiled with SunStudio "0x580"

and a tweak I made to use the line:

#define COMPILER_STRING "SunStudio " ## xstr(__SUNPRO_C)

removes the quotes to give:

Compiled with SunStudio 0x580

Which is what I was looking for. Thanks!

HT
Dec 2 '05 #4
Here is My try :

#include <stdio.h>

#define str(x) #x
#define paste(x,y) x##y
#define Mystr(x) str(x)
#define __SUNPRO_C 0x580

#if defined(__IGNUC__)

#define COMPILER_STRING "gcc " __VERSION__

#elif defined(__SUNPRO_C)

#define COMPILER_STRING paste("SunStudio",Mystr(__SUNPRO_C))

#else

#define COMPILER_STRING "unknown compiler/version"

#endif

int main(void)
{
printf("Compiled with %s\n", COMPILER_STRING);
return 0;
}
-- Raghu.
"Henry Townsend" <he************@not.here> wrote in message
news:fu********************@comcast.com...
Suman wrote:
Here's my try:


a tad improved:
[snip]


Yes, that works. Actually it prints:

Compiled with SunStudio "0x580"

and a tweak I made to use the line:

#define COMPILER_STRING "SunStudio " ## xstr(__SUNPRO_C)

removes the quotes to give:

Compiled with SunStudio 0x580

Which is what I was looking for. Thanks!

HT

Dec 2 '05 #5
Except that I would not use small letters for macros. Or did I miss
something? The solution is good but hard to read for me.

Hubble

Dec 2 '05 #6

Henry Townsend wrote:
Consider the little program below. It simply attempts to determine what
version of what compiler it was built with via documented preprocessor
macros and print out the result. When the compiler is gcc it works fine,
because __VERSION__ is supplied as a string. But identifying the Sun
compiler is harder because it provides __SUNPRO_C as a numerical value
(equal in my case to 0x580). My attempt to stringify it produces an error:


Why not just use a simple shell script invoked by makefile to do just
that?

$ cat build_string
#! /bin/bash

echo "namespace $1 { "
echo "extern char const build_string[] = "
echo \"$(date -u +"%F %T UTC") \"
echo \"$(g++ --version | head -n 1)\"
echo ";"
echo "}"

bash-3.00$ cat Makefile

....
all: app
app: dirs build_string $(app_obj)
$(CXX) -o $(bin_dir)app $(app_obj) $(LDFLAGS)
....
..PHONY : dirs clean build_string install
build_string:
./build_string app > $(src_dir)build_string.cpp
....

Dec 2 '05 #7
"Hubble" <re****@huober.de> writes:
Except that I would not use small letters for macros. Or did I miss
something? The solution is good but hard to read for me.


What solution?

Please read <http://cfaj.freeshell.org/google/>.

--
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.
Dec 2 '05 #8
Keith Thompson writes:
What solution Please read <http://cfaj.freeshell.org/google/>.
Sorry for using google:

Suman writes:
#define str(x) #x
#define xstr(x) str(x)
#define paste(x, y) x ## y


I would not use small letters for macros. Or did I miss
something? These macros is good but hard to read for me.

Hubble

Dec 3 '05 #9
Hubble wrote:
Suman writes:
#define str(x) #x
#define xstr(x) str(x)
#define paste(x, y) x ## y


I would not use small letters for macros. Or did I miss
something? These macros is good but hard to read for me.


The first two of those macros are copied from the C standard.
That makes it easy to look them up in the standard
if you want to see what the standard has to say about them.
Otherwise, neither would I write them that way.

--
pete
Dec 3 '05 #10
On 2005-12-02, Suman <sk*****@gmail.com> wrote:

#ifdef __STDC__
#define str(x) #x
#define paste(x, y) x ## y #else
#define str(x) "x"
#define paste(x) x/**/y
#endif #define xstr(x) str(x)


--
hey, if you're going to hide them in their own macros, you might as well...
Dec 3 '05 #11
pete wrote:

Hubble wrote:
Suman writes:
#define str(x) #x
#define xstr(x) str(x)
#define paste(x, y) x ## y


I would not use small letters for macros. Or did I miss
something? These macros is good but hard to read for me.


The first two of those macros are copied from the C standard.
That makes it easy to look them up in the standard
if you want to see what the standard has to say about them.
Otherwise, neither would I write them that way.


I suppose they wouldn't be that difficult to look up
if they were in caps and you knew they were in the standard.
There's a couple of things
that I copy from the C standard in my code.

For example

int main(int argc, char *argv[])

even though

int main(int argc, char **argv)

is really my prefered style.

--
pete
Dec 3 '05 #12
"Hubble" <re****@huober.de> writes:
Keith Thompson writes:
What solution

Please read <http://cfaj.freeshell.org/google/>.


Sorry for using google:

[snip]

No need to apologize; using google is perfectly acceptable as long as
you use it correctly. (Some people have killfiled everything posted
through groups.google.com, but that's their decision.)

--
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.
Dec 3 '05 #13

pete wrote:
Hubble wrote:
Suman writes:
#define str(x) #x
#define xstr(x) str(x)
#define paste(x, y) x ## y


I would not use small letters for macros. Or did I miss
something? These macros is good but hard to read for me.


The first two of those macros are copied from the C standard.
That makes it easy to look them up in the standard
if you want to see what the standard has to say about them.
Otherwise, neither would I write them that way.


Addendum:
The third, is a modified form of what you find in K&R 2:
#define paste(front, back) front ## back

Dec 5 '05 #14
Suman wrote:

pete wrote:
Hubble wrote:
Suman writes:

>#define str(x) #x
>#define xstr(x) str(x)
>#define paste(x, y) x ## y

I would not use small letters for macros. Or did I miss
something? These macros is good but hard to read for me.


The first two of those macros are copied from the C standard.
That makes it easy to look them up in the standard
if you want to see what the standard has to say about them.
Otherwise, neither would I write them that way.


Addendum:
The third, is a modified form of what you find in K&R 2:
#define paste(front, back) front ## back


That's good enough for me.

It might be worth keeping in mind
that there's a few deviations here and there in standard C,
from what's generally considered good style.
FILE is a typedef, stdout is a macro.

--
pete
Dec 5 '05 #15

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

Similar topics

2
1853
by: Steven T. Hatton | last post by:
I'm serious about this folks! I really want to see the CPP obviated. People who have never used tools such as JBuilder may not fully appreciate the advantages they provide. This isn't about...
8
5331
by: Siemel Naran | last post by:
#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__, __LINE__, #x); } MSVC7 gives an error: "error C2014: preprocessor command must start as first nonwhite space".
9
3637
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
11
3015
by: Richard Meister | last post by:
Hi, I'd like to define several constants and make sure that all of them are smaller than a given other constant. I thought this could be done by a simple macro. Something like this: #define...
32
2731
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
1
1930
by: Ravi | last post by:
Hi, I have this #define KB 1 #define KB_AM 33 #define KB_RM 44 #define AM 2 #define RM 3
31
2880
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
2
1996
by: babakandme | last post by:
Hi everybody:D I've a string that contains the name of a class. Some members told that I can use """Stringizing Operator (#)""", but the problem is here, that I have the string, & I want...
7
3346
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
0
7006
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
7169
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,...
1
6892
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
5467
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,...
1
4917
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
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
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...

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.