Question related to printf

H

harsha

You seem to understand what is going on, but lack the proper
vocabulary to describe it correctly. Casting takes place via
explicit use of a cast operator, but there is no cast operator
here.

Thank you for correcting..
 
K

Kenneth Brody

Philip said:
somenath wrote: [...]
So C compiler is not going to convert '1' + 1 to 49 + 1 it will
convert '1'+1 as 1+1 . Is my understanding is correct ?

No, '1' (the character) is wholly unrelated to 1 (the integer constant).
The fact that '1'+1 == '2' makes sense mathematically is pure
coincidence. Other mathematical constructs are not likely to work on
numerical characters, for example '1'*2 == '2' is guaranteed to be
false.

I think you mean "is not guaranteed to be true".
The character '1' is never interpreted as the integer 1.

.... in any character set with which you are peronally familiar.

I don't believe that anything in the Standard would forbid such a
character set, as long as '0' through '9' were contiguous.

On second thought, if '1'==1 were true, then '0'==0 and '0'=='\0',
which would be a bad thing. Does the Standard address this?
What pete was telling you concerns the fact that all characters are
represented as integer types (such as char or int), and the %c printf
format specifier actually expects an int (not a char) to supply the
character value. You can still provide a char to printf because char
values are automatically converted to int values in calls to variadic
functions. But you can also provide an int, because this is precisely
what %c expects.

However, these two are not identical, as far as C is concerned:

printf("%c",'1');
and
printf("%c",49);

Yes, the results are the same in ASCII, but that's a feature of the
ASCII character set, and not the C language.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

somenath said:
Because the integer value of '1' is 49 so 49+1 =50 ,character
equivalent of decimal 50 is 2 .
But my doubt is
The type of the expression '1' + 1 is integer. So if I use %c to
print the integer will it show undefined behavior?
[...]

You're missing the very important distinction between "int" and
"integer". The word "int" isn't just an abbreviation for "integer"
(though that's where the word comes from). In C, there are a number
of distinct integer types, including unsigned char, int, long, and
several others. "int" is just one of those types.

Thus it doesn't make sense to say that the type of the expression is
integer. The type of the expression is int, which is an integer type.
 
K

Keith Thompson

Kenneth Brody said:
Philip said:
somenath wrote: [...]
So C compiler is not going to convert '1' + 1 to 49 + 1 it will
convert '1'+1 as 1+1 . Is my understanding is correct ?

No, '1' (the character) is wholly unrelated to 1 (the integer constant).
The fact that '1'+1 == '2' makes sense mathematically is pure
coincidence. Other mathematical constructs are not likely to work on
numerical characters, for example '1'*2 == '2' is guaranteed to be
false.

I think you mean "is not guaranteed to be true".

I thought so as well when I first read that, but no, I think he meant
"is guaranteed to be false", for reasons you address below.
... in any character set with which you are peronally familiar.

I don't believe that anything in the Standard would forbid such a
character set, as long as '0' through '9' were contiguous.

On second thought, if '1'==1 were true, then '0'==0 and '0'=='\0',
which would be a bad thing. Does the Standard address this?

Certainly an implementation with '0' == '\0' would have serious
problems; for one thing, a string could not contain the '0' character
(except as the terminator). Whether the standard specifically forbids
such an implementation is another question.

C99 5.2.1p2 says:

A byte with all bits set to 0, called the _null character_, shall
exist in the basic execution character set; it is used to
terminate a character string.

That section also lists the other members of the basic execution
character set, including the digits '0' .. '9'. One could infer that
all members of the basic execution character set must be distinct, so
'0' != '\0', but it's not *quite* explicitly stated.

C99 6.2.5p3 says:

If a member of the basic execution character set is stored in a
char object, its value is guaranteed to be nonnegative.

which doesn't *by itself* forbid '0' == '\0'.

I believe it's the intent to forbid '0' == '\0', but I haven't (so
far) been able to find explicit wording to that effect in the
standard, either because I haven't searched thoroughly enough or
because the authors thought it was too obvious to bother stating.
 
M

Martien Verbruggen

Philip said:
somenath wrote: [...]
So C compiler is not going to convert '1' + 1 to 49 + 1 it will
convert '1'+1 as 1+1 . Is my understanding is correct ?

No, '1' (the character) is wholly unrelated to 1 (the integer constant).
The fact that '1'+1 == '2' makes sense mathematically is pure
coincidence. Other mathematical constructs are not likely to work on
numerical characters, for example '1'*2 == '2' is guaranteed to be
false.

I think you mean "is not guaranteed to be true".

I think it's guaranteed to be false as well. '0'-'9' have to be
contiguous values. If '1' were 1, then '0' would be 0, which is not
possible, as that value has a spacial meaning. You wouldn't be able to
produce the string "0123456789" if '0' were allowed to be 0.

Martien
 
H

Harald van Dijk

Certainly an implementation with '0' == '\0' would have serious
problems; for one thing, a string could not contain the '0' character
(except as the terminator). Whether the standard specifically forbids
such an implementation is another question.

It did, in 5.2.1.2p1:

-- The null character ('\0') may not be used as part of a multibyte
encoding, except for the one-byte null character itself. This allows
existing functions which manipulate strings to work transparently with
multibyte sequences.

(Single byte characters are also multibyte characters.)

However, this has been changed to:

-- A byte with all bits zero shall be interpreted as a null character
independent of shift state. Such a byte shall not occur as part of any
other multibyte character.

I'm not sure if this still requires '0' to be distinct from '\0', because
if '0' == '\0', then '0' is not any "other" multibyte character.
 
K

Keith Thompson

Harald van Dijk said:
I don't. '1' is allowed to be greater than INT_MAX / 2, in which case the
behaviour is undefined, and the result is allowed to be true, or there
might not be a result at all.

You're a devious man. (That's a compliment, of course.)
 
