Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 27, 2009, 09:23 AM   #1
mdeh
macrumors 6502
 
Join Date: Jan 2009
Question about Kochan 2.0 ## "merging operator" Page 247

I am not sure if this is a typo or something I simply do not understand....but it is probably the latter. Anyway, here is the setup.

The text explains the use of the merging operator "##"
NOTE...although the text uses printf, I believe the same principles apply to NSLog

Code:
#define printx(n) printf( "%i\n", x ## n) )
A small test program produces an error.

Code:
#include <stdio.h>

#define printx(n) printf( "%i\n", x ## n) 

int main (int argc, const char * argv[]) {
	printx(FF);
	return 0;
}
Quote:
error: 'xFF' undeclared (first use in this function)
The preprocessed code shows this:

Code:
 printf( "%i\n", xFF);
My question.
Is the code usable as is, or did the publisher omit the "0" from the define, as in "Ox ## n .

thank you in advance
mdeh is offline   0 Reply With Quote
Old Jan 27, 2009, 10:12 AM   #2
bbarnhart
macrumors 6502a
 
bbarnhart's Avatar
 
Join Date: Jan 2002
Location: Stilwell, Kansas
Send a message via Skype™ to bbarnhart
I didn't know there was a C++ ## operator. What does that do?

If it were me, I'd switch out that #define with a real function or method.

Your sample doesn't compile because you need a zero before the x

Code:
#define printx(n) printf( "%i\n", 0x ## n) )
bbarnhart is offline   0 Reply With Quote
Old Jan 27, 2009, 10:20 AM   #3
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by mdeh View Post
I am not sure if this is a typo or something I simply do not understand....but it is probably the latter. Anyway, here is the setup.

The text explains the use of the merging operator "##"
NOTE...although the text uses printf, I believe the same principles apply to NSLog

Code:
#define printx(n) printf( "%i\n", x ## n) )
A small test program produces an error.

Code:
#include <stdio.h>

#define printx(n) printf( "%i\n", x ## n) 

int main (int argc, const char * argv[]) {
	printx(FF);
	return 0;
}


The preprocessed code shows this:

Code:
 printf( "%i\n", xFF);
My question.
Is the code usable as is, or did the publisher omit the "0" from the define, as in "Ox ## n .

thank you in advance
From what you are writing, his example code looks wrong. It should probably have been 0x ## n in the macro, then the preprocessed code would have been

Code:
 printf( "%i\n", 0xFF);
On the other hand, you could have the following inside main ():

Code:
        int x1 = 10; int x2 = 20; int x3 = 30;
	printx(1); printx(2); printx(3);
which would print the variables x1, x2 and x3 just fine. But I think the preprocessing output shows exactly what ## does: It creates a single symbol from the "x" on the left side and whatever you substituted for "n" on the right side.

And for the second poster: ## is a _preprocessor_ operator, and it works just fine and in exactly the same way in C, C++, Objective-C and Objective-C++.
gnasher729 is offline   0 Reply With Quote
Old Jan 27, 2009, 10:21 AM   #4
bbarnhart
macrumors 6502a
 
bbarnhart's Avatar
 
Join Date: Jan 2002
Location: Stilwell, Kansas
Send a message via Skype™ to bbarnhart
The operator ## concatenates two arguments leaving no blank spaces between them:

#define glue(a,b) a ## b
glue(c,out) << "test";

glue(c,out) is translated in the preprocessor to cout
bbarnhart is offline   0 Reply With Quote
Old Jan 27, 2009, 10:27 AM   #5
mdeh
Thread Starter
macrumors 6502
 
Join Date: Jan 2009
Quote:
Originally Posted by gnasher729 View Post
On the other hand, you could have the following inside main ():

Code:
        int x1 = 10; int x2 = 20; int x3 = 30;
	printx(1); printx(2); printx(3);
which would print the variables x1, x2 and x3 just fine. But I think the preprocessing output shows exactly what ## does: It creates a single symbol from the "x" on the left side and whatever you substituted for "n" on the right side.

Thanks gnasher. I bet this is what the author meant, as it is repeated quite a few times throughout the text. BTW...this is really neat. I had no idea you could **create** a variable, but it makes sense though. Would you ever use this practically?
Thank you again.

Last edited by mdeh; Jan 27, 2009 at 10:32 AM.
mdeh is offline   0 Reply With Quote
Old Jan 27, 2009, 12:06 PM   #6
Krevnik
macrumors 68020
 
Krevnik's Avatar
 
Join Date: Sep 2003
Quote:
Originally Posted by mdeh View Post
Thanks gnasher. I bet this is what the author meant, as it is repeated quite a few times throughout the text. BTW...this is really neat. I had no idea you could **create** a variable, but it makes sense though. Would you ever use this practically?
Thank you again.
Outside of a macro? I can't think of a single thing.

The key behind the '##' operator is to merge two symbols into one.

'foo ## bar' will always create a 'foobar' symbol. When the compiler comes round, it simply sees 'foobar' and tries to figure out what foobar means using the normal C rules.

