标题:printf("%s and %s", a, b) 为何无法输出b的值?
取消只看楼主
Ted_Ming
Rank: 2
等 级:论坛游民
帖 子:15
专家分:35
注 册:2016-11-2
结帖率:100%
已结贴  问题点数:20 回复次数:1 
printf("%s and %s", a, b) 为何无法输出b的值?
// input: hello world
// output:
//     hello
//     world
//     hello and Hi
//     hello and
int main() {
    char *a, *b;

    scanf("%s %s", a, b);

    printf("%s\n", a);
    printf("%s\n", b);
    printf("%s and %s\n", a, "Hi");   
    printf("%s and %s\n", a, b);
}

// output:
//     hello
//     world
//     hello and Hi
//     hello and world
int main() {
    char *a = "hello";
    char *b = "world";

    printf("%s\n", a);
    printf("%s\n", b);
    printf("%s and %s\n", a, "Hi");   
    printf("%s and %s\n", a, b);
}

我想知道为何第一段的   printf("%s and %s\n", a, b); 输出结果 不是 "hello and world", 而是 "hello and"
搜索更多相关主题的帖子: hello world 
2016-11-09 15:57
Ted_Ming
Rank: 2
等 级:论坛游民
帖 子:15
专家分:35
注 册:2016-11-2
得分:0 
感谢大家,确实是指针的问题。程序在 Mac 上 是跑不起来的。window上的行为我想应该就如8楼所说的不可预知。
2016-11-10 15:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-470945-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 2.135716 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved