By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

o/p problem

P: n/a
what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC... (i
was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...

Dec 23 '05 #1
Share this Question
Share on Google+
35 Replies


P: n/a
On Fri, 23 Dec 2005 04:33:33 -0800, fidlee wrote:
what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC...
(i was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...


6.4.4.4 "Character constants" #10:
The value of an integer character constant containing more than one
character (e.g., 'ab'), or containing a character or escape sequence that
does not map to a single-byte execution character, is
implementation-defined.

In your case, 43 * 256 + 43.

--
Pieter Droogendijk <pieter at binky dot org dot uk>
PGP/1E92DBBC [ Make way for the Emperor's Finest. ] binky.org.uk

Dec 23 '05 #2

P: n/a
fidlee wrote:
what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC... (i
was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...


Character constants can contain multiple characters, the integer value
of which is implementation-defined. You will need to consult the
documentation that came with your compiler for the details but you
might observe the fact that 43*256+43=11051.

Robert Gamble

Dec 23 '05 #3

P: n/a
@ Robert

so u mean to say that this statement will behave differently on the
different compilers??
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
(windows) i dont think they are compiler dependednt...

Can someone please tell me as to why the compiler behaves in the
following way?? (43*256+43=11051)

Dec 23 '05 #4

P: n/a
* fidlee <fi****@gmail.com> wrote:
what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC... (i
was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...


gcc gives this warning:

multichar.c:5:15: warning: multi-character character constant

Now you should be equipped for a Google search. ;)
Stefan Krah

--
Break original Enigma messages: http://www.bytereef.org/m4_project.html
Dec 23 '05 #5

P: n/a
true that it does give the multi character constant warning... but in
ends up giving the same o/p right ?? :)

Dec 23 '05 #6

P: n/a
On Fri, 23 Dec 2005 05:19:31 -0800, fidlee wrote:
so u mean to say that this statement will behave differently on the
different compilers??
Not quite. Different compilers, architectures, operating systems,
characters sets, etcetera.
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux)
g++ implies a C++ compiler, mind. You probably mean gcc.
and turboC (windows) i dont think they are compiler dependednt...
It might do the same on a whole lot of compilers for Windows and Linux.
Try it on a few more systems, especially if they don't use ASCII.

Or better yet, try it on a mainframe.
Can someone please tell me as to why the compiler behaves in the
following way?? (43*256+43=11051)


Not I. Perhaps your compiler documentation or operating system manual can
help you there.

--
Pieter Droogendijk <pieter at binky dot org dot uk>
PGP/1E92DBBC [ Make way for the Emperor's Finest. ] binky.org.uk

Dec 23 '05 #7

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

fidlee wrote:
@ Robert

so u mean to say that this statement will behave differently on the
different compilers??
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
(windows) i dont think they are compiler dependednt...
Certainly, the results will be compiler dependant. It's just that both
of your compilers give the same results :-)

Consider that the result depends on
- - the width (sizeof()) of a character constant (hint: it isn't
necessarily == 1)
- - the 'endianness' of a character constant (hint: it isn't necessarily
little-endian)
- - the characterset used to express characters in (hint: it isn't always
ASCII)

Think of what the differences might be between your results and the
results of the same program when compiled and run on a system with
- - 32bit integer values
- - big-endian integers
- - EBCDIC-US characterset
Can someone please tell me as to why the compiler behaves in the
following way?? (43*256+43=11051)


Because, on /your/ system, character constants are stored as 16bit
integer values, in the ASCII characterset. Since the ASCII for '+' is
binary 43, then the first '+' (being shifted left into the high-order 8
bits of your 16bit integer) becomes 43*256 (or 43 << 8). The second '+'
(not moving from the low-order position) becomes 43. And the combination
of the two (a 16bit integer, remember?) becomes (45*256)+43

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDq/8+agVFX4UWr64RAnAEAJ95BsHcGCYBqkvNRyHHzggeNFTPZwCe JvP6
ymeBG+DMNGdtX6rSOY+HKVI=
=Ra/t
-----END PGP SIGNATURE-----
Dec 23 '05 #8

P: n/a
@Pieter
thanx for the help offered...
but i still do think that there must be some logic as to y the c
compiler behaves in this fashion...

if someone knows d logic, just let me know ...

Dec 23 '05 #9

P: n/a
[Original context restored]

Please read the document located at http://cfaj.freeshell.org/google/
before posting again to this group.
fidlee wrote:
Robert Gamble wrote:
fidlee wrote:
what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC... (i
was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...
Character constants can contain multiple characters, the integer value
of which is implementation-defined. You will need to consult the
documentation that came with your compiler for the details but you
might observe the fact that 43*256+43=11051.

@ Robert

so u mean to say that this statement will behave differently on the
different compilers??


I mean what I said which was that the value of such character constants
is implementation-defined, it may very well behave differently on
different compilers and you can be sure that not all compilers will
treat them the same way.
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
(windows) i dont think they are compiler dependednt...
First off, g++ is a c++ compiler, C and C++ are different languages,
the latter of which is off-topic here. Secondly, just because two or
more compilers behave in the same manner does not mean that the results
are portable or even Standard conforming, thinking otherwise would be a
serious error.
Can someone please tell me as to why the compiler behaves in the
following way?? (43*256+43=11051)


As I and others have already told you, the Standard declares this to be
implementation-defined, therefore any reasons for why this behavior is
defined in a specific way on an a certain implementation would not be
found in the Standard but in the documentation that came with your
compiler. If your documentation does not provide an explanation to
your liking, ask the implementors for a rationale, not us.

Robert Gamble

Dec 23 '05 #10

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lew Pitcher wrote:
fidlee wrote:
@ Robert

so u mean to say that this statement will behave differently on the
different compilers??
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
(windows) i dont think they are compiler dependednt...

Certainly, the results will be compiler dependant. It's just that both
of your compilers give the same results :-)

Consider that the result depends on
- the width (sizeof()) of a character constant (hint: it isn't
necessarily == 1)
- the 'endianness' of a character constant (hint: it isn't necessarily
little-endian)
- the characterset used to express characters in (hint: it isn't always
ASCII)

Think of what the differences might be between your results and the
results of the same program when compiled and run on a system with
- 32bit integer values
- big-endian integers
- EBCDIC-US characterset


For example, here's a test on /my/ system

source code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("The character constant '++' == %d\n",'++');
return EXIT_SUCCESS;
}
execution results:

The character constant '++' == 20046
Of course, this was compiled and executed on an IBM OS/390 operating
system using the OS/390 C V2 R10 compiler (5647A01). OS/390 uses EBCDIC
variant charactersets, and the source code was written using the
EBCDIC-US variant.

[snip]

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDrAJ2agVFX4UWr64RAiFlAJ9Lqz5umtvPU9xWbKkFRy X85PXWZwCgiCYF
GAh416UYHPyruVU5PoEusow=
=RCUr
-----END PGP SIGNATURE-----
Dec 23 '05 #11

P: n/a
* fidlee <fi****@gmail.com> wrote:
true that it does give the multi character constant warning... but in
ends up giving the same o/p right ?? :)


