您所在的位置: 首页 > 读书频道 > 设计开发 > 其它开发 >

Unicode and ANSI Functions in the C Run-Time Library

http://book.51cto.com  2008-07-30 16:19  Jeffrey Richter, Christophe Nasarre  Microsoft Press  我要评论(0)
  • 摘要:《Windows核心编程(第5版•英文版)》第二章介绍了字符串操作的相关知识。本文介绍了在C运行期源码被编译成Unicode或ANSI函数的过程。
  • 标签:Unicode  ANSI  函数  C  源码  编译
Unicode and ANSI Functions in the C Run-Time Library

Like the Windows functions, the C run-time library offers one set of functions to manipulate ANSI characters and strings and another set of functions to manipulate Unicode characters and strings. However, unlike Windows, the ANSI functions do the work; they do not translate the strings to Unicode and then call the Unicode version of the functions internally. And, of course, the Unicode versions do the work themselves too; they do not internally call the ANSI versions.

An example of a C run-time function that returns the length of an ANSI string is strlen, and an example of an equivalent C run-time function that returns the length of a Unicode string is wcslen.

Both of these functions are prototyped in String.h. To write source code that can be compiled for either ANSI or Unicode, you must also include TChar.h, which defines the following macro:

   #ifdef _UNICODE
#define _tcslen     wcslen
#else
#define _tcslen     strlen
#endif
Now, in your code, you should call _tcslen. If _UNICODE is defined, it expands to wcslen; otherwise, it expands to strlen. By default, when you create a new C++ project in Visual Studio, _UNICODE is defined (just like UNICODE is defined). The C run-time library always prefixes identifiers that are not part of the C++ standard with underscores, while the Windows team does not do this. So, in your applications you'll want to make sure that both UNICODE and _UNICODE are defined or that neither is defined. Appendix A, "The Build Environment," will describe the details of the CmnHdr.h header file used by all the code samples of this book to avoid this kind of problem.
【责任编辑:阚书 TEL:(010)68476606】

回书目   上一节   下一节
微软 Windows 7 技术前瞻
2008年第6届OpenOffice.org世界开源大会
最优性价比组建无线网络
网络应用性能控管最佳实践
如何有效提升企业安全审计应用水平
 
 验证码: (点击刷新验证码)   匿名发表
  • Linux C编程实战

  • 作者:童永清
  • 本书系统地介绍了在Linux平台下用C语言进行程序开发的过程,集趣味性、实战性于一体的160多段代码实例,帮助读者快速掌握在Linu..
Copyright©2005-2008 51CTO.COM 版权所有