繁体   English   中英

ASCII值= 0和'\\ 0'

[英]ASCII value = 0 and '\0'

我看过这篇文章 但当我尝试时:

  printf("before null %c after null\n", 0);  // (ASCII=0) != '\0' ??

而不是得到:

before null 

我有:

before null   after null

所以我的问题是:ASCII值0实际上是否等于'\\ 0'?

ASCII值0实际上等于\\0吗?


字符串如何存储在内存中以及由printf()printf()处理的差异非常重要。

"before null %c after null\n"
"before null \0 after null\n"

两者都存储在内存中,末尾有一个隐式的\\0终止符。 第二个在中间有一个显式\\0字符的事实改变了事情。

printf()将扫描字符串直到“ 结束 ”,打印组件,因为它在C中“ 结束 ”通常意味着直到第一个\\0 / nul字符。

使用第一个变体, printf()字符复制到输出,直到它到达%c指令,此时它会查看给函数的参数...它可能会发现你给了'\\0' ,或者它可能会发现你给了'+' - 无论哪种方式,它都会将它复制到输出中。 然后它会继续将字符复制到输出中,寻找字符串的“ 结束 ”。

使用第二个变体, printf()将开始将字符复制到输出,将找到“ 结束 ”(由\\0表示),然后停止。

如果你要使用snprintf() ,那么结果/输出将包含以下内容:(同样,隐式\\0终止)

"before null \0 after null\n"
"before null "

如果您随后打印这两个,它们看起来会一样,但内存内容会有所不同。

但是, printf()的输出是终端(或文件)... \\0取决于您的终端仿真器...它可能根本不显示,可能显示为空格,或者它可能有一个有趣的盒子符号......

需要注意的重要一点是,这发生在运行时 - 而不是编译时。

原因是printf实际上没有"Hello %s", "World"替换"Hello %s", "World" "Hello World"然后将它们全部打印出来。 相反 ,它按顺序打印"Hello "然后在每个角色独奏中连续打印"Hello " "World"

如果您曾尝试使用putchar()打印单个null字符,您会注意到它会打印一个空格,这就是printf基于它打印空间的原因。

请注意,它不会在其他系统上打印任何内容(例如Linux)。

printf实际工作原理的示例代码。


const char * x;
// while the current char != '\0'
while (*format)
{
    // if the current char == '%'
    if (*format == '%')
    {
        // increment the pointer so we can point to the next char and skip printing '%'
        switch (*(++format)) // then switch that next char (specifier).
        {
        case 'c':
            putchar(va_arg(args, char)); // if the argument is null, then it's putchar(0);
            break;
        case 's':
            // regular operation of printing a string argument.
            x = va_arg(args, const char*);
            while (*x) putchar(*x++);
            break;
        }
        // skips the format specifier so we don't print it (e.g 's', 'c'..)
        *format++;
    }
    // else: isn't a format specfier.
    else
       // print the current char (pointer) of the original string
        putchar(*format++); // increments it for the next operation.
}
va_end(args);

所以回到你的问题,它将打印每个字符,当涉及到0的参数为null时putchar()将根据你的系统放置一个空格或什么都没有。

你可以说printf参数是不是真的有原始字符串任何关系终止的话,他们不知道对方。
就像你在printf("Hello %s, from SO!", "World"); "World"实际上终止于\\0 ,但它将, from SO!终止本身而不是其他, from SO!

是的0'\\0' 他们是同一个角色。

printf不会在用于该字符位置终止printf格式串%c给定的值时, 格式说明符 0'\\0' 相反, nul的终端输出通常是占位符(例如space等)

但是你可以在字符串中插入一个nul ,然后使用%s 格式说明符输出字符串,并看到实际上十进制 0实际上是等效ASCII 字符 '\\0'的ASCII值,并将在该点终止字符串nul-character (参见: www.ASCIItable.com ),例如

#include <stdio.h>

#define FMT "before null %c after null\n"

int main (void) {

    char buf[sizeof FMT * 2];

    puts (FMT);

    sprintf (buf, FMT, 0);
    printf ("using 0 : '%s'", buf);
    putchar ('\n');

    sprintf (buf, FMT, '\0');
    printf ("using \\0: '%s'", buf);
    putchar ('\n');

    return 0;
}

示例使用/输出

$ ./bin/str_printf_null
before null %c after null

using 0 : 'before null '
using \0: 'before null '

暂无
暂无

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

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