My point (which you didn't quote) was that your question could possibly
be answered by googling for "multi-character character constant".
Stefan Krah

--
How to quote using Google Groups: http://cfaj.freeshell.org/google/
Break original Enigma messages: http://www.bytereef.org/m4_project.html
Dec 23 '05 #12

P: n/a
@Lew Pitcher
thanx for your answer... but the reason y i am getting confused is
this:
char a='i';
printf("%d",a);

the following code gives the output as 1

whereas
printf("%d",'i')

gives the output as 2

arent both character constants here??

why does the compiler react differently??

this may be a simple question for you guys but i have been tryin hard
to understand...
so please dont lose your temper :)

Dec 23 '05 #13

P: n/a
fidlee wrote:
@Pieter
thanx for the help offered...
but i still do think that there must be some logic as to y the c
compiler behaves in this fashion...

if someone knows d logic, just let me know ...


I is spelled capitalized. I know of no c compiler called y, thus
cannot explain its aberrant and unspecified behaviour. I also
never heard of d logic. Is this something like so-called female logic?

Believe it or not, there is a reason that Usenet protocols have
evolved over the past thirty plus years, and that these include
adequate context and spelling.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Dec 23 '05 #14

P: n/a
@Chuck F

I am really sorry for all the spelling mistakes. I will make sure that
there are no more of them from now on.
By the way it seems that you are the guardian of the 'Usenet messaging
standards'
Because I have seen similar comments from you on many other posts.

Have you ever posted a message that has proved helpful to someone ? :))

Dec 23 '05 #15

