| re: defining wide-character strings with macros
On Mon, 15 Nov 2004 17:02:08 -0500, Victor Bazarov wrote:
[color=blue]
> Robert Gamble wrote:[color=green]
>> On Mon, 15 Nov 2004 12:11:20 -0800, Dave Ohlsson wrote:
>>
>>[color=darkred]
>>>Hi,
>>>
>>>In ISO C/C++, a string constant prefixed by the letter `L' is a wide
>>>string constant and is of type "array of wchar_t".
>>>
>>>Consider the following C program fragment:
>>>
>>> #include <stdio.h>
>>> int main()
>>> {
>>> const wchar_t* my_string = L"a";
>>> wprintf(my_string);
>>> return 0;
>>> }[/color]
>>
>>
>> Now that your question has been answered, a couple of points:
>>
>> 1. int main (void) is preferred[/color]
>
> Not in C++ it isn't. Watch your cross-postings.[/color]
In the sentence immediately preceding the code being discussed, the
author indicates the code is C, not C++
[color=blue][color=green]
>> 2. you need to include <wchar.h> for wprintf
>> 3. the wprintf statement is better written as:
>> wprintf(L"%ls\n", my_string); (or without the \n)[/color]
>
> How is it better?[/color]
Still talking C here (don't know about C++):
printf(string) is considered very bad practice in C since the string being
passed may not be a valid format string which the printf functions expect
as their first argument.
Additionally, and arguably more significant, is the fact that using the
single argument form with the printf functions presents a serious security
vulnerability resulting in potential buffer overflows if the appropriate
data finds it's way into the string being passed (such as %n).
[color=blue]
> V[/color]
Rob Gamble |