H

Harald van Dijk

I think it's guaranteed to be false as well.

I don't. '1' is allowed to be greater than INT_MAX / 2, in which case the
behaviour is undefined, and the result is allowed to be true, or there
might not be a result at all.
 
C

CBFalconer

somenath said:
.... snip ...

Now suppose for the following code

#include<stdio.h>

int main(void)
{
printf("%c", '1'+ 1);
return 0;
}

The output of the program is 2 .
....
The type of the expression '1' + 1 is integer. So if I use %c to
print the integer will it show undefined behavior?

The type of the expression '1' is integer. You have nothing but
integer expressions. The %c tells printf to translate the integer
to the appropriate char.
 
C

CBFalconer

Al said:
But Indian English is not the usual language for this newsgroup.
Correcting the OP's usage is a benefit to him in interaction
with his peers in other countries.

But 'doubt' is perfectly understandable to all. I had an English
girlfriend in Montreal some time ago who asked me to 'knock her up'
in the morning. I contrived to translate it to her satisfaction
(not mine).
 
P

Philip Potter

Harald said:
I don't. '1' is allowed to be greater than INT_MAX / 2, in which case the
behaviour is undefined, and the result is allowed to be true, or there
might not be a result at all.

Indeed, I stand very much corrected. :)
 
R

Richard Bos

Mark Bluemel said:
Please don't let's reopen this discussion. As far as Indian English
speakers are concerned, this use of the term "doubt" is perfectly valid.

As far as Washington English speakers are concerned, the use of
"democracy" to mean "Diebold voting machines" is also valid. It behooves
It's really not worth arguing this out each time an Indian posts a
"doubt"/question...

I'm not arguing, I'm simply correcting.

Richard
 
R

Richard

Philip Potter said:
somenath said:
somenath wrote:
I have one question regarding the behavior of printf function.
Characters Argument Type : Printed As
c int; single character
So if I use %c to print the integer will it show undefined behavior?
Your answer is already there.

It's special rules that allow %c
to be used with char type arguments.

N869 6.3.1 Arithmetic operands
6.3.1.1 Boolean, characters, and integers

[#2] The following may be used in an expression wherever an
int or unsigned int may be used:
-- An object or expression with an integer type whose
integer conversion rank is less than the rank of int
and unsigned int.
-- A bit-field of type _Bool, int, signed int, or unsigned
int.
If an int can represent all values of the original type, the
value is converted to an int; otherwise, it is converted to
an unsigned int. These are called the integer
promotions.42) All other types are unchanged by the integer
promotions.

42)The integer promotions are applied only: as part of the
usual arithmetic conversions, to certain argument
expressions, to the operands of the unary +, -, and ~
operators, and to both operands of the shift operators,
as specified by their respective subclauses.

So C compiler is not going to convert '1' + 1 to 49 + 1 it will
convert '1'+1 as 1+1 . Is my understanding is correct ?

No, '1' (the character) is wholly unrelated to 1 (the integer
constant). The fact that '1'+1 == '2' makes sense mathematically is
pure coincidence.

It is not pure coincidence at all. It is specified to do just that.
 
R

Robert Latest

