Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C compiling error: stray '##' in program

Tags:

c

I was working with an embedded kernel source when I saw something like this:

#define OMAP_SYS_TIMER_INIT(name, clkev_nr, clkev_src, clksrc_nr, clksrc_src) \
static void __init omap##name##_timer_init(void)                              \
{                                                                             \
    omap2_gp_clockevent_init((clkev_nr), clkev_src);                          \
    omap2_gp_clocksource_init((clksrc_nr), clksrc_src);                       \
}

and when I tryed to make a program that used this ## thing (that I don't know the name) to see what it could really do I didn't got it to work. Below is what I did to test it's function. I just want to see if the argument inside the ## is literal or not, but something is clearly missing in my code for it to compile...

#include <stdio.h>
#include <stdlib.h>

#define DEFINE_1 2
#define DEFINE_2 4
#define DEFINE_3 6

#define DEFINE_i 9

int main(void)
{
  int i;
  for(i=1;i<4;i++) {
    printf("numero %d = %d\n",i,DEFINE_##i##);
  }
  return EXIT_SUCCESS;
}

The output of gcc is:

test.c: In function ‘main’:
test.c:14:5: error: stray ‘##’ in program
test.c:14:33: error: ‘DEFINE_’ undeclared (first use in this function)
test.c:14:33: note: each undeclared identifier is reported only once for each function it appears in
test.c:14:42: error: expected ‘)’ before ‘i’
test.c:14:42: error: stray ‘##’ in program

Anyone knows what is wrong? Thanks

like image 470
Davi S Evangelista Avatar asked Dec 18 '12 12:12

Davi S Evangelista


People also ask

What is stray error in C?

Error: stray '\240' in program is a character encoding error message. \240 is a non-breaking space in iso8859-1 encoding used on web pages, especially when the code shouldn't get line-wrapped by the browser. The compiler doesn't understand non-breaking space and reports about a straying special character error. eg.

How do I fix error 342 on stray?

The problem is that you have Unicode quotation marks instead of ASCII quotation marks; probably your editor automatically changed them, or you copied the text from a site that does this automatically in its authoring software. Replace the quotes with normal ASCII quote (0x22, ") and it should work.

What does stray 342 mean?

If the error message referred to a stray \342 in the code it is most likely to have been caused by copying the code from a Web page which has Unicode characters in it.

What is stray 302 in program Arduino?

It means you have a weird character (302 is its representation) in your code. Search for Shift+space for example which is a spacenolinebreak character you might have gotten in your program by copying and pasting from the webpage or fat finger when you typed in your code.


2 Answers

It's the token concatenation operator for the C preprocessor. The reason your example doesn't compile is because you're not using the ## operator within a macro (i.e. #define statement).

Here's another post with some more information.

like image 83
hall.stephenk Avatar answered Oct 03 '22 14:10

hall.stephenk


## means concatenation at time of preprocessing. http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html

like image 39
kassak Avatar answered Oct 03 '22 15:10

kassak