why printf("%c\n", b) can not get the wanted result?

B

baumann@pan

hi all,
printf("%c",b) doesn't work properly.
#include <stdio.h>
int a , b;
char d, e;
char * p;
float f;

int main(int argc, char* argv[])
{
unsigned char a = 0;
printf("a:%d,b:%d,d:%c,e:%c,p:%p,f:%f\n",a,b,d,e,p, f);
return 0;
}

the prog runs with result as below
a:0,b:0,d:,e:,p:(nil),f:0.000000

if i run it in windows xp, i got
a:0,b:0,d:a,e:a,p:(nil),f:0.000000


why it can not printf char type variable d & e?

and if i change the printf statement to

printf("a:%d,b:%d,d:%x,e:%x,p:%p,f:%f\n",a,b,d,e,p, f);
a:0,b:0,d:0,e:0,p:(nil),f:0.000000

i got what i want.

but i would ask why? my answer to my question is since d, e are char
variable with value 0, maybe %c only print char which are readable char
such as '?','a-z' etc. but value 0 is not a readable char, so under
linux the printf("%c",0) would print nothing.

if my answer is right, why under windowz , it prints a?
 
A

Allan Bruce

baumann@pan said:
hi all,
printf("%c",b) doesn't work properly.
#include <stdio.h>
int a , b;
char d, e;
char * p;
float f;

int main(int argc, char* argv[])
{
unsigned char a = 0;
printf("a:%d,b:%d,d:%c,e:%c,p:%p,f:%f\n",a,b,d,e,p, f);

You havent initialised your variables except for a - you must give them
values before you can print them out!
return 0;
}

the prog runs with result as below
a:0,b:0,d:,e:,p:(nil),f:0.000000

if i run it in windows xp, i got
a:0,b:0,d:a,e:a,p:(nil),f:0.000000


why it can not printf char type variable d & e?

you need to assign a value to them, e.g char d = 'A';
and if i change the printf statement to

printf("a:%d,b:%d,d:%x,e:%x,p:%p,f:%f\n",a,b,d,e,p, f);
a:0,b:0,d:0,e:0,p:(nil),f:0.000000

i got what i want.

but i would ask why? my answer to my question is since d, e are char
variable with value 0, maybe %c only print char which are readable char
such as '?','a-z' etc. but value 0 is not a readable char, so under
linux the printf("%c",0) would print nothing.

0 is defined as the null character in c - although usually written as '\0'
therefore it is not printed out.
if my answer is right, why under windowz , it prints a?

because you have entered the realm of undefined bahaviour. Because the
variables are not initialised the compiler is allowed to do anything it
wants.
Allan
 
B

baumann@pan

since these variables are global, so they got initilized with value 0.
as you can see from the result.

the source code should be
#include <stdio.h>
int a , b;
char d, e;
char * p;
float f;


int main(int argc, char* argv[])
{
// unsigned char a = 0; // it's useless
printf("a:%d,b:%d,d:%c,e:%c,p:­%p,f:%f\n",a,b,d,e,p, f);
return 0;



}
 
E

Eric Sosman

Allan said:
hi all,
printf("%c",b) doesn't work properly.
#include <stdio.h>
int a , b;
char d, e;
char * p;
float f;