Philip said:
No, '1' (the character) is wholly unrelated to 1 (the integer constant).

'1' is an integer constant, not a character.
The fact that '1'+1 == '2' makes sense mathematically is pure
coincidence.

No, it is mandated by the standard, as is:

'1'+('7'-'5')*2 == '5'

but

'1'+'7'*2-'5'*2 == '5'

isn't.

robert
 
P

Philip Potter

Robert said:
'1' is an integer constant, not a character.

In n1256 it is defined by section 6.4.4.4, "Character constants". Though
it does have type int, it does not stop it representing a character
(and, at the same time, representing an integer value).
No, it is mandated by the standard, as is:

'1'+('7'-'5')*2 == '5'

but

'1'+'7'*2-'5'*2 == '5'

isn't.

I was saying "the fact that it looks like '1' is acting as the number 1
is coincidence" not "the fact that it works at all is coincidence".
Thanks for clearing any confusion.
 
J

Jack Klein

Hi All,

I have one question regarding the behavior of printf function.
In page number of 154 in K&R2 in Table 7-1 it is stated that

Characters Argument Type : Printed As

d,i int, decimal number
c int; single character

Now suppose for the following code

#include<stdio.h>

int main(void)
{
printf("%c", '1'+ 1);
return 0;
}

The output of the program is 2 .

Because the integer value of '1' is 49 so 49+1 =50 ,character
equivalent of decimal 50 is 2 .
But my doubt is
The type of the expression '1' + 1 is integer. So if I use %c to

You are not quite correct here, the type of the expression "'1' + 1"
is indeed an integer, but the term "integer" in C means any one of the
integer types, char, short, int, or long (or even long long under
C99). Including the signed and unsigned versions of each type. They
are all integer types in C.

It would be more correct here to say that the type of the expression
is an int, which of course is one of the integer types.

Perhaps you don't realize it, but the type of a character constant
such as '1' in C is type int, not type char. Even if you don't add
anything to it.

Furthermore, even if you pass a char to printf(), it is automatically
promoted to int in the call.

char ch = '1';
printf("%c", ch);

....causes the value of ch to be read, the char value of '1' to be
converted to an int with the value of '1', and that int value is
passed to printf().
print the integer will it show undefined behavior?
From the K&R2 description i think it will not show undefined behavior.
Is my understanding correct?


Regards,
Somenath

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
D

David Thompson

C99 6.2.5p3 says:

If a member of the basic execution character set is stored in a
char object, its value is guaranteed to be nonnegative.

which doesn't *by itself* forbid '0' == '\0'.
Nit: C99 with (at least) TC1. It's perhaps debatable whether that's
'really' C99 or C01/whatever. As originally published, and in C90, it
said 'positive' which read strictly excludes zero, and thus conflicts
with the requirement that null==0 be in the BECS. However elsewhere
the standard isn't always careful to distinguish math 'positive' (> 0)
and computer 'positive' (sign bit off, >= 0).

End nit. You may now resume caring about things that might conceivably
make some difference to anyone in the real world. <G>

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
J

jameskuyper

Nit: C99 with (at least) TC1. It's perhaps debatable whether that's
'really' C99 or C01/whatever. As originally published, and in C90, it
said 'positive' which read strictly excludes zero, and thus conflicts
with the requirement that null==0 be in the BECS. However elsewhere
the standard isn't always careful to distinguish math 'positive' (> 0)
and computer 'positive' (sign bit off, >= 0).

Do you have an authoritative reference for referring to the sign bit
off states as "positive" rather than "non-negative"? I'm not familiar
with any such usage being commonplace.
 
H

Harald van Dijk

Do you have an authoritative reference for referring to the sign bit off
states as "positive" rather than "non-negative"? I'm not familiar with
any such usage being commonplace.

The C standard refers the two possible zero values as "positive zero" and
"negative zero" in various places.
 
J

James Kuyper

Harald said:
The C standard refers the two possible zero values as "positive zero" and
"negative zero" in various places.

OK, I though he was referring to signed integers.

In floating point operations, if the difference between positive zero
and negative zero matters, then f(positive zero) should properly be
interpreted as the limit of f(z) as z approaches zero from above, and
f(negative zero) is the limit of f(z) as z approaches zero from below.
The ISO/IEC 559 rules for handling negative and positive zeros are, for
the most part, consistent with this concept.

The thing that it actually approached in both case is true zero, which
is neither positive nor negative. When the difference between positive
and negative zero doesn't matter, they should be considered as redundant
representations for true zero.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top