0

While reading Programming Pearls, I encountered the program timemod.c available here.

I am not good at C Macros so couldn't figure out what the \ means after each line in the definition of M(op). What is that for? Also what is meant by first line printf(" %-22s", #op);?

Along with this answer if you can point to some good source or tut. for C Prepocessor, it will be appreciated.

kamalbanga
  • 1,881
  • 5
  • 27
  • 46
  • 1
    `\\` is for escaping the newline after it, so you can span 1 single line of instructions on multiple lines. in other words, if you escape newlines, the next line still counts as a follow up on the previous line of commands. – user2485710 May 03 '14 at 08:13
  • 2
    You might want to get yourself a [decent introductory book on C](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) before you go too much further with learning programming. – Paul R May 03 '14 at 08:14
  • @PaulR I do have read Kernighan & Ritchie but couldn't clear the above asked doubts from it. – kamalbanga May 03 '14 at 08:20
  • K&R is a good start but it's rather out-of-date - you might want to try one or two more books from [the list](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Paul R May 03 '14 at 08:30
  • @PaulR I even own the Linden's `Expert C Programming` which I guess is pretty good. However I couldn't find much about preprocessor in it. – kamalbanga May 03 '14 at 08:33
  • Linden's Expert C Programming is 1994 - still useful but maybe try something more modern that includes C99 ? – Paul R May 03 '14 at 08:35
  • @PaulR There are many books in the list you gave. Can you suggest `one` out of those? – kamalbanga May 03 '14 at 08:49

2 Answers2

4

couldn't figure out what the \ means after each line in the definition of M(op). What is that for?

Backslash at the (very) end of the line allows the macro definition to continue to the next line. Otherwise it would end at the newline.

Also what is meant by first line printf(" %-22s", #op);?

The # sign is the "stringize operator"; it turns the macro argument op into a string literal by putting double-quotes around it.

Aside from that, %-22s tells printf to print a string with left-justification in 22 characters.

Along with this answer if you can point to some good source or tut. for C Prepocessor, it will be appreciated.

Learning the preprocessor "just happens"… it is a tool of last resort, so expertise in it should not usually be actively sought. I've written a C++11/C99 preprocessor — and it's harder than it sounds — but I have not encountered a good tutorial.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Would you give some minimum working example that uses the "stringize operator" `#` anywhere not in preprocessor? – kamalbanga May 03 '14 at 08:25
  • 1
    @kamalbanga : The "stringization operator" is a pre-processor operator, it is not valid outside of pre-processor macro definitions. – Clifford May 03 '14 at 08:42
2

For documentation of the C pre-processor, the GNU compiler documentation is pretty comprehensive, though beware of those parts described as GNU specific extensions if you are not using GCC.

C pre-processor macros must be defined on a single line, but that makes very long macros difficult to read. The \ is an escape character, it removes the next character from the parsing stream, so that in this case it is as if the newline was not there.

The # preceding a proprocessor argumnet turns the argument into a string, so in teh example:

#define PRINT_INT_EXPR( v ) printf( "%s = %d", #v, v ) ;  

Given: int counter = 5 ; PRINT_INT_EXPR( counter ) ;

The output will be: "counter = 5", but equally you could write:

PRINT_INT_EXPR( 1+2+3+4 )

and the output would be: "1+2+3+4+5 = 15".

I would suggest that the example you cite is hardly a "programming pearl" (neither are teh examples of mine above), the definition of the macro M(op) is an example of all that is bad and ill-advised about using function-like macros in C. The fact that you had to ask what it means is evidence enough. The fact that it can be broken by adding a single invisible space or tab character after one of the \ escape characters should leave you recoiling in horror.

Clifford
  • 88,407
  • 13
  • 85
  • 165