473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to print a string of unsigned chars?

Hello,
I have the following string "ADDASSFölhandel ADFS" which is referred to
by a variable whose type declaration is "usigned char *extendedasciistring".

When I print using %s I see the following:"ADDASSF\366lhandel ADFS"
Could someone please tell me how to print this?

Thanks.

Nov 13 '05 #1
3 12939
"RHNewBie" <as**@asdf.com> wrote in message
news:3F**************@asdf.com...
Hello,
I have the following string "ADDASSFölhandel ADFS" which is referred to
by a variable whose type declaration is "usigned char *extendedasciistring".
When I print using %s I see the following:"ADDASSF\366lhandel ADFS"
Interesting. I would expect the exact opposite.
Could someone please tell me how to print this?


To print what? A string containing a character outside the source character
set? I believe it is implementation defined.

However, you raised an interesting point. %s in printf expects a char *. Am
I
allowed to feed it unsigned char * or some such? What possible problems can
I
encounter there? Anyone?
Nov 13 '05 #2
On Fri, 31 Oct 2003 22:02:41 -0000, "Peter Pichler" <pi*****@pobox.sk>
wrote:
<snip>
However, you raised an interesting point. %s in printf expects a char *. Am
I
allowed to feed it unsigned char * or some such? What possible problems can
I
encounter there? Anyone?

Since the usual participants don't seem to want to rerun this debate,
which has been held several times, I will try to summarize.

C90 6.1.2.5 or C99 6.2.5p26 requires that "a pointer to *void* shall
have the same representation and alignment requirements as a pointer
to a character type". Although it isn't explicit, I'm sure that 'a'
is intended to be quantified as 'any', or 'each', not just 'some',
character type, and thus pointers to all three flavors of char must be
implemented the same. (And also to qualified versions thereof, by the
succeeding sentence.) And according to footnote 16 or 39,
"The same representation and alignment requirements are meant to imply
interchangeability as
arguments to functions, return values from functions, and members of
unions."

But footnotes are not normative. Plus it is not explicit if standard
library functions must use the same argument-passing mechanisms or
conventions as user-written functions, and in particular if they must
use (as-if) <stdargs.h> for variadic args which along with
unprototyped args must support this substitution, plus one for
corresponding signed/unsigned integers, explicitly in C99 6.5.2.2.

This is not just theoretical; all standard library functions are
permitted to be also implemented (shadowed) as macros, unless
#undef'ed or used in parens, and it is not too unusual for normal uses
of at least some of them (like memcpy) to be opencoded or otherwise
specially "tweaked" by the compiler. OTOH it must work to put the
address of a stdlib function in a correctly typed pointer and use that
to call it, just like for a user-written function.

So, in brief, it's not absolutely clear this particular case --
unsigned char * versus plain char * for %s -- is guaranteed. But it is
unlikely there is any sensible or even feasible way to meet all the
other things that *are* required without also doing this; and there is
such a large body of existing C code that is careless about pointers
to different flavors of char that an implementation that (perversely)
made them not work, even if arguably legal, would be unacceptable to
many many users. Certainly I have not seen anyone report here such an
implementation, and do not expect to encounter one.

If you really want to *look* for possible problems, you might try IBM
AS/400; I don't *think* its capabilities go down this fine, but ICeBW.
In years past Intel i432 and especially the several LISP machines
might be better bets, but in the unlikely case you can find one of
those still preserved/working, I'd predict its owner/user(s) don't
even *want* a C implementation.

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #3
"Dave Thompson" <da*************@worldnet.att.net> wrote in message
news:4h********************************@4ax.com...
On Fri, 31 Oct 2003 22:02:41 -0000, "Peter Pichler" <pi*****@pobox.sk>
wrote:
However, you raised an interesting point. %s in printf expects a char *. Am I
allowed to feed it unsigned char * or some such? What possible problems can I
encounter there? Anyone?
Since the usual participants don't seem to want to rerun this debate,
which has been held several times, I will try to summarize.


Oh sorry, I didn't know it was discussed here. It is not a FAQ AFAICS,
and I have come back after a long absense, so I guess I just missed it.
C90 6.1.2.5 or C99 6.2.5p26 requires that "a pointer to *void* shall
have the same representation and alignment requirements as a pointer
to a character type".
Yes, I realize this and understand the implications. I was more curious
about printf's *interpretation* of unsigned char * than about the way
printf receives it. I guess it would be the same with %c receiving an
unsigned char instead of char, if it wasn't for standard promotions.
So, in brief, it's not absolutely clear this particular case --
unsigned char * versus plain char * for %s -- is guaranteed. But it is
unlikely there is any sensible or even feasible way to meet all the
other things that *are* required without also doing this; and there is
such a large body of existing C code that is careless about pointers
to different flavors of char that an implementation that (perversely)
made them not work, even if arguably legal, would be unacceptable to
many many users. Certainly I have not seen anyone report here such an
implementation, and do not expect to encounter one.


Yes, the quality of implementation issue. My question was purely
academic,
thinkng it migh be an interesting topic for a discussion if someone got
bored. As I said, sorry about kicking a dead dog (or is it a horse?).

Peter Pichler
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
8
by: 116Rohan | last post by:
I came across a question in one of the Computing olympiad regarding string pattern matching. Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the...
8
by: js | last post by:
HI guys, How do you write Perl's print a ... z, A ... Z, "\n"' in Python In Python?
5
by: Hans Mull | last post by:
Hi! How can I convert a string to a const unsigned char*? (string::c_str() converts the string to a signed char) Thanks in advance, Hans
5
by: tech | last post by:
Hi, I need to parse a string used to represent a time and then populate a simple time struct. The time string will always be this format 23:45.45 ie hours separated from mins by ':' and...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.