473,698 Members | 2,275 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
14 6646
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.c om, but that's their decision.)

--
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 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
1868
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 argument, and your IDE would rename every identifier qualified with that namespace to the new name. ...
8
5341
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
3656
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 phase only. I am not using the standalone program 'cpp' because proper functioning of my project...
11
3041
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
2783
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 differences ? If the output is not deterministic then is it possible that the output of the...
1
1944
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
2911
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 many passes. The preprocessor processes a grammer unique to the preprocessor and only that grammer. ...
2
2013
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
3357
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 switch. I have implemented it as under. Please ignore those magic numbers as I have mimized logic to...
0
8600
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9156
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
9021
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
8860
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7712
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
6518
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
5860
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
4361
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
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.