C++ 在 printf 中使用#define?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3011220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:46:55  来源:igfitidea点击:

Use a #define in printf?

c++stringc-preprocessor

提问by John

I was wanting to use a constant of some kind for the application ID (so I can use it in printf).

我想为应用程序 ID 使用某种常量(所以我可以在 printf 中使用它)。

I had this:

我有这个:

#define _APPID_ "Hello World!"

And then the simple printf, calling it into %s (string). It put this out:

然后是简单的 printf,将其调用为 %s(字符串)。它提出了这个:

simple.cpp:32: error: cannot convert ‘_IO_FILE*' to ‘const char*' for argument ‘1' to ‘int printf(const char*, ...)'

simple.cpp:32: error: cannot convert ‘_IO_FILE*' to ‘const char*' for argument ‘1' to ‘int printf(const char*, ...)'

What would I use to define the application ID to use in printf? I tried:

我将使用什么来定义要在 printf 中使用的应用程序 ID?我试过:

static const char _APPID_[] = "Hello World"`

but it didn't work, same error I think.

但它没有用,我认为同样的错误。

回答by Mark Rushakoff

I'm not sure I understand exactly what you tried... but this works:

我不确定我是否完全理解你的尝试......但这有效:

#include <stdio.h>

#define _APPID_ "Hello world"

int main()
{
    printf("The app id is " _APPID_ "\n");
    /* Output: The app id is Hello world */
    return 0;
}

When presented with two constant strings back to back (i.e. "hello " "world"), the compiler treats them as a single concatenated constant string ("hello world").

当出现两个背靠背的常量字符串时(即"hello " "world"),编译器将它们视为单个连接的常量字符串 ( "hello world")。

That means that in the case of trying to printfa compile-time constant string, you don't needto use printf("%s", _APPID_)(although that should still work).

这意味着在尝试printf编译时常量字符串的情况下,您不需要使用printf("%s", _APPID_)(尽管这应该仍然有效)。

回答by sth

According to the error message, the problem is most likely not caused by the string constant, but by incorrect parameters given to printf().

根据错误信息,问题很可能不是由字符串常量引起的,而是由给printf().

If you want to print to a file, you should use fprintf(), not printf(). If you want to print to the screen, use printf(), but don't give a file handle as its first parameter.

如果要打印到文件,则应使用fprintf(),而不是printf()。如果要打印到屏幕,请使用printf(),但不要将文件句柄作为其第一个参数。

回答by jim mcnamara

In source.h

在 source.h

#ifndef _SOURCE_H
#define SOURCE_H
#ifdef APP_ID
#define WHOAMI printf("%s\n", APP_ID);
#endif
#endif

In your program:

在你的程序中:

#define APP_ID __FILE__
#include "source.h"
int main()
{
    WHOAMI
    return 0;
}

the reason for this is to have a stadnard include file - source.h. __FILE__inside a header file returns the name of the header file, so the APP_ID definition is constrained to live in the C file.

这样做的原因是有一个标准包含文件 - source.h。 __FILE__在头文件中返回头文件的名称,因此 APP_ID 定义被限制在 C 文件中。

If you don't define APP_ID the code won't compile.

如果您不定义 APP_ID 代码将不会编译。

回答by MSalters

_APPID_is a name that's reserved for the implementation. It matches the pattern ^_[A-Z].*

_APPID_是为实现保留的名称。它匹配模式^_[A-Z].*

Rename it to e.g. APP_ID.

将其重命名为例如APP_ID