Normally, the merge operator has such a small niche use, nobody uses it. I ran across it /once/ in some headers awhile back on Windows, but that was about it.
__________________
iMac 2013 27", 13" rMBP, iPad 4, iPhone 5s
Krevnik is offline   0 Reply With Quote
Old Jan 27, 2009, 12:39 PM   #7
mdeh
Thread Starter
macrumors 6502
 
Join Date: Jan 2009
Quote:
Originally Posted by Krevnik View Post
Normally, the merge operator has such a small niche use, nobody uses it. I ran across it /once/ in some headers awhile back on Windows, but that was about it.
Thank you.
mdeh is offline   0 Reply With Quote
Old Jan 27, 2009, 01:54 PM   #8
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by mdeh View Post
Thanks gnasher. I bet this is what the author meant, as it is repeated quite a few times throughout the text. BTW...this is really neat. I had no idea you could **create** a variable, but it makes sense though. Would you ever use this practically?
Thank you again.
The only reason i could imagine using it practically is for obfuscation. Precompiler directives can be great, but i have never been a fan because it makes things, to me, less readable/debuggable. if something looks like a function, i am going to try to find the definition, likely with a debugger. When it's actually a macro... whoops, no function, now to grep some headers, trying to find the #define.

I'm not saying they don't have their use, and I suppose it's nice to save a little stack space if you are constantly seeing if a character is special by using a macro instead of making a function call, but if things are that performance intensive you can always in-line functions.

-Lee
lee1210 is offline   0 Reply With Quote
Old Jan 27, 2009, 08:00 PM   #9
skochan
macrumors regular
 
Join Date: Apr 2006
Location: California
 
Quote:
Originally Posted by lee1210 View Post
The only reason i could imagine using it practically is for obfuscation. Precompiler directives can be great, but i have never been a fan because it makes things, to me, less readable/debuggable. if something looks like a function, i am going to try to find the definition, likely with a debugger. When it's actually a macro... whoops, no function, now to grep some headers, trying to find the #define.

I'm not saying they don't have their use, and I suppose it's nice to save a little stack space if you are constantly seeing if a character is special by using a macro instead of making a function call, but if things are that performance intensive you can always in-line functions.

-Lee
Yes, it was a stretch for me to even think of a simple example for the ##. It's in the book for completeness in the chapter on the preprocessor. I've never used it in my programs.

Cheers,

Steve Kochan
skochan is offline   0 Reply With Quote
Old Jan 28, 2009, 02:05 PM   #10
autorelease
macrumors regular
 
Join Date: Oct 2008
Location: Achewood, CA
I've seen it used in embedded code. For example, you might define an interrupt handler for the TIMER2_OVERFLOW event with

Code:
ISR(TIMER2_OVERFLOW)
{
  ...
}
ISR might be a macro like

Code:
#define ISR(intr) __interrupt void _vect_ ## intr()
which would expand to

Code:
__interrupt void _vect_TIMER2_OVERFLOW()
{
  ...
}
The linker would then recognize __vect_TIMER2_OVERFLOW as the name of an interrupt handler and make sure it gets put at the proper location in the binary. However, the macro hides these details from the programmer, requiring him/her to only know the name of the interrupt.

Token pasting is basically just another glorious way to abuse the preprocessor. (The stringizing operator, #, is by far the best. I should warn you, this is coming from a guy who once used #__VA_ARGS__ in a macro...)
autorelease is offline   0 Reply With Quote
Old Jan 28, 2009, 04:23 PM   #11
mdeh
Thread Starter
macrumors 6502
 
Join Date: Jan 2009
Quote:
Originally Posted by autorelease View Post
I've seen it used in embedded code. For example, you might define an interrupt handler for the TIMER2_OVERFLOW event with

Code:
ISR(TIMER2_OVERFLOW)
{
  ...
}
ISR might be a macro like

Code:
#define ISR(intr) __interrupt void _vect_ ## intr()
which would expand to

Code:
__interrupt void _vect_TIMER2_OVERFLOW()
{
  ...
}
The linker would then recognize __vect_TIMER2_OVERFLOW as the name of an interrupt handler and make sure it gets put at the proper location in the binary. However, the macro hides these details from the programmer, requiring him/her to only know the name of the interrupt.

Token pasting is basically just another glorious way to abuse the preprocessor. (The stringizing operator, #, is by far the best. I should warn you, this is coming from a guy who once used #__VA_ARGS__ in a macro...)

Thanks for that. The wonderful thing about forums is that there is often someone who can put all this in perspective. That's part of the fun in posting. Thank you to you and Lee and the others for your views.
mdeh is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
"No items found" message on main page - more security fallout? 50548 Site and Forum Feedback 3 Nov 18, 2013 08:36 PM
How can I stop ipod from defaulting on "on this ipod" page instead of "settings" in i kateharp iPod 1 May 15, 2013 08:18 AM
Strange "Page Outs" Question knoxtown iMac 2 Feb 17, 2013 02:27 AM
Merging two "new account" reservations into one family plan notjustgc iPhone 1 Sep 27, 2012 11:06 PM
How is "Scuffgate" merging and becoming "Dent and Scratchgate" in most media outlets? Rm.237 iPhone 5 Sep 23, 2012 06:48 PM

Forum Jump

All times are GMT -5. The time now is 04:14 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC