473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

define a string

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

Nov 14 '05 #1
3 36348
In article <11**********************@o13g2000cwo.googlegroups .com>,
QQ <ju****@yahoo.com> wrote:
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


What difficulty are you having with it? Can you present a -short- program
showing what you are trying to do but which isn't working?
--
Any sufficiently advanced bug is indistinguishable from a feature.
-- Rich Kulawiec
Nov 14 '05 #2
QQ wrote:
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

Nov 14 '05 #3
QQ wrote:
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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
2
by: saurabh | last post by:
solve a problem: define a Class(MyClassSArray a, b, c) which implements an array of string. But, in the backend it is actually implementing Linked List, so that memory is dynamically allocated....
4
by: shapper | last post by:
Hello, I want to define a string value in a short way, if possible, as follows: Weight = "Light" if Count < 10, "Normal" if Count >= 10 and < 20, "Heavy" if Count >= 20 How can I do this?
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.