简体   繁体   English

格式字符串周围的“ TEXT”在“ printf”中是什么意思

[英]what does the `TEXT` around the format string mean in “printf”

The following prints the percentage of memory used. 下面显示了已用内存的百分比。

  printf (TEXT("There is  %*ld percent of memory in use.\n"),
            WIDTH, statex.dwMemoryLoad);

WIDTH is defined to be equal to 7. WIDTH定义为等于7。

What does TEXT mean, and where is this sort of syntax defined in printf? TEXT是什么意思,printf中定义的这种语法在哪里?

As others already said, TEXT is probably a macro. 正如其他人已经说过的, TEXT可能是一个宏。

To see what they become, simply look at the preprocessor output. 要查看它们变成了什么,只需查看预处理器输出。 If are using gcc: 如果正在使用gcc:

gcc -E file.c

TEXT() is probably a macro or function which returns a string value. TEXT()可能是返回字符串值的宏或函数。 I think it is user defined and does some manner of formatting on that string which is passed as an argument to the TEXT function. 认为它是用户定义的,并且对作为参数传递给TEXT函数的字符串进行某种格式化。 You should go to the function declaration for TEXT() to see what exactly it does. 您应该转到TEXT()的函数声明,以了解其确切功能。

Just guessing but TEXT is a char* to char* function that takes care of translating a text string for internationalization support. 只是猜测,但是TEXTchar*char*函数,它负责翻译文本字符串以提供国际化支持。

Note that if this is the case then may be you are also required to always use TEXT with a string literal (and not with expressions or variables) to allow an external tool to detect all literals that need translations by a simple scan of the source code. 请注意,如果是这种情况,那么您还必须始终将TEXT与字符串文字(而不是表达式或变量)一起使用,以允许外部工具通过对源代码的简单扫描来检测所有需要翻译的文字。 For example may be you should never write: 例如,也许你永远不应该写:

puts(TEXT(flag ? "Yes" : "No"));

and you should write instead 而你应该写

puts(flag ? TEXT("Yes") : TEXT("No"));

Something that is instead standard but not used very often is the parameteric width of a field: for example in printf("%*i", x, y) the first parameter x is the width used to print the second parameter y as a decimal value. 取而代之的是标准但很少使用的是字段的参数宽度:例如,在printf("%*i", x, y) ,第一个参数x是用于将第二个参数y打印为小数的宽度值。

When used with scanf instead the * special char can be used to specify that you don't want to store the field (ie to "skip" it instead of reading it). 当与scanf使用时, *特殊字符可用于指定您不想存储该字段(即“跳过”它而不是读取它)。

_TEXT() or _T() is a microsoft specific macro. _TEXT()或_T()是Microsoft特定的宏。 This MSDN link says 这个MSDN链接说

To simplify code development for various international markets,  
the Microsoft run-time library provides Microsoft-specific "generic-text" mappings for many data types,  routines, and other objects.  
These mappings are defined in TCHAR.H.  
You can use these name mappings to write generic code that can be compiled for any of the three kinds of character sets:  
ASCII (SBCS), MBCS, or Unicode, depending on a manifest constant you define using a #define statement.  
Generic-text mappings are Microsoft extensions that are not ANSI compatible.  

_TEXT is a macro to make a strings "character set neutral". _TEXT是使字符串“字符集中性”的宏。 For example _T("HELLO"); 例如_T(“ HELLO”);

Characters can either be denoted by 8 bit ANSI standards or the 16 bit Unicode notation. 字符可以用8位ANSI标准或16位Unicode表示法表示。

If you define _TEXT for all strings and define a preprocessor symbol "_UNICODE", all such strings will follow UNICODE encoding. 如果为所有字符串定义_TEXT并定义预处理程序符号“ _UNICODE”,则所有此类字符串将遵循UNICODE编码。 If you don't define _UNICODE, the strings will all be ANSI. 如果不定义_UNICODE,则所有字符串均为ANSI。 Hence the macro _TEXT allows you to have all strings as UNICODE or ANSI. 因此,宏_TEXT允许您将所有字符串都设置为UNICODE或ANSI。 So no need to change every time you change your character set. 因此,无需在每次更改字符集时都进行更改。

TEXT() is a unicode support macro defined in winnt.h . TEXT()winnt.h定义的Unicode支持宏。 If UNICODE is defined then it prepends L to the string making it wide. 如果定义了UNICODE ,则它将L附加在字符串之前,使其变宽。

Also see TEXT vs. _TEXT vs. _T, and UNICODE vs. _UNICODE blog post. 另请参见TEXT vs. _TEXT vs. _T,以及UNICODE vs. _UNICODE博客文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM