473,563 Members | 2,635 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(__SUNPR O_C)
#define COMPILER_STRING "SunStudio " #__SUNPRO_C
#else
#define COMPILER_STRING "unknown compiler/version"
#endif

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

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(__SUNPR O_C)
#define COMPILER_STRING xstr(paste(SunS tudio, xstr(__SUNPRO_C )))
#else
#define COMPILER_STRING "unknown compiler/version"
#endif

int main(void)
{
printf("Compile d 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(__SUNPR O_C)
#define COMPILER_STRING xstr(SunStudio xstr(__SUNPRO_C ))
#else
#define COMPILER_STRING "unknown compiler/version"
#endif

int main(void)
{
printf("Compile d 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(__SUNPR O_C)

#define COMPILER_STRING paste("SunStudi o",Mystr(__SUNP RO_C))

#else

#define COMPILER_STRING "unknown compiler/version"

#endif

int main(void)
{
printf("Compile d with %s\n", COMPILER_STRING );
return 0;
}
-- Raghu.
"Henry Townsend" <he************ @not.here> wrote in message
news:fu******** ************@co mcast.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_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.
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

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

Similar topics

2
1861
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 pointer safety, or making sure you aren't blowing the bounds of an array. Imagine you could select a namespace, enter a command with a new name as an...
8
5337
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
3646
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. Could someone point me to the appropriate section, or else confirm the behaviour as a bug? For a particular project, I am using the C preprocessor...
11
3023
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 MAX 999 #define DEF_CHECKED_VAL( name, value) #if (value < MAX) \ #define name MAX \
32
2749
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 the same file ? If not then how much variation is allowed ? Is it just a bit more or less white space here and there or could could there be larger...
1
1939
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
2887
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 statements that the compiler does not process. The good people in the alt.comp.lang.learn.c-c++ newsgroup insist that the preprocessor is just one of...
2
2006
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 something vice- versa. As we know with """Stringizing Operator (#)""", we can get the stirng name of a class or ... str <--- #ClassA
7
3351
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 parameters value and signal ID. Signal Id is used to get a user configurable parameter inside a configuration file, which depends on the type of...
0
7580
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
7882
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
7945
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
6244
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...
0
3634
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...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
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 we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
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.