如何让这个宏定义输出的是字符串?

stevenjin 2009-04-20 10:44:30
#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;
#include <iostream>
using namespace std;
#define MYTEXT(str) #str

void main()
{
cout<<MYTEXT(hello world);
getchar();
}
...全文
447 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackyjkchen 2009-04-22
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 Wind_Runner 的回复:]
没有这个限制吧?
至少要分情况,
用define有它的好,用const也一样,各有千秋吧

引用 11 楼 spy109 的回复:
楼上到处都是正解

但是 编程时是不提倡 用#define 的

常量用 const

函数用 inline 去声明

可以避免 诸如楼主的问题
[/Quote]

高质量C++的原话
amossavez 2009-04-22
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 jackyjkchen 的回复:]
引用 13 楼 Wind_Runner 的回复:
没有这个限制吧?
至少要分情况,
用define有它的好,用const也一样,各有千秋吧

引用 11 楼 spy109 的回复:
楼上到处都是正解

但是 编程时是不提倡 用#define 的

常量用 const

函数用 inline 去声明

可以避免 诸如楼主的问题


高质量C++的原话
[/Quote]

呵呵,这都记住了呀!
newborn2012 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 morris88 的回复:]
C/C++ code#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;
#include <iostream>
using namespace std;
#define MYTEXT(str) "\""#str"\""

void main()
{
cout<<MYTEXT(hello world);
getchar();
}
[/Quote]
没错,就是这样子滴。
brookmill 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 morris88 的回复:]
C/C++ code#include "stdafx.h"
#define MYTEXT(str) "\"" #str "\""

void main()
{
cout<<MYTEXT(hello world);
getchar();
}
[/Quote]
这个确实很不错,得学习一下
宏替换之后的结果是: cout<<"\"" "hello world" "\"";
变成了3个独立的字符串。
这种连续的字符串,编译器会把它们连起来作为一个字符串处理,
于是最终结果就是 "\"hello world\""
brookmill 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 stevenjin 的回复:]
#define MYTEXT(str) \" #str \" /*为何不可以这样?*/

void main()
{
cout<<MYTEXT(hello world);
cout<<"hello\"wor\"ld";
getchar();
}
[/Quote]

这样定义,
cout<<MYTEXT(hello world); 宏替换之后变成了这样: cout<<\" #str \";

大概是因为引号的原因吧。宏定义里面如果有一对引号,其间的内容是不做参数替换的,原样拷贝
Wind_Runner 2009-04-21
  • 打赏
  • 举报
回复
没有这个限制吧?
至少要分情况,
用define有它的好,用const也一样,各有千秋吧

[Quote=引用 11 楼 spy109 的回复:]
楼上到处都是正解

但是 编程时是不提倡 用#define 的

常量用 const

函数用 inline 去声明

可以避免 诸如楼主的问题
[/Quote]
wwwypy 2009-04-21
  • 打赏
  • 举报
回复
顶!
术木 2009-04-21
  • 打赏
  • 举报
回复
楼上到处都是正解

但是 编程时是不提倡 用#define 的

常量用 const

函数用 inline 去声明

可以避免 诸如楼主的问题
stevenjin 2009-04-21
  • 打赏
  • 举报
回复
行,
只是不明白,为何不可以这样?

#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;
#include <iostream>
using namespace std;
#define MYTEXT(str) \" #str \" /*为何不可以这样?*/

void main()
{
cout<<MYTEXT(hello world);
cout<<"hello\"wor\"ld";
getchar();
}
liao05050075 2009-04-20
  • 打赏
  • 举报
回复
确实可以
morris88 2009-04-20
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;
#include <iostream>
using namespace std;
#define MYTEXT(str) "\"" #str "\""

void main()
{
cout<<MYTEXT(hello world);
getchar();
}


貌似可以吧...
brookmill 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 stevenjin 的回复:]
也就是现在的宏定义只输出了hello world
现在我要改变一下,输出"hello world"
也就是多了双引号""
[/Quote]
楼主需要一个宏
从 hello world
生成 "\"hello world\""

等高手的答案
stevenjin 2009-04-20
  • 打赏
  • 举报
回复
也就是现在的宏定义只输出了hello world
现在我要改变一下,输出"hello world"
也就是多了双引号""
blh 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 stevenjin 的帖子:]
C/C++ code#include"stdafx.h"#include<iostream>#include<assert.h>usingnamespacestd;
#include<iostream>usingnamespacestd;#defineMYTEXT(str) #strvoidmain()
{
cout<<MYTEXT(hello world);
getchar();
}
[/Quote]


不知道你要问什么?
stevenjin 2009-04-20
  • 打赏
  • 举报
回复
输出的字符串没有""
输出结果是这样
hello world

我要改为输出结果是这样:
"hello world"
wanjingwei 2009-04-20
  • 打赏
  • 举报
回复
你的是对的啊
jackyjkchen 2009-04-20
  • 打赏
  • 举报
回复
楼主不是已经达到目的了么
lingyin55 2009-04-20
  • 打赏
  • 举报
回复
#define MYTEXT(str) printf("%s",str);

调用
cout<<MYTEXT("hello world");

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