如果我执行printf("one \ 0two");会怎样? [英] What happens if I do printf("one\0two");?

查看:186
本文介绍了如果我执行printf("one \ 0two");会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所示,如果我用C ++进行打印,这意味着什么:

As implied by the title, what's meant to get printed if I do this in C++:

printf("one\0two");

?当我这样做时,GCC会给我一个警告,但是Visual Studio可以使用它.他们的工作方式完全不同吗?考虑一下,我希望printf在第一个\ 0处停止,但是显然使用此代码的代码到目前为止在Windows上都可以正常工作,所以我不确定.

? GCC gives me a warning when I do this, but visual studio is fine with it. Do they work differently at all? Thinking about it, I'd expect printf to stop at the first \0, but apparently the code that uses this has been working fine on windows until now, so I'm not sure.

推荐答案

您将获得:

$ a.out
one$

\0为null,并结束字符串.没有换行符.

the \0 is a null and ends the string. No newline.

$ cat try.c
#include <stdio.h>

int
main(){
    printf("one\0two");
    return 0;
}
542 $ gcc try.c
try.c: In function ‘main’:
try.c:5: warning: embedded ‘\0’ in format
543 $ ./a.out
one544 $ 

这篇关于如果我执行printf("one \ 0two");会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