开发者

'??' is getting converted into '^' in Visual C++. Why is it happening and what is the way out?

'??' gets converted into '^' if I compile mn VC+开发者_Python百科+ program and run it

e.g.

sprintf( ch, "??") 

prints out

^

But if I run the same code in Turbo C/C++, there is no such problem. Why is it happening on VC++ on Windows?


?? alone is not a trigraph, although ??' corresponds to ^.

Perhaps you typed it here different from what you have in code and you can't see the trailing single quote in your string because it's too close to the closing quote.

So in code you probably have:

sprintf( ch, "??'");


Are you sure it was a double-quote and not a single-quote? If it was ??', then you've just encountered a trigraph, which is a "feature" that really should be removed, but isn't due to IBM not migrating to UTF-8 from EBCDIC. (Trigraphs were considered for removal when C++0x was still open for changes, but the move to get trigraphs removed were vehemently blocked by IBM and its representatives at the ISO C++ committee).


The ?? usually sequence starts a trigraph, but the sequence "??" isn't a trigraph so it shouldn't be interpreted as such - maybe there's a bug in the compiler - exactly which version are you using, and what's the exact code (including variable declarations)?

This code prints "??" in several versions of MSVC 6 through VS 2010 as you might expect:

char ch[20];
sprintf( ch, "??");
printf( "%s\n", ch);

But replace the snprintf()line with:

sprintf( ch, "'??'");

and the output becomes "'^" (except in VS 2010).

A quick test shows that VS 2010 disables trigraph support by default (it prints out "'??'" in the 2nd test). In VS 2010 you have to explicitly enable trigraph support using the /Zc:trigraphs option. Nice.

For more details on what trigraphs are, see: Purpose of Trigraph sequences in C++?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