简体   繁体   English

用C语言将数字打印成字符串

[英]Print digital to string in C language

#include <stdio.h>

#define stringify(s)    tostring(s)
#define tostring(s)     #s

#define MAX_VALUE 65536
#define NUM 64 * 1024

enum {
    MIN_VALUE = 1024,
};

int main(int argc, char *argv[])
{
    const char *max_str = stringify(MAX_VALUE);
    const char *min_str = stringify(MIN_VALUE);
    printf("max = %s, min = %s\n", max_str, min_str);
    return 0;
}

The output is "max = 65536, min = MIN_VALUE num = 1024 * 64" Experts, how can I modify my code to output like this: max = 65536, min = 1024 num = 65536 输出为“ max = 65536,min = MIN_VALUE num = 1024 * 64”专家,我如何修改我的代码以这样输出:max = 65536,min = 1024 num = 65536

Thanks . 谢谢 。

MIN_VALUE is a number. MIN_VALUE是一个数字。 Why do you need to stringify it? 为什么需要对它进行分类?

Just use: 只需使用:

printf("%d\n", MIN_VALUE);

I think you're better off using a function instead of a macro for this, the reason being that macros are only expanded even before compile time, let alone runtime. 我认为您最好还是使用函数而不是宏,原因是宏仅在编译时才展开,更不用说运行时了。

consider this example: 考虑以下示例:

#define stringify(V) #V

#include <stdio.h>

int main()
{
    int x = 5;
    const char *str = stringify(x);
    printf("%s\n", str);
}

after the preprocessor has done it's work, the code will look like this: 在预处理器完成工作之后,代码将如下所示:

#include <stdio.h>

int main()
{
    int x = 5;
    const char *str = "x";
    printf("%s\n", str);
}

that is because all the preprocessor directive # does, is wrap the given parameter in quotes. 那是因为所有预处理指令#都将给定的参数括在引号中。

If you want to have an int to string behaviour that works on constants, enums (cast to int) and integer variables, you could use sprintf : 如果您想让int字符串行为适用于常量,枚举(广播到int)和整数变量,则可以使用sprintf

#include <stdio.h>
#include <stdlib.h>

char *stringify(int x)
{
    /* get the length of the required buffer */
    int len = snprintf(0, 0, "%i", x);
    /* allocate memory */
    char *res = malloc(sizeof(char) * (len + 1));
    /* handle allocation failure */
    if(!res)
        return 0;

    /* convert the int to string */
    snprintf(res, len + 1, "%i", x);

    /* return the result */
    return res;
}

int main()
{
    int x = 5;
    char *str = stringify(x);
    printf("%s\n", str);
    /* we free the memory allocated by malloc */
    free(str);
}

this would be one way you could to this in C. If you want to know more about the functions I used, have a look at: 这将是在C语言中实现此目的的一种方法。如果想进一步了解我使用的功能,请查看:

#define statements are handled by the pre-processor before the compiler gets to see the code so it's basically a text substitution (it's actually a little more intelligent with the use of parameters and such). #define语句在编译器查看代码之前由预处理器处理,因此它基本上是文本替换(实际上,使用参数等会更智能)。

Since stringify(s) is #defined , the preprocessor faithfully does it job. 由于stringify(s)#defined ,因此预处理程序会忠实地执行此工作。

stringify(MAX_VALUE) decays to stringify(65536) since MAX_VALUE is #defined to 65536 , also known at preprocessing. stringify(MAX_VALUE)衰减为stringify(65536)因为将MAX_VALUE定义为65536 (在预处理中也是如此)。

But Enumerations are part of the C language itself and not known at preprocessing, So, stringify(MIN_VALUE) retains as stringify(MIN_VALUE) and hence toString(MIN_VALUE) 但是Enumerations是C语言本身的一部分,在预处理时不知道,因此, stringify(MIN_VALUE)保留为stringify(MIN_VALUE),因此保留为toString(MIN_VALUE)

To do integer arithmetic or to print 做整数算术或打印

num = 65536

"yes", there is a way to make the preprocessor perform integer arithmetic, which is to use it in a preprocessor condition. “是”,有一种方法可以使预处理器执行整数运算,即在preprocessor条件下使用它。

#if 1024*64 == 65536
    printf("num=65536\n");
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM