public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Signedness of char and puts()
@ 2008-03-28 20:04 Ed Stafford
  2008-03-28 23:25 ` Eljay Love-Jensen
  0 siblings, 1 reply; 12+ messages in thread
From: Ed Stafford @ 2008-03-28 20:04 UTC (permalink / raw)
  To: gcc-help

The following code produces 2 warnings with gcc 4.1.1:

signed char * mystring = "Hello";                       
puts (mystring);

warning: pointer targets in initialization differ in signedness
warning: pointer targets in passing argument 1 of 'puts' differ in signedness

Modifying the code as shown below corrects the problem:

char * mystring = "Hello";

The same warnings occur is the char is declared unsigned.

Does anyone know why this occurs?  

Thank you.            

 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Signedness of char and puts()
  2008-03-28 20:04 Signedness of char and puts() Ed Stafford
@ 2008-03-28 23:25 ` Eljay Love-Jensen
  2008-03-29  1:04   ` me22
                     ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Eljay Love-Jensen @ 2008-03-28 23:25 UTC (permalink / raw)
  To: Ed Stafford, GCC-help

Hi Ed,

> Does anyone know why this occurs? 

Those are three different types.
+ char
+ unsigned char
+ signed char

"Hello" is a char*, not an unsigned char* or signed char*.

(In C++ "Hello" is a char const*.)

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Signedness of char and puts()
  2008-03-28 23:25 ` Eljay Love-Jensen
@ 2008-03-29  1:04   ` me22
  2008-03-29  1:19   ` Matthew Woehlke
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: me22 @ 2008-03-29  1:04 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: Ed Stafford, GCC-help

On Fri, Mar 28, 2008 at 4:13 PM, Eljay Love-Jensen <eljay@adobe.com> wrote:
>
>  > Does anyone know why this occurs?
>
>  "Hello" is a char*, not an unsigned char* or signed char*.
>
>  (In C++ "Hello" is a char const*.)
>

( Actually, to be pedantic, it's a char const [6] in C++.  Normally
this doesn't matter, but someone just yesterday found a bug in
libstdc++ caused by it, so you never know. )

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Signedness of char and puts()
  2008-03-28 23:25 ` Eljay Love-Jensen
  2008-03-29  1:04   ` me22
@ 2008-03-29  1:19   ` Matthew Woehlke
  2008-03-29 12:43     ` Eljay Love-Jensen
  2008-03-31  9:15   ` Michael Eager
  2008-04-17  9:38   ` lynx.abraxas
  3 siblings, 1 reply; 12+ messages in thread
From: Matthew Woehlke @ 2008-03-29  1:19 UTC (permalink / raw)
  To: gcc-help

Eljay Love-Jensen wrote:
> Hi Ed,
> 
>> Does anyone know why this occurs? 
> 
> Those are three different types.
> + char
> + unsigned char
> + signed char
> 
> "Hello" is a char*, not an unsigned char* or signed char*.
> 
> (In C++ "Hello" is a char const*.)

Isn't "Hello" technically const in C also, since -fwrite-strings was 
(thankfully, rightly) killed?

-- 
Matthew
Do you know where your towel is?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Signedness of char and puts()
  2008-03-29  1:19   ` Matthew Woehlke
@ 2008-03-29 12:43     ` Eljay Love-Jensen
  0 siblings, 0 replies; 12+ messages in thread
From: Eljay Love-Jensen @ 2008-03-29 12:43 UTC (permalink / raw)
  To: Matthew Woehlke, GCC-help

Hi Matthew,

> Isn't "Hello" technically const in C also, since -fwrite-strings was
> (thankfully, rightly) killed?

Maybe.  Hopefully.  I have not kept up with the latest developments in C.
(I switched to C++ in 1990, so I didn't get to enjoy the ISO/IEC 9899:1990
era of C standardization.)

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Signedness of char and puts()
  2008-03-28 23:25 ` Eljay Love-Jensen
  2008-03-29  1:04   ` me22
  2008-03-29  1:19   ` Matthew Woehlke
@ 2008-03-31  9:15   ` Michael Eager
  2008-03-31 11:44     ` John Love-Jensen
  2008-04-17  9:38   ` lynx.abraxas
  3 siblings, 1 reply; 12+ messages in thread
From: Michael Eager @ 2008-03-31  9:15 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: Ed Stafford, GCC-help

Eljay Love-Jensen wrote:
> Hi Ed,
> 
>> Does anyone know why this occurs? 
> 
> Those are three different types.
> + char
> + unsigned char
> + signed char
> 
> "Hello" is a char*, not an unsigned char* or signed char*.

In C, char is signed or unsigned, depending on the implementation.
In C++, it is a distinct type.



-- 
Michael Eager	 eager@eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Signedness of char and puts()
  2008-03-31  9:15   ` Michael Eager
@ 2008-03-31 11:44     ` John Love-Jensen
  2008-03-31 14:56       ` Michael Eager
  0 siblings, 1 reply; 12+ messages in thread
From: John Love-Jensen @ 2008-03-31 11:44 UTC (permalink / raw)
  To: Michael Eager; +Cc: Ed Stafford, GCC-help

Hi Michael,

> In C, char is signed or unsigned, depending on the implementation.

Hmmm, on my system, gcc on a C source is treating them as three distinct
types.

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Signedness of char and puts()
  2008-03-31 11:44     ` John Love-Jensen