int main(int argc, char* argv[])
{
unsigned char a = 0;
printf("a:%d,b:%d,d:%c,e:%c,p:%p,f:%f\n",a,b,d,e,p, f);


You havent initialised your variables except for a - you must give them
values before you can print them out!

Look again: All the variables have been initialized,
most to some form of zero and `p' to NULL.
the prog runs with result as below
a:0,b:0,d:,e:,p:(nil),f:0.000000

if i run it in windows xp, i got
a:0,b:0,d:a,e:a,p:(nil),f:0.000000

[...]

but i would ask why? my answer to my question is since d, e are char
variable with value 0, maybe %c only print char which are readable char
such as '?','a-z' etc. but value 0 is not a readable char, so under
linux the printf("%c",0) would print nothing.


0 is defined as the null character in c - although usually written as '\0'
therefore it is not printed out.

"It is not printed out" is not a behavior the Standard
requires. The Standard specifies how a text stream (like
stdout) must treat printable characters and a few control
characters, but doesn't describe what happens what happens
to other characters. This allows implementations to attach
their own interpretations to outputs like "\033[2J" -- of
course, a program that relies on such special interpretation
is non-portable, but portability is not the only important
characteristic of a program.

In particular, the Standard does not describe the effect
of sending a '\0' character to a text stream; the implementation
is free to do whatever it likes. It appears that the two
systems the O.P. uses treat a '\0' differently: one "prints"
it as a non-graphic, non-spacing character, while the other
"prints" it as something resembling a lower-case 'a' (that's
how it appears in my newsreader, but I'm fairly confident it
looks a little different on the O.P.'s screen).
 
R

Richard Bos

Allan Bruce said:
baumann@pan said:
hi all,
printf("%c",b) doesn't work properly.
#include <stdio.h>
int a , b;
char d, e;
char * p;
float f;

int main(int argc, char* argv[])
{
unsigned char a = 0;
printf("a:%d,b:%d,d:%c,e:%c,p:%p,f:%f\n",a,b,d,e,p, f);

You havent initialised your variables except for a

Yes, he has: they're file-scope static objects, which are initialised to
(value, not bits) zero if you don't initialise them yourself.

I don't think this is legal, since 'a' is clearly not the null
character.

Richard
 
M

Mark McIntyre

since these variables are global, so they got initilized with value 0.
as you can see from the result.

In that case, whats your problem? You tell it that everything has the
value zero or NULL, and thats what it prints.

What were you expecting it to print when you told it to print the
value zero as a char? Did you expect it to print '0'? Nope, that has
value 48.
 
M

Mark McIntyre

On Wed, 13 Apr 2005 15:07:08 GMT, in comp.lang.c ,
I don't think this is legal, since 'a' is clearly not the null
character.

its almost certainly been mangled by usenet from a white smiley face
or some other glyph that MS tended to stuff into the low and high
range of ascii, outside the printable chars.
 
K

Keith Thompson

baumann@pan said:
hi all,
printf("%c",b) doesn't work properly.
#include <stdio.h>
int a , b;
char d, e;
char * p;
float f;

int main(int argc, char* argv[])
{
unsigned char a = 0;
printf("a:%d,b:%d,d:%c,e:%c,p:%p,f:%f\n",a,b,d,e,p, f);
return 0;
}

the prog runs with result as below
a:0,b:0,d:,e:,p:(nil),f:0.000000

if i run it in windows xp, i got
a:0,b:0,d:a,e:a,p:(nil),f:0.000000


why it can not printf char type variable d & e?

It does.

Here's a simpler example:

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

When I run this, the ouput looks like:

a = ''
a = 0

but in fact the program printed a null character. On Unix, I can see
it by piping the output through "cat -v", which displays non-printable
characters in a readable form:

a = '^@'
a = 0

The manner in which a null character is displayed may vary from one
system to another.
 
K

Keith Thompson

Mark McIntyre said:
What were you expecting it to print when you told it to print the
value zero as a char? Did you expect it to print '0'? Nope, that has
value 48.

Or whatever value is defined by the character set being used. It's
guaranteed not to be 0, since that's the null character, but it
needn't be 48. 48 is certainly the most common value, but I think
EBCDIC uses 240.
 
R

Richard Bos

Mark McIntyre said:
On Wed, 13 Apr 2005 15:07:08 GMT, in comp.lang.c ,


its almost certainly been mangled by usenet from a white smiley face
or some other glyph that MS tended to stuff into the low and high
range of ascii, outside the printable chars.

It probably has been mangled by something or other, but '\0' is a
non-printing character even in MS-DOS and MS-Windows character sets; at
least, in all I know.

Richard
 
M

Mark McIntyre

On Wed, 13 Apr 2005 15:07:08 GMT, in comp.lang.c ,


its almost certainly been mangled by usenet from a white smiley face
or some other glyph that MS tended to stuff into the low and high
range of ascii, outside the printable chars.

It probably has been mangled by something or other, but '\0' is a
non-printing character even in MS-DOS and MS-Windows character sets; at
least, in all I know.[/QUOTE]

On an Atari ST, I seem to recall it printed the lower left side of the
face of a man smoking a pipe...
 
K

Keith Thompson

Mark McIntyre said:
[...]
It probably has been mangled by something or other, but '\0' is a
non-printing character even in MS-DOS and MS-Windows character sets; at
least, in all I know.

On an Atari ST, I seem to recall it printed the lower left side of the
face of a man smoking a pipe...

J.R. "Bob" Dobbs, if I'm not mistaken (of the Church of the Subgenius).

(This is, of course, topical, since it demonstrates some of the
possible range of implementation-defined behavior.)
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top