开发者

What will be the output ? (VIsual C ++ in debug mode)

File:开发者_如何学运维 sub.cpp

// The string to print
char str[] = "Hello World!\n";

File: main.cpp

/************************************************
 * print string -- Print a simple string. *
 ************************************************/
#include <iostream>

extern char *str; // The string to print

int main()
{
    std::cout << str << std::endl;
    return (0);
}


The next time you find yourself somewhere wanting to know what a snippet of C++ code does, but without a compiler handy to test it for yourself, try codepad.org. Here's the output of your sample code:

Line 9: error: conflicting declaration 'char* str' compilation terminated due to -Wfatal-errors.

As Benoit mentioned in a comment, you have declared str in one place as char* and in another as char[]. Those two types are incompatible, and that gives the linker a fit.


Changing the declarations to match will produce the following output, as probably expected:

Hello World!


I'm not sure why you expect that the results will be different in Visual C++, much less in "Debug" mode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