// in macro's

S

Servé Lau

Consider the following code:

#define MYMACRO "http//www.bla.com"

When I use it like func(MYMACRO), I get the error "incomplete string
literal".

First, is the compiler correct?
Second, is it ok if I change it to this? (it does compile now)

#define MYMACRO "http\/\/www.bla.com"

In other words, can I escape the slash?
 
M

Mark A. Odell

Consider the following code:

#define MYMACRO "http//www.bla.com"

When I use it like func(MYMACRO), I get the error "incomplete string
literal".

First, is the compiler correct?
Second, is it ok if I change it to this? (it does compile now)

#define MYMACRO "http\/\/www.bla.com"

In other words, can I escape the slash?

How about "you must escape the slash" if your C compiler is running
non-standard mode (allows // as comment delim.). Oh and there should be a
colon in there too.
 
C

Christopher Benson-Manica

Servé Lau said:
#define MYMACRO "http//www.bla.com"
When I use it like func(MYMACRO), I get the error "incomplete string
literal".
First, is the compiler correct?
No.

Second, is it ok if I change it to this? (it does compile now)
#define MYMACRO "http\/\/www.bla.com"
In other words, can I escape the slash?

K&R2 says that the result of doing so is undefined. I don't know if C99
specifies that as well.
 
S

Serve Lau

Mark A. Odell said:
How about "you must escape the slash" if your C compiler is running
non-standard mode (allows // as comment delim.). Oh and there should be a
colon in there too.


yes, outlook kep turning it into a hyperlink without quotes so I didn't know
what it would send
 
I

Ivan Vecerina

| Consider the following code:
|
| #define MYMACRO "http//www.bla.com"
|
| When I use it like func(MYMACRO), I get the error "incomplete string
| literal".

Glp... even though // comments are in the C99 standard,
within a string literal a // should not be interpreted
as such.

| First, is the compiler correct?
| Second, is it ok if I change it to this? (it does compile now)
|
| #define MYMACRO "http\/\/www.bla.com"
|
| In other words, can I escape the slash?

Not portably according to the C standard(s).

The following workaround would be standard-compliant:
#define MYMACRO "http:/" "/www.bla.com"

Consecutive string literals are concatenated by the compiler,
so you can split the slashes by splitting the string.

Alternatively, you could also substitute a slash with
its ASCII code (=> not compatible with e.g. EBDIC machines) :
#define MYMACRO "http:\x5C/www.bla.com"

(NB: I inserted a column for proper URL formatting)



hth - Ivan
 
M

Martijn

yes, outlook kep turning it into a hyperlink without quotes so I
didn't know what it would send

First type the URL, then the quotes (I know, it's a little cumbersome)
 
J

Jimmy

Your compiler should be able to generate an output file of just the results
of pre-processor directives and you can see exactly what the #define
substitution looks like.
 
K

Kevin Goodsell

Jimmy said:
> Your compiler should be able to generate an output file of just the results
> of pre-processor directives and you can see exactly what the #define
> substitution looks like.
>

Please don't top-post.

I think the compiler is doing something wrong, but I don't know exactly
what. Odds are, it is not a C99 compiler, and therefore should not care
at all about '//' (which does not indicate a comment in C dialects prior
to C99).

No, you cannot escape a forward slash. '\/' is not a defined escape
sequence.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top