468,484 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

what's the effect of uppercase "S" in printf

code below:

#include <stdio.h>

int main(void)
{
char *aaa[]={"a","b","c"};
printf("%S\n",*aaa); /*the uppercase character S*/
getchar();

}

the stdout give me : "abc"
but ,if i change the "%S" to "%s" /*lowercase s*/
it give me "a" only.
there are no info about %S in c std lib reference,
any one can tell me the truth ?

Sep 12 '06 #1
4 15759
marsarden wrote:
code below:

#include <stdio.h>

int main(void)
{
char *aaa[]={"a","b","c"};
printf("%S\n",*aaa); /*the uppercase character S*/
getchar();

}

the stdout give me : "abc"
but ,if i change the "%S" to "%s" /*lowercase s*/
it give me "a" only.
there are no info about %S in c std lib reference,
any one can tell me the truth ?
The S conversion specifier comes from SUSv2 and is
not part of standard C. It has the same meaning as the
ls specifier which is that the argument is of type wchar_t.

I only have a hazy understanding of wide characters so
I can't give you a full explanation why you get output abc
when using the S conversion. The answer may be implementation
specific. It must have something to do with the printf function
interpreting the bytes 'a' , NUL as part of a single wide character
so it doesn't terminate the wide character string there.

Sep 12 '06 #2
"marsarden" <ar*******@gmail.comwrites:
code below:

#include <stdio.h>

int main(void)
{
char *aaa[]={"a","b","c"};
printf("%S\n",*aaa); /*the uppercase character S*/
getchar();

}

the stdout give me : "abc"
but ,if i change the "%S" to "%s" /*lowercase s*/
it give me "a" only.
there are no info about %S in c std lib reference,
any one can tell me the truth ?
There is no "%S" format in standard C. As far as the standard is
concerned, it can do anything.

<OT>
On one system, the documentation for printf() says:

S (Not in C99, but in SUSv2.) Synonym for ls. Don't use.

"%ls" expects a wchar_t* argument, so again, you're getting undefined
behavior.
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 12 '06 #3

"Spiros Bousbouras" <sp****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
marsarden wrote:
code below:

#include <stdio.h>

int main(void)
{
char *aaa[]={"a","b","c"};
printf("%S\n",*aaa); /*the uppercase character S*/
getchar();

}

the stdout give me : "abc"
but ,if i change the "%S" to "%s" /*lowercase s*/
it give me "a" only.
there are no info about %S in c std lib reference,
any one can tell me the truth ?

The S conversion specifier comes from SUSv2 and is
not part of standard C. It has the same meaning as the
ls specifier which is that the argument is of type wchar_t.

I only have a hazy understanding of wide characters so
I can't give you a full explanation why you get output abc
when using the S conversion. The answer may be implementation
specific. It must have something to do with the printf function
interpreting the bytes 'a' , NUL as part of a single wide character
so it doesn't terminate the wide character string there.
References:

GCC:
http://www.gnu.org/software/libc/man...%20Conversions
" %S This is an alias for %ls which is supported for compatibility with the
Unix standard. "

Posix:
http://www.opengroup.org/onlinepubs/...ns/printf.html
1) "S [XSI] Equivalent to ls. "
2) "l (ell)
Specifies that a following d, i, o, u, x, or X conversion specifier applies
to a long or unsigned long argument; that a following n conversion specifier
applies to a pointer to a long argument; that a following c conversion
specifier applies to a wint_t argument; that a following s conversion
specifier applies to a pointer to a wchar_t argument; or has no effect on a
following a, A, e, E, f, F, g, or G conversion specifier. "
Rod Pemberton
Sep 12 '06 #4
marsarden said:
code below:

#include <stdio.h>

int main(void)
{
char *aaa[]={"a","b","c"};
printf("%S\n",*aaa); /*the uppercase character S*/
getchar();

}

the stdout give me : "abc"
The C language does not make any provision for what happens when you tell
printf to expect a format specifier of a kind it understands, and then fail
to provide one. So the behaviour of your program is undefined, and it could
do anything at all, including (but by no means restricted to) what you say
it did do.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Tobiah | last post: by
5 posts views Thread by Jarek | last post: by
1 post views Thread by blue | last post: by
5 posts views Thread by junky_fellow | last post: by
2 posts views Thread by Lasse Edsvik | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.