@ 2008-03-31 14:56       ` Michael Eager
  2008-03-31 15:06         ` Ed Stafford
  0 siblings, 1 reply; 12+ messages in thread
From: Michael Eager @ 2008-03-31 14:56 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: Ed Stafford, GCC-help

John Love-Jensen wrote:
> Hi Michael,
> 
>> In C, char is signed or unsigned, depending on the implementation.
> 
> Hmmm, on my system, gcc on a C source is treating them as three distinct
> types.

This might be either C99 or a gcc extension.
I'm an old-timer -- much more familiar with ANSI/ISO C.

-- 
Michael Eager	 eager@eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Signedness of char and puts()
  2008-03-31 14:56       ` Michael Eager
@ 2008-03-31 15:06         ` Ed Stafford
  2008-03-31 17:28           ` Michael Eager
  0 siblings, 1 reply; 12+ messages in thread
From: Ed Stafford @ 2008-03-31 15:06 UTC (permalink / raw)
  To: Michael Eager, John Love-Jensen; +Cc: GCC-help

Thanks for the replies.

It does seem that gcc is treating them as 3 separate
types. 

The compiler will treat plain char as signed or unsigned
depending on the ABI, but even if the default is for char
to act like unsigned char, it will warn you that char and
unsigned char are of different types.

From the docs:

Which of signed char or unsigned char has the same range, 
representation, and behavior as "plain" char. Determined by ABI. 
The options '-funsigned-char' and '-fsigned-char' change the
default.

It just seems that older versions of gcc didn't do this.

Does it seem I'm on the right track here?

Ed


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Signedness of char and puts()
  2008-03-31 15:06         ` Ed Stafford
@ 2008-03-31 17:28           ` Michael Eager
  0 siblings, 0 replies; 12+ messages in thread
From: Michael Eager @ 2008-03-31 17:28 UTC (permalink / raw)
  To: Ed Stafford; +Cc: John Love-Jensen, GCC-help

Ed Stafford wrote:
> Thanks for the replies.
> 
> It does seem that gcc is treating them as 3 separate
> types. 
> 
> The compiler will treat plain char as signed or unsigned
> depending on the ABI, but even if the default is for char
> to act like unsigned char, it will warn you that char and
> unsigned char are of different types.
> 
>From the docs:
> 
> Which of signed char or unsigned char has the same range, 
> representation, and behavior as "plain" char. Determined by ABI. 
> The options '-funsigned-char' and '-fsigned-char' change the
> default.
> 
> It just seems that older versions of gcc didn't do this.
> 
> Does it seem I'm on the right track here?

Yes.  It appears that you will only get these messages
if you specify -Wall.  I don't see any messages on a
compile without the flag.

-- 
Michael Eager	 eager@eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Signedness of char and puts()
  2008-03-28 23:25 ` Eljay Love-Jensen
                     ` (2 preceding siblings ...)
  2008-03-31  9:15   ` Michael Eager
@ 2008-04-17  9:38   ` lynx.abraxas
  2008-04-17 13:49     ` Eljay Love-Jensen
  3 siblings, 1 reply; 12+ messages in thread
From: lynx.abraxas @ 2008-04-17  9:38 UTC (permalink / raw)
  To: gcc-help

On 28/03/08 15:13:35, Eljay Love-Jensen wrote:
> Hi Ed,
> 
> > Does anyone know why this occurs? 
> 
> Those are three different types.
> + char
> + unsigned char
> + signed char
> 
> "Hello" is a char*, not an unsigned char* or signed char*.
> 
> (In C++ "Hello" is a char const*.)

I wonder when it is improtant to distinguish between char and the signed or
unsigned version?
How should one print the acual value of a character? printf("Character value
of %s is %d.\n",
+"a", 'a')???

Thanks
Lynx

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Signedness of char and puts()
  2008-04-17  9:38   ` lynx.abraxas
@ 2008-04-17 13:49     ` Eljay Love-Jensen
  0 siblings, 0 replies; 12+ messages in thread
From: Eljay Love-Jensen @ 2008-04-17 13:49 UTC (permalink / raw)
  To: lynx.abraxas, GCC-help

Hi Lynx,

> How should one print the actual value of a character?

Any of these will work:

printf("Character value of %s is %d.\n", "a", 'a');
printf("Character value of %s is %d.\n", "a", 'a' & 0xFF);
printf("Character value of %s is %u.\n", "a", 'a');
printf("Character value of %s is %u.\n", "a", 'a' & 0xFF);

Remember that char and short get promoted to int as parameters.

Without the 0xFF, the character could have sign extension, if that's what
you want.  (I assumed 8-bit char.)

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2008-04-17 10:45 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-28 20:04 Signedness of char and puts() Ed Stafford
2008-03-28 23:25 ` Eljay Love-Jensen
2008-03-29  1:04   ` me22
2008-03-29  1:19   ` Matthew Woehlke
2008-03-29 12:43     ` Eljay Love-Jensen
2008-03-31  9:15   ` Michael Eager
2008-03-31 11:44     ` John Love-Jensen
2008-03-31 14:56       ` Michael Eager
2008-03-31 15:06         ` Ed Stafford
2008-03-31 17:28           ` Michael Eager
2008-04-17  9:38   ` lynx.abraxas
2008-04-17 13:49     ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).