define a string

Q

QQ

How to define a string?

Usually we can

#define MAX 30

However if I'd like to define a string const
can I?
#define S "Hello"

seems not working

Thanks
 
T

Thomas Matthews

QQ said:
How to define a string?

Usually we can

#define MAX 30

However if I'd like to define a string const
can I?
#define S "Hello"

seems not working

Thanks

Just remember that the #define is a substitution
that takes place before the translation phase.

So, when using the #define, try getting the syntax
correct first:

/* First try with this syntax */
const char * S = "Hello";

/* Then move it to a #define: */
#define S (const char *)("Hello")

{Although literal strings are constants.}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

Joe Wright

QQ said:
How to define a string?

Usually we can

#define MAX 30

However if I'd like to define a string const
can I?
#define S "Hello"

seems not working

Thanks

#include <stdio.h>
#define S "Hello"
int main(void) {
puts(S);
return 0;
}

It's 'working' here.
 

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
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top