P: n/a
On 23 Dec 2005 04:33:33 -0800, "fidlee" <fi****@gmail.com> wrote:
what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC... (i
was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...

It is implementation dependent, although it is legal.

In your case, b ut not guaranteed to be portable, you have got an int
composed of two bytes with value '+' (43), thta is: 257*43=11051

Don't count on it for other compilers/platforms

Regards,

Zara

Dec 23 '05 #16

P: n/a
fidlee wrote:
@Lew Pitcher
thanx for your answer... but the reason y i am getting confused is
this:
char a='i';
printf("%d",a);

the following code gives the output as 1

whereas
printf("%d",'i')

gives the output as 2

arent both character constants here??


Yes, 'i' is a character constant in both cases, but it has type int. In
the first case you are assigning it to a type char, possibly signed
(implementation-defined), then converting the type char to int. In the
second case, you are not converting to type char. If your char type is
signed and (char)'i' is negative (depends on the character set and
number of bits in a byte), you can get different values printed.

In your situation, I suspect that there are other reasons that are not
visible in the snippets you posted.

--
Thad

Dec 23 '05 #17

P: n/a
fidlee wrote:
@Chuck F

I am really sorry for all the spelling mistakes. I will make sure that
there are no more of them from now on.
In general we don't mind spelling mistakes or poor grammar since we
recognise that posting in a foreign language can be difficult. However,
since most languages have similar capitalisation rules that in general
is not a good reason for not using any capitalisation. Also,
abbreviating words to single characters makes it hard to read posts, and
why should many people have to work to read a post when the author for
negligible extra effort can write it out in full?
By the way it seems that you are the guardian of the 'Usenet messaging
standards'
He is not, but *many* of us agree with Chuck. Since you are using Google
Groups read http://cfaj.freeshell.org/google/
Because I have seen similar comments from you on many other posts.
That is because you have been making it hard for people to understand
your posts and the instructions are posted here regularly, so you should
have seen them before you first posted.
Have you ever posted a message that has proved helpful to someone ? :))


Chuck has posted many helpful posts, and that includes the posts telling
people how to post properly. :-)
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 23 '05 #18

P: n/a
"fidlee" <fi****@gmail.com> writes:
@Lew Pitcher
thanx for your answer... but the reason y i am getting confused is
this:
char a='i';
printf("%d",a);

the following code gives the output as 1
What do you mean by "following"?
whereas
printf("%d",'i')

gives the output as 2

arent both character constants here??

why does the compiler react differently??


Are you sure about that? Please show us a small, complete, compilable
program that demonstrates this. On my system, the following program:

#include <stdio.h>
int main(void)
{
char a='i';
printf("%d\n", a);
printf("%d\n", 'i');
return 0;
}

produces the following output:

105
105

If your system uses ASCII or an ASCII-like character set (as it almost
certainly does), the output of both printf statements should be 105.
Regardless of the character set, the output should the the same for
both statements.

And please read <http://cfaj.freeshell.org/google/> and follow its
advice.

--
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.
Dec 23 '05 #19

P: n/a
fidlee wrote:
@ Robert

so u mean to say that this statement will behave differently on the
different compilers??
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
(windows) i dont think they are compiler dependednt...

Can someone please tell me as to why the compiler behaves in the
following way?? (43*256+43=11051)

Naturally..
'+'
00000000 00000000 00000000 00101011
43
'++'
00000000 00000000 00101011 00101011
11051
'+++'
00000000 00101011 00101011 00101011
2829099
'++++'
00101011 00101011 00101011 00101011
724249387

...don't you think?
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 23 '05 #20

P: n/a
On 23 Dec 2005 06:03:06 -0800, in comp.lang.c , "fidlee"
<fi****@gmail.com> wrote:
@Pieter
thanx for the help offered...
firstly, can you please read this: Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
<but i still do think that there must be some logic as to y the c
compiler behaves in this fashion...
Secondly,. please do not use abbreviations in posts, such as y for
why, d for the. Your posts will be easier to read if you avoid
unnecessary abbreviations.
if someone knows d logic, just let me know ...


There *is* no single logic, it depends on your compiler, operating
system, character set (anyone tried it on a non-ISO charset?), etc
etc.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 24 '05 #21

P: n/a
On 23 Dec 2005 06:45:51 -0800, in comp.lang.c , "fidlee"
<fi****@gmail.com> wrote:
By the way it seems that you are the guardian of the 'Usenet messaging
standards'
No, he's just a responsible citizen.
Because I have seen similar comments from you on many other posts.
And yet you still don't follow his advice. Strange...
Have you ever posted a message that has proved helpful to someone ? :))


He's already posted advice that will be helpful to you. For instance
if you continue to ignore the comment about google posting, people
will start ignoring your posts entirely.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 24 '05 #22

P: n/a

Keith Thompson wrote:
"fidlee" <fi****@gmail.com> writes:
@Lew Pitcher
thanx for your answer... but the reason y i am getting confused is
this:
char a='i';
printf("%d",a);

the following code gives the output as 1


What do you mean by "following"?
whereas
printf("%d",'i')

gives the output as 2

arent both character constants here??

why does the compiler react differently??


Are you sure about that? Please show us a small, complete, compilable
program that demonstrates this. On my system, the following program:

#include <stdio.h>
int main(void)
{
char a='i';
printf("%d\n", a);
printf("%d\n", 'i');
return 0;
}

produces the following output:

105
105

If your system uses ASCII or an ASCII-like character set (as it almost
certainly does), the output of both printf statements should be 105.
Regardless of the character set, the output should the the same for
both statements.

sorry. my mistake.
it was actually
1)
char a='i';
printf("%d",sizeof(a));

2)
char a='i';
printf("%d",sizeof('i'));

this problem comes up in turbo C

Dec 24 '05 #23

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

fidlee wrote:
[snip]
sorry. my mistake.
it was actually
1)
char a='i';
You defined [a] as a char
You initialized [a] from a character constant.
This is roughly the same as saying
char a = 43;
in that 43 is an integer, and 'i' is an integer.
But, you can still make the assignment, with a loss of precision.
printf("%d",sizeof(a));
By definition sizeof(char) is 1
So, sizeof(a) is 1

2)
char a='i';
printf("%d",sizeof('i'));


Here, you take the sizeof of a character constant.
As I said before, character constants (the things in single quotes) are
/integers/ not characters.
Apparently, on your platform, integers are 2 characters wide, so
sizeof('i')
like
sizeof(292)
will return the size of an integer constant (2).

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDrNRnagVFX4UWr64RArxwAKCUTHBJf9wM3c9Z2+S73L 7s5fyd0gCgmb92
89A/GHd3MfqY1OkCzxn4hmM=
=nRIN
-----END PGP SIGNATURE-----
Dec 24 '05 #24

P: n/a
fidlee wrote:
Keith Thompson wrote:

.... snip ...

Are you sure about that? Please show us a small, complete,
compilable program that demonstrates this. On my system, the
following program:

#include <stdio.h>
int main(void)
{
char a='i';
printf("%d\n", a);
printf("%d\n", 'i');
return 0;
}

produces the following output:

105
105

If your system uses ASCII or an ASCII-like character set (as
it almost certainly does), the output of both printf
statements should be 105. Regardless of the character set, the
output should the the same for both statements.

sorry. my mistake.
it was actually
1)
char a='i';
printf("%d",sizeof(a));

2)
char a='i';
printf("%d",sizeof('i'));

this problem comes up in turbo C


Congratulations on learning to quote, and to avoid silly
abbreviations. To avoid mistakes you should also always cut and
paste your code samples (from compilable source). This avoids
silly typos invalidating any conclusions.

In this newsgroup we deal only with ISO standard C. Thus the fact
that you are using Turbo C does not matter. What does matter is
that you are using only features of standard C, and no extensions
such as Turbos <conio.h> That way we are all talking about the
same language.

The differences you are seeing in your code are because 'i' is an
integer constant, and thus its size is that of an int. However it
can be stored in a char (the int is sufficiently small), and the
sizeof a char is 1 by definition.

These relative sizes, for your system, are spelled out in the
include file <limits.h> Thus your code can always adapt to the
actual situation.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Dec 24 '05 #25

P: n/a
Congratulations on learning to quote, and to avoid silly
abbreviations. To avoid mistakes you should also always cut and
paste your code samples (from compilable source). This avoids
silly typos invalidating any conclusions.


thanx a lot for the advice. I am really sorry i hadnt realised that
typing mistakes could be confusing. because I post messages from
Google Groups.

Dec 24 '05 #26

P: n/a
"fidlee" <fi****@gmail.com> writes:
[...]
sorry. my mistake.
it was actually
1)
char a='i';
printf("%d",sizeof(a));

2)
char a='i';
printf("%d",sizeof('i'));

this problem comes up in turbo C


This demonstrates why it's important to post the actual code that you
compiled and ran. Don't try to summarize it; cut-and-paste it.
Otherwise, we can't tell which problems are in your actual code and
which were introduced when you posted it.

The "%d" format expects an int. The sizeof operator yields a value of
type size_t. It's likely to work if int and size_t happen to be the
same size, but you should make sure the types match:

printf("%d", (int)sizeof(a));
printf("%d", (int)sizeof('i'));

Finally, this isn't a problem; it's a feature. See question 8.9 in
the comp.lang.c FAQ.

--
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.
Dec 24 '05 #27

P: n/a
fidlee a écrit :
what should be the ouptut of
printf("%d",'++');
Undefined behavour. The a character constant only have 1 character in
conforming mode.
the ascii value of + is 43... the output comes as 11051 in turboC... (i
was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...

Get a better compiler, or learn how to use it properly.

Compiling: main.c
main.c:9:17: warning: multi-character character constant

In addition, you should add a '\n' to the output string to be sure it
will be displayed in time.

--
A+

Emmanuel Delahaye
Dec 24 '05 #28

P: n/a
On Sat, 24 Dec 2005 16:28:54 +0100, in comp.lang.c , Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote:
fidlee a écrit :
what should be the ouptut of
printf("%d",'++');


Undefined behavour. The a character constant only have 1 character in
conforming mode.


I think you've made a mistake there. A character constant is defined
thus (6.4.4.4):

"An integer character constant is a sequence of one or more multibyte
characters enclosed in single-quotes, as in 'x'."

and further

"The value of an integer character constant containing more than one
character (e.g., 'ab'), or containing a character or escape sequence
that does not map to a single-byte execution character, is
implementation-defined."
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 24 '05 #29

P: n/a
Lew Pitcher <lp******@sympatico.ca> writes:
[...]
2)
char a='i';
printf("%d",sizeof('i'));


Here, you take the sizeof of a character constant.
As I said before, character constants (the things in single quotes) are
/integers/ not characters.
Apparently, on your platform, integers are 2 characters wide, so
sizeof('i')
like
sizeof(292)
will return the size of an integer constant (2).


You missed an important distinction. A character constant isn't just
an integer; it's specifically of type int. Strictly speaking, there's
no such thing as "the size of an integer constant"; an integer
constant is a source code construct such as 42 or 0x137LL. The result
of sizeof('i') or sizeof 'i' is the same as sizeof(int).

--
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.
Dec 24 '05 #30

P: n/a
Lew Pitcher <Le*********@td.com> writes:
- the width (sizeof()) of a character constant (hint: it isn't
necessarily == 1)


in fact, it is guaranteed to be at least 2.

DES
--
Dag-Erling Smørgrav - de*@des.no
Dec 30 '05 #31

P: n/a
de*@des.no (Dag-Erling Smørgrav) writes:
Lew Pitcher <Le*********@td.com> writes:
- the width (sizeof()) of a character constant (hint: it isn't
necessarily == 1)


in fact, it is guaranteed to be at least 2.


No, it isn't; consider CHAR_BIT==16, sizeof(int)==1.

--
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.
Dec 30 '05 #32

P: n/a
Dag-Erling Smørgrav wrote:
Lew Pitcher <Le*********@td.com> writes:
- the width (sizeof()) of a character constant (hint: it isn't
necessarily == 1)


in fact, it is guaranteed to be at least 2.


Not it isn't. A system with CHAR_BIT==16 and sizeof(int)==1 is perfectly
valid, and I've worked on embedded systems like this.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 30 '05 #33

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
de*@des.no (Dag-Erling Smørgrav) writes:
Lew Pitcher <Le*********@td.com> writes:
- the width (sizeof()) of a character constant (hint: it isn't
necessarily == 1)


in fact, it is guaranteed to be at least 2.


No, it isn't; consider CHAR_BIT==16, sizeof(int)==1.


Or how about a system on an 8 bit processor?
(I'm not sure about this - does the standard require ints to be at least 16
bits?)

Dec 30 '05 #34

P: n/a
Kenny McCormack said:
(I'm not sure about this - does the standard require ints to be at least
16 bits?)


Yes.

--
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)
Dec 31 '05 #35

P: n/a
On Fri, 23 Dec 2005 11:54:45 -0700, Thad Smith <Th*******@acm.org>
wrote:
fidlee wrote:
char a='i';
printf("%d",a); printf("%d",'i') arent both character constants here??


Yes, 'i' is a character constant in both cases, but it has type int. In
the first case you are assigning it to a type char, possibly signed
(implementation-defined), then converting the type char to int. In the
second case, you are not converting to type char. If your char type is
signed and (char)'i' is negative (depends on the character set and
number of bits in a byte), you can get different values printed.

True for characters in general, but the characters required by the
standard in the basic execution character set, including a-z A-Z 0-9,
must have positive values when stored in (plain) char, whether that
"is" signed or unsigned. (Except zero for the null character.)
In your situation, I suspect that there are other reasons that are not
visible in the snippets you posted.


That did in fact turn out to be the case elsethread.
- David.Thompson1 at worldnet.att.net
Jan 4 '06 #36

This discussion thread is closed

Replies have been disabled for this discussion.