Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

L in return

2 views
Skip to first unread message

frank

unread,
Nov 18, 2009, 9:02:42 PM11/18/09
to

What's the L doing in here:

#include <stdio.h>
#include <wchar.h>

int my_function(wchar_t *a){
return wprintf(a);
}
int main(){
return my_function(L"hi there\n");
}

// gcc -Wall -Wextra hi1.c -o out

I think I want to rewrite this slightly to address the compiler's
criticism:

dan@dan-desktop:~/source$ gcc -Wall -Wextra hi1.c -o out
hi1.c: In function ‘my_function’:
hi1.c:5: warning: implicit declaration of function ‘wprintf’
dan@dan-desktop:~/source$ ./out
hi there

Am I correct to think that wprintf expects at least 2 args?


--
frank

"Guns: yes, they are harmful."

Seebs

unread,
Nov 18, 2009, 9:05:10 PM11/18/09
to
On 2009-11-19, frank <fr...@example.invalid> wrote:
> What's the L doing in here:

It's not in a return, it's in a string literal.

> int my_function(wchar_t *a){

wchar_t * usually means a wide string

> return my_function(L"hi there\n");

And whaddya know, you've used a wide string.

> dan@dan-desktop:~/source$ gcc -Wall -Wextra hi1.c -o out

> hi1.c: In function ?my_function?:
> hi1.c:5: warning: implicit declaration of function ?wprintf?


> dan@dan-desktop:~/source$ ./out
> hi there
>
> Am I correct to think that wprintf expects at least 2 args?

No, you are not.

Is it REALLY that hard to read the messages? It did not say "warning: wprintf
needs at least 2 args". It said "warning: implicit declaration of
function wprintf." Why do you think it said that? Could it be because
you did not provide a declaration of wprintf? Maybe you should include
the header which is needed to use wprintf.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

frank

unread,
Nov 18, 2009, 9:21:59 PM11/18/09
to
On Thu, 19 Nov 2009 02:05:10 +0000, Seebs wrote:

> On 2009-11-19, frank <fr...@example.invalid> wrote:
>> What's the L doing in here:
>
> It's not in a return, it's in a string literal.
>
>> int my_function(wchar_t *a){
>
> wchar_t * usually means a wide string
>
>> return my_function(L"hi there\n");
>
> And whaddya know, you've used a wide string.
>
>> dan@dan-desktop:~/source$ gcc -Wall -Wextra hi1.c -o out hi1.c: In
>> function ?my_function?:
>> hi1.c:5: warning: implicit declaration of function ?wprintf?
>> dan@dan-desktop:~/source$ ./out
>> hi there
>>
>> Am I correct to think that wprintf expects at least 2 args?
>
> No, you are not.
>
> Is it REALLY that hard to read the messages? It did not say "warning:
> wprintf needs at least 2 args". It said "warning: implicit declaration
> of function wprintf." Why do you think it said that? Could it be
> because you did not provide a declaration of wprintf? Maybe you should
> include the header which is needed to use wprintf.
>
> -s

According to p. 387, H&S V, these are the two #includes you snipped.

Alan Curry

unread,
Nov 18, 2009, 9:39:20 PM11/18/09
to
In article <7mjna2F...@mid.individual.net>,

frank <fr...@example.invalid> wrote:
>#include <stdio.h>
>#include <wchar.h>
>
>int my_function(wchar_t *a){
> return wprintf(a);
>}
>int main(){
> return my_function(L"hi there\n");
>}
>
>// gcc -Wall -Wextra hi1.c -o out
>
>I think I want to rewrite this slightly to address the compiler's
>criticism:
>
>dan@dan-desktop:~/source$ gcc -Wall -Wextra hi1.c -o out
>hi1.c: In function ‘my_function’:
>hi1.c:5: warning: implicit declaration of function ‘wprintf’

The program is fine, but gcc's default mode is too old-fashioned. wprintf was
added in the 199409 ("C95") revision of the language, so you need at least
gcc -std=iso9899:199409 to use it.

--
Alan Curry

Keith Thompson

unread,
Nov 18, 2009, 9:59:10 PM11/18/09
to
frank <fr...@example.invalid> writes:
> What's the L doing in here:
>
> #include <stdio.h>
> #include <wchar.h>
>
> int my_function(wchar_t *a){
> return wprintf(a);
> }
> int main(){
> return my_function(L"hi there\n");
> }
>
> // gcc -Wall -Wextra hi1.c -o out
>
> I think I want to rewrite this slightly to address the compiler's
> criticism:
>
> dan@dan-desktop:~/source$ gcc -Wall -Wextra hi1.c -o out
> hi1.c: In function ‘my_function’:
> hi1.c:5: warning: implicit declaration of function ‘wprintf’
> dan@dan-desktop:~/source$ ./out
> hi there

Apparently gcc combined with glibc hides the declaration of wprintf
by default. You can make it visible with "-std=iso9899:199409" or
"-std=c99".

> Am I correct to think that wprintf expects at least 2 args?

No.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson

unread,
Nov 18, 2009, 10:01:45 PM11/18/09
to
Seebs <usenet...@seebs.net> writes:
> On 2009-11-19, frank <fr...@example.invalid> wrote:
>> What's the L doing in here:
>
> It's not in a return, it's in a string literal.

Which is in a return statement.

>> int my_function(wchar_t *a){
>
> wchar_t * usually means a wide string
>
>> return my_function(L"hi there\n");
>
> And whaddya know, you've used a wide string.

It's understandable that someone who hadn't encountered the L"..."
syntax before wouldn't realize that the L is part of the string
literal.

>> dan@dan-desktop:~/source$ gcc -Wall -Wextra hi1.c -o out
>> hi1.c: In function ?my_function?:
>> hi1.c:5: warning: implicit declaration of function ?wprintf?
>> dan@dan-desktop:~/source$ ./out
>> hi there
>>
>> Am I correct to think that wprintf expects at least 2 args?
>
> No, you are not.
>
> Is it REALLY that hard to read the messages? It did not say "warning: wprintf
> needs at least 2 args". It said "warning: implicit declaration of
> function wprintf." Why do you think it said that? Could it be because
> you did not provide a declaration of wprintf? Maybe you should include
> the header which is needed to use wprintf.

He did. It's declared in <wchar.h>. The problem is that he needs to
invoke gcc with the right -std=... option to make it visible.

frank

unread,
Nov 18, 2009, 10:51:12 PM11/18/09
to

Alright, thx alan. There are some strange things happening with gcc that
I'm gonna have to work around. This switch fixes the problem with
widechar, but it now sees my c99 comments as errors!

-std=c99 works. It's time for me to go face my public as a karaoke
star. Cheers,

frank

unread,
Nov 18, 2009, 10:58:07 PM11/18/09
to
On Wed, 18 Nov 2009 18:59:10 -0800, Keith Thompson wrote:

>> Am I correct to think that wprintf expects at least 2 args?
>
> No.

int wprintf(const wchar_t * restrict format, ...);

Isn't the first argument supposed to look like " %s \n" and then the
second to be pointer to the text literal?

Seebs

unread,
Nov 18, 2009, 11:18:49 PM11/18/09
to
On 2009-11-19, frank <fr...@example.invalid> wrote:
> According to p. 387, H&S V, these are the two #includes you snipped.

I have never seen that error occur if <wchar.h> was included.

Seebs

unread,
Nov 18, 2009, 11:20:13 PM11/18/09
to
On 2009-11-19, Keith Thompson <ks...@mib.org> wrote:
> He did. It's declared in <wchar.h>. The problem is that he needs to
> invoke gcc with the right -std=... option to make it visible.

Oh!

Whoops, sorry frank. I missed the header when looking at the code
originally, I guess. Sorry about that!

Seebs

unread,
Nov 18, 2009, 11:21:12 PM11/18/09
to
On 2009-11-19, frank <fr...@example.invalid> wrote:
> int wprintf(const wchar_t * restrict format, ...);

> Isn't the first argument supposed to look like " %s \n" and then the
> second to be pointer to the text literal?

No.

"..." means "and possibly more arguments". Whether or not there are ANY
arguments corresponding to "..." depends.

wprintf(L"hello\n");

is fine because the string doesn't contain any format characters. But
if you did
wprintf(L"hello, %s\n");
that would be an error because you used a %s but didn't provide a
corresponding argument.

Keith Thompson

unread,
Nov 18, 2009, 11:31:32 PM11/18/09
to
frank <fr...@example.invalid> writes:
> On Wed, 18 Nov 2009 18:59:10 -0800, Keith Thompson wrote:
>>> Am I correct to think that wprintf expects at least 2 args?
>>
>> No.
>
> int wprintf(const wchar_t * restrict format, ...);
>
> Isn't the first argument supposed to look like " %s \n" and then the
> second to be pointer to the text literal?

Just as with printf, the first argument is the format string, which
specifies the number and type(s) of any following arguments. If the
format string contains no '%' characters (or if any '%' characters are
doubled), then the format string is (or should be) the only argument.

wprintf(L"One argument\n");
wprintf(L"Two arguments: %s\n", s1);
wprintf(L"Three arguments: %s %s\n", s1, s2);
wprintf(L"Four arguments: %s%s %s %s\n", s1, s2, s3);

Keith Thompson

unread,
Nov 18, 2009, 11:32:55 PM11/18/09
to
Seebs <usenet...@seebs.net> writes:
> On 2009-11-19, frank <fr...@example.invalid> wrote:
>> According to p. 387, H&S V, these are the two #includes you snipped.
>
> I have never seen that error occur if <wchar.h> was included.

I have. Try it with "gcc -Wall -Wextra".

In the glibc version of <wchar.h>, the declaration of wprintf is
#ifdef'ed out unless the C version is specified to be at least C95,
which it isn't by default.

(Without the warning options, the declaration is missing but there's
no warning on the call.)

Seebs

unread,
Nov 18, 2009, 11:33:46 PM11/18/09
to
On 2009-11-19, Keith Thompson <ks...@mib.org> wrote:
> In the glibc version of <wchar.h>, the declaration of wprintf is
> #ifdef'ed out unless the C version is specified to be at least C95,
> which it isn't by default.

Ohhhhh.

> (Without the warning options, the declaration is missing but there's
> no warning on the call.)

Wacky.

Richard

unread,
Nov 19, 2009, 12:02:12 AM11/19/09
to
Seebs <usenet...@seebs.net> writes:

> On 2009-11-19, Keith Thompson <ks...@mib.org> wrote:
>> He did. It's declared in <wchar.h>. The problem is that he needs to
>> invoke gcc with the right -std=... option to make it visible.
>
> Oh!
>
> Whoops, sorry frank. I missed the header when looking at the code
> originally, I guess. Sorry about that!
>
> -s

NO. You wanted to miss the header. If you missed it then you actively
looked for it by definition. And had you actively looked for it you
could not possibly have missed it.

Basically you could not wait to tut tut and non standard code. Standard
procedure.

--
"Avoid hyperbole at all costs, its the most destructive argument on
the planet" - Mark McIntyre in comp.lang.c

Richard

unread,
Nov 19, 2009, 12:03:38 AM11/19/09
to
Seebs <usenet...@seebs.net> writes:

> On 2009-11-19, Keith Thompson <ks...@mib.org> wrote:
>> In the glibc version of <wchar.h>, the declaration of wprintf is
>> #ifdef'ed out unless the C version is specified to be at least C95,
>> which it isn't by default.
>
> Ohhhhh.
>
>> (Without the warning options, the declaration is missing but there's
>> no warning on the call.)
>
> Wacky.
>
> -s

Indeed. In an earlier post you claimed to have "missed" the
include. Nice back tracking. Not.

frank

unread,
Nov 19, 2009, 1:01:40 PM11/19/09
to
On Wed, 18 Nov 2009 20:31:32 -0800, Keith Thompson wrote:

> frank <fr...@example.invalid> writes:
>> On Wed, 18 Nov 2009 18:59:10 -0800, Keith Thompson wrote:
>>>> Am I correct to think that wprintf expects at least 2 args?
>>>
>>> No.
>>
>> int wprintf(const wchar_t * restrict format, ...);
>>
>> Isn't the first argument supposed to look like " %s \n" and then the
>> second to be pointer to the text literal?
>
> Just as with printf, the first argument is the format string, which
> specifies the number and type(s) of any following arguments. If the
> format string contains no '%' characters (or if any '%' characters are
> doubled), then the format string is (or should be) the only argument.
>
> wprintf(L"One argument\n");
> wprintf(L"Two arguments: %s\n", s1);
> wprintf(L"Three arguments: %s %s\n", s1, s2); wprintf(L"Four
> arguments: %s%s %s %s\n", s1, s2, s3);

I thought this would compile without objection, but not so:

dan@dan-desktop:~/source$ cat hi2.c
#include <stdio.h>
#include <wchar.h>

int my_function(wchar_t *a){
return wprintf("%ls\n", a);
}
int main(){
return my_function(L"hi there");
}

// gcc -std=c99 -Wall -Wextra hi2.c -o out
dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra hi2.c -o out
hi2.c: In function ‘my_function’:
hi2.c:5: warning: passing argument 1 of ‘wprintf’ from incompatible
pointer type
dan@dan-desktop:~/source$

I guess my point is that a is not const wchar_t * restrict format. So
when it's just wprintf(a), then it's different than the specification.

Ben Bacarisse

unread,
Nov 19, 2009, 1:25:45 PM11/19/09
to
frank <fr...@example.invalid> writes:
<snip>

> I thought this would compile without objection, but not so:
>
> dan@dan-desktop:~/source$ cat hi2.c
> #include <stdio.h>
> #include <wchar.h>
>
> int my_function(wchar_t *a){
> return wprintf("%ls\n", a);
> }
> int main(){
> return my_function(L"hi there");
> }
>
> // gcc -std=c99 -Wall -Wextra hi2.c -o out
> dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra hi2.c -o out
> hi2.c: In function ‘my_function’:
> hi2.c:5: warning: passing argument 1 of ‘wprintf’ from incompatible
> pointer type
> dan@dan-desktop:~/source$
>
> I guess my point is that a is not const wchar_t * restrict format. So
> when it's just wprintf(a), then it's different than the
> specification.

The complaint is about argument 1 -- the "%ls\n". You need a wide
string: L"%ls\n".

--
Ben.

Seebs

unread,
Nov 19, 2009, 2:14:32 PM11/19/09
to
On 2009-11-19, frank <fr...@example.invalid> wrote:
> I thought this would compile without objection, but not so:
>
> dan@dan-desktop:~/source$ cat hi2.c
> #include <stdio.h>
> #include <wchar.h>
>
> int my_function(wchar_t *a){
> return wprintf("%ls\n", a);
> }
> int main(){
> return my_function(L"hi there");
> }
>
> // gcc -std=c99 -Wall -Wextra hi2.c -o out
> dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra hi2.c -o out
> hi2.c: In function ?my_function?:
> hi2.c:5: warning: passing argument 1 of ?wprintf? from incompatible
> pointer type
> dan@dan-desktop:~/source$
>
> I guess my point is that a is not const wchar_t * restrict format. So
> when it's just wprintf(a), then it's different than the specification.

No, it's not.

The problem is that the FORMAT STRING ("%ls\n") you used is not a wchar_t *.

Notice: Argument *1*.

frank

unread,
Nov 19, 2009, 3:16:42 PM11/19/09
to
On Thu, 19 Nov 2009 18:25:45 +0000, Ben Bacarisse wrote:

> frank <fr...@example.invalid> writes: <snip>
>> I thought this would compile without objection, but not so:
>>
>> dan@dan-desktop:~/source$ cat hi2.c
>> #include <stdio.h>
>> #include <wchar.h>
>>
>> int my_function(wchar_t *a){
>> return wprintf("%ls\n", a);
>> }
>> int main(){
>> return my_function(L"hi there");
>> }
>>
>> // gcc -std=c99 -Wall -Wextra hi2.c -o out dan@dan-desktop:~/source$
>> gcc -std=c99 -Wall -Wextra hi2.c -o out hi2.c: In function
>> ‘my_function’: hi2.c:5: warning: passing argument 1 of ‘wprintf’ from
>> incompatible pointer type
>> dan@dan-desktop:~/source$
>>
>> I guess my point is that a is not const wchar_t * restrict format. So
>> when it's just wprintf(a), then it's different than the specification.

I wanted to say a few more words about this. I've been trying to wrap my
head around what "const wchar_t * restrict format" is. I encountered it
first in a serious way when I started to work my way through H&S V, where
this is similar to one of the arguments for the functions in the fprintf
family. For fwprintf, for example, it's the second argument.

int my_function(wchar_t *a){
return wprintf(a);
}

Isn't the a here not const and thus not something that could be "const
wchar_t * restrict format"?


>
> The complaint is about argument 1 -- the "%ls\n". You need a wide
> string: L"%ls\n".

Thx, ben. Apparently I need both cap L's:

dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra hi2.c -o out

dan@dan-desktop:~/source$ ./out
hi there

dan@dan-desktop:~/source$ cat hi2.c
#include <stdio.h>
#include <wchar.h>

int my_function(wchar_t *a){
return wprintf( L"%ls\n", a);


}
int main(){
return my_function(L"hi there");
}

// gcc -std=c99 -Wall -Wextra hi2.c -o out
dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra hi2.c -o out

hi2.c: In function ‘main’:
hi2.c:8: warning: passing argument 1 of ‘my_function’ from incompatible
pointer type


dan@dan-desktop:~/source$ cat hi2.c
#include <stdio.h>
#include <wchar.h>

int my_function(wchar_t *a){
return wprintf( L"%ls\n", a);
}
int main(){
return my_function("hi there");
}

// gcc -std=c99 -Wall -Wextra hi2.c -o out
dan@dan-desktop:~/source$

Keith Thompson

unread,
Nov 19, 2009, 4:08:33 PM11/19/09
to
frank <fr...@example.invalid> writes:
[...]

> I wanted to say a few more words about this. I've been trying to wrap my
> head around what "const wchar_t * restrict format" is. I encountered it
> first in a serious way when I started to work my way through H&S V, where
> this is similar to one of the arguments for the functions in the fprintf
> family. For fwprintf, for example, it's the second argument.
>
> int my_function(wchar_t *a){
> return wprintf(a);
> }
>
> Isn't the a here not const and thus not something that could be "const
> wchar_t * restrict format"?
[...]

Let's look at a (perhaps) simpler example.

#include <stdio.h>

void func(char *param)
{
*param = 'H';
puts(param);
}

void const_func(const char *param)
{
#if 0
*param = 'H'; /* illegal, violates "const" promise */
#endif
puts(param);
}

int main(void)
{
char s[] = "hello, world";
char *arg = s;
const char *const_arg = s;

func(arg); /* ok */
#if 0
func(const_arg); /* illegal, violates promise made by */
/* declaration of const_arg */
#endif
const_func(arg); /* ok, doesn't violate any "const" promise */
const_func(const_arg); /* ok */
return 0;
}

Passing a pointer-to-(non-const)-char to a function that takes a
pointer-to-const-char, as in "const_func(arg);" above, is perfectly
valid. The "const" on the parameter declaration is a promise
not to use the parameter to modify what the parameter points to.
The code doesn't try to violate that promise, so there's no problem.

Passing a pointer-to-const to a function that takes a
pointer-to-non-const, on the other hand, is a constraint violation.
The declaration "const char *const_arg;" promises not to use
const_arg to modify the data it points to. You can't pass that
pointer to a function that doesn't make a similar promise. But as
we can see, you can modify the same data through another path.

The above program should compile and run without error. If you change
"#if 0" to "#if 1", the compiler will complain.

frank

unread,
Nov 19, 2009, 5:54:13 PM11/19/09
to
On Thu, 19 Nov 2009 13:08:33 -0800, Keith Thompson wrote:

[snipped and re-ordered for thematic reasons]


> Passing a pointer-to-(non-const)-char to a function that takes a
> pointer-to-const-char, as in "const_func(arg);" above, is perfectly
> valid. The "const" on the parameter declaration is a promise not to use
> the parameter to modify what the parameter points to. The code doesn't
> try to violate that promise, so there's no problem.
>
> Passing a pointer-to-const to a function that takes a
> pointer-to-non-const, on the other hand, is a constraint violation. The
> declaration "const char *const_arg;" promises not to use const_arg to
> modify the data it points to. You can't pass that pointer to a function
> that doesn't make a similar promise. But as we can see, you can modify
> the same data through another path.
>
> The above program should compile and run without error. If you change
> "#if 0" to "#if 1", the compiler will complain.

I'm just about there with this material and this is certainly good source
to get right at the meat of it. This shows when the #ifs are changed:

dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra hi3.c -o out
hi3.c: In function ‘const_func’:
hi3.c:13: error: assignment of read-only location ‘*param’
dan@dan-desktop:~/source$ cat hi3.c

#include <stdio.h>

void func(char *param)
{
*param = 'H';
puts(param);
}

void const_func(const char *param)
{
#if 1


*param = 'H'; /* illegal, violates "const" promise */
#endif
puts(param);
}

int main(void)
{
char s[] = "hello, world";
char *arg = s;
const char *const_arg = s;

func(arg); /* ok */
#if 0
func(const_arg); /* illegal, violates promise made by */
/* declaration of const_arg */
#endif
const_func(arg); /* ok, doesn't violate any "const" promise */
const_func(const_arg); /* ok */
return 0;
}

// gcc -std=c99 -Wall -Wextra hi3.c -o out
dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra hi3.c -o out
hi3.c: In function ‘main’:
hi3.c:26: warning: passing argument 1 of ‘func’ discards qualifiers from
pointer target type
dan@dan-desktop:~/source$ cat hi3.c

#include <stdio.h>

void func(char *param)
{
*param = 'H';
puts(param);
}

void const_func(const char *param)
{
#if 0
*param = 'H'; /* illegal, violates "const" promise */
#endif
puts(param);
}

int main(void)
{
char s[] = "hello, world";
char *arg = s;
const char *const_arg = s;

func(arg); /* ok */
#if 1


func(const_arg); /* illegal, violates promise made by */
/* declaration of const_arg */
#endif
const_func(arg); /* ok, doesn't violate any "const" promise */
const_func(const_arg); /* ok */
return 0;
}

// gcc -std=c99 -Wall -Wextra hi3.c -o out

> Let's look at a (perhaps) simpler example.
>
> #include <stdio.h>
>
> void func(char *param)
> {
> *param = 'H';
> puts(param);
> }

Why don't we see an H in stdout here?

dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra hi4.c -o out
dan@dan-desktop:~/source$ ./out
here in func

Hello, world
Hello, world
Hello, world
dan@dan-desktop:~/source$ cat hi4.c

#include <stdio.h>

void func(char *param)
{
puts("here in func\n");
*param = 'H';
puts(param);
}

void const_func(const char *param)
{
#if 0
*param = 'H'; /* illegal, violates "const" promise */
#endif
puts(param);
}

int main(void)
{
char s[] = "hello, world";
char *arg = s;
const char *const_arg = s;

func(arg); /* ok */
#if 0
func(const_arg); /* illegal, violates promise made by */
/* declaration of const_arg */
#endif
const_func(arg); /* ok, doesn't violate any "const" promise */
const_func(const_arg); /* ok */
return 0;
}

// gcc -std=c99 -Wall -Wextra hi4.c -o out

Keith Thompson

unread,
Nov 19, 2009, 6:01:45 PM11/19/09
to
frank <fr...@example.invalid> writes:
[...]

> Why don't we see an H in stdout here?
>
> dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra hi4.c -o out
> dan@dan-desktop:~/source$ ./out
> here in func
>
> Hello, world
> Hello, world
> Hello, world
> dan@dan-desktop:~/source$ cat hi4.c
[...]

We do, at the beginning of each of the three lines of output.

The array s was initialized to the strong "hello, world".
The assignment
*param = 'H';
changed the first character from 'h' to 'H'; the rest of the string
was untouched.

Ted DeLoggio

unread,
Nov 19, 2009, 7:26:25 PM11/19/09
to


What is H&S V?

--
Ted DeLoggio

Richard Heathfield

unread,
Nov 19, 2009, 7:36:20 PM11/19/09
to
In <x6KdnUbVGbu...@giganews.com>, Ted DeLoggio wrote:

<snip>

> What is H&S V?

Presumably "C: A Reference Manual", 5th Ed. Harbison & Steele,
published by Prentice Hall.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Morris Keesan

unread,
Nov 20, 2009, 1:29:49 PM11/20/09
to
On Wed, 18 Nov 2009 21:21:59 -0500, frank <fr...@example.invalid> wrote:

> On Thu, 19 Nov 2009 02:05:10 +0000, Seebs wrote:

...


>> Maybe you should
>> include the header which is needed to use wprintf.

...


> According to p. 387, H&S V, these are the two #includes you snipped.

If Harbison & Steele really say that you need two #includes to use wprintf,
then they're wrong. According to the standard, all you need for wprintf
is <wchar.h>. You don't need <stdio.h> unless you're using one of the
functions that takes a (FILE *) argument, such as fwprintf.


--
Morris Keesan -- mke...@post.harvard.edu

frank

unread,
Nov 20, 2009, 5:45:35 PM11/20/09
to
On Wed, 18 Nov 2009 20:31:32 -0800, Keith Thompson wrote:

>> int wprintf(const wchar_t * restrict format, ...);
>>
>> Isn't the first argument supposed to look like " %s \n" and then the
>> second to be pointer to the text literal?
>
> Just as with printf, the first argument is the format string, which
> specifies the number and type(s) of any following arguments. If the
> format string contains no '%' characters (or if any '%' characters are
> doubled), then the format string is (or should be) the only argument.
>
> wprintf(L"One argument\n");
> wprintf(L"Two arguments: %s\n", s1);
> wprintf(L"Three arguments: %s %s\n", s1, s2); wprintf(L"Four
> arguments: %s%s %s %s\n", s1, s2, s3);

Am I correct to think that wchar_t *a is compatible with const wchar_t *
restrict format in this context:

#include <stdio.h>
#include <wchar.h>

int my_function(wchar_t *a){
return wprintf(a);
}
int main(){
return my_function(L"hi there\n");
}

// gcc -std=c99 -Wall -Wextra hi1.c -o out

It's const by virtue of looking at the source and seeing nothing that
makes it unconst. The restrict is something that was added in recent
treatments. I have no idea what it's supposed to do.

If it is compatible, then I wonder what else you could throw at wprintf
in its first argument without doing something stupid or illegal.

Ben Bacarisse

unread,
Nov 20, 2009, 7:23:55 PM11/20/09
to
frank <fr...@example.invalid> writes:

> On Wed, 18 Nov 2009 20:31:32 -0800, Keith Thompson wrote:
>
>>> int wprintf(const wchar_t * restrict format, ...);
>>>
>>> Isn't the first argument supposed to look like " %s \n" and then the
>>> second to be pointer to the text literal?
>>
>> Just as with printf, the first argument is the format string, which
>> specifies the number and type(s) of any following arguments. If the
>> format string contains no '%' characters (or if any '%' characters are
>> doubled), then the format string is (or should be) the only argument.
>>
>> wprintf(L"One argument\n");
>> wprintf(L"Two arguments: %s\n", s1);
>> wprintf(L"Three arguments: %s %s\n", s1, s2); wprintf(L"Four
>> arguments: %s%s %s %s\n", s1, s2, s3);
>
> Am I correct to think that wchar_t *a is compatible with const wchar_t *
> restrict format in this context:

Whether two types are compatible or not depends on the types
themselves are does not vary from one context to another. wchar_t *
is not compatible with either const wchar_t * or with const wchar_t *
restrict.

What you are discussing is something else that, sadly, does not have a
name. It is the property that type T1 can be passed as argument to
a function that has a parameter of T2 in that position. This is
closely related to question whether an expression of type T1 can be
assigned to a object of type T2 because parameter passing is defined
in terms of assignment.

Unfortunately neither of these two properties is symmetric, so we have
to careful when inverting a term. Maybe just using "may be assigned
to" and "passed to" will do as a shorthand. In that case, wchar_t *
may be passed to both const wchar_t * and to const wchar_t * restrict.

To take another example, wchar_t * may be passed to const wchar_t *
const but it may no be assigned to that type.

None of these different types are compatible with each other.

<snip>
--
Ben.

0 new messages