Question related to printf

R

Richard Bos

somenath said:
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 .

Almost. The absolute values 49 and 50 for '1' and '2' are not guaranteed
(because the Standard doesn't require ASCII), but ISO C _does_ demand
that '2' immediately follows '1'.
But my doubt is

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

From the ISO C Standard (1989, as it happens, but C99 is the same):

# 4.9.6.1 The fprintf function
....
# c The int argument is converted to an unsigned char, and the
# resulting character is written.

Note: _int_ argument, which is converted to an unsigned char. So no, as
you thought, it does not have undefined behaviour.

Richard
 
S

somenath

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
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
 
M

Mark Bluemel

Richard said:
Question. Your _question_ is.

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.

It's really not worth arguing this out each time an Indian posts a
"doubt"/question...
 
S

somenath

Almost. The absolute values 49 and 50 for '1' and '2' are not guaranteed
(because the Standard doesn't require ASCII), but ISO C _does_ demand
that '2' immediately follows '1'.

Does this mean that the output of the above program may be not 2 in
case of non ASCII based system ?
 
P

pete

somenath said:
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.
 
J

jameskuyper

somenath said:
Does this mean that the output of the above program may be not 2 in
case of non ASCII based system ?

No, it means that the above program is guaranteed to print 2, no
matter which character encoding a conforming implementation uses. This
implies that a conforming implementation is not allowed to use an
encoding in which the codes for decimal digits are not sequential.
 
M

Mark Bluemel

somenath said:
Does this mean that the output of the above program may be not 2 in
case of non ASCII based system ?

As Richard stated, ISO C requires that '1' is followed by '2' in the
character set - indeed it requires that '0' - '9' are contiguous and
sequential.
 
S

somenath

somenath said:
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 ?
 
R

Richard Heathfield

somenath said:

So C compiler is not going to convert '1' + 1 to 49 + 1 it will
convert '1'+1 as 1+1 .

The compiler adds '1' and 1, and obtains the result '2'.
 
M

Martin Ambuhl

somenath said:
On Dec 12, 9:04 pm, (e-mail address removed) (Richard Bos) wrote:

Does this mean that the output of the above program may be not 2 in
case of non ASCII based system ?

No, as it Mr. Bos plainly stated, '2' immediately follows '1', so '1'+1
is always '2'. How could you read what he wrote otherwise? It is not,
however, a given that, for example, 'e'+1 is 'f'.
 
P

Philip Potter

Mark said:
As Richard stated, ISO C requires that '1' is followed by '2' in the
character set - indeed it requires that '0' - '9' are contiguous and
sequential.

But no such guarantees are made about any other characters. So the
following program is /not/ guaranteed to output 'b' (though it will in
ASCII):

#include <stdio.h>
int main(void) {
printf("%c\n", 'a'+1);
return 0;
}

I only say this because I found it surprising when I first learned about it.

Also note the trailing newline. On some systems, the last line of output
in a program /must/ be terminated with a newline.
 
S

somenath

somenath said:

<snip>




The compiler adds '1' and 1, and obtains the result '2'.

I did not get your point
Suppose in case of the bellow statement
int x = '1'+ 1 ;
as per the integer promotion rule '1' will be promoted to int 1 .
so why are you mentioning it as '1' ? Please provide some input .
 
P

Philip Potter

somenath said:
somenath said:
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. Other mathematical constructs are not likely to work on
numerical characters, for example '1'*2 == '2' is guaranteed to be
false. The character '1' is never interpreted as the integer 1.

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.

Phil
 
H

harsha

I did not get your point
Suppose in case of the bellow statement
int x = '1'+ 1 ;
as per the integer promotion rule '1' will be promoted to int 1 .
so why are you mentioning it as '1' ? Please provide some input .

'1' will not be promoted to int 1.
it will be casted to integer value of whatever character encoding the
machine follows.
 
P

Philip Potter

somenath said:
I did not get your point
Suppose in case of the bellow statement
int x = '1'+ 1 ;
as per the integer promotion rule '1' will be promoted to int 1 .

No, it is promoted to the integer '1'. Which may well have the value 49.

(If '1' is promoted to 1, then what is 'a' promoted to?)
 
B

Ben Pfaff

somenath said:
int x = '1'+ 1 ;
as per the integer promotion rule '1' will be promoted to int 1 .

No, no promotion takes place. '1' has type int, as does every
(non-wide) character constant.
 
B

Ben Pfaff

harsha said:
'1' will not be promoted to int 1.
it will be casted to integer value of whatever character encoding the
machine follows.

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.
 
P

pete

somenath said:
I did not get your point

His point is that the value of '1' is implementation defined.
He doesn't care about what value '1' equals on your system.
putchar('1') does what it's supposed to do, everywhere, always.
We mostly don't care about code like
putchar(48)
on this newsgroup.

Suppose in case of the bellow statement
int x = '1'+ 1 ;
as per the integer promotion rule '1' will be promoted to int 1 .
so why are you mentioning it as '1' ? Please provide some input .

There's no promotions going on here.

('1') is an expression of type int.

The value of '1' is different on different systems.
To write good code, it's generally better use '1'
to reference the value that prints out "1".
 
A

Al Balmer

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.

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.
It's really not worth arguing this out each time an Indian posts a
"doubt"/question...

Well, nobody was arguing, until you posted :)
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top