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

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

P: n/a
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?

Nov 14 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

"baumann@pan" <ba*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Nov 14 '05 #2

P: n/a
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;

}

Nov 14 '05 #3

P: n/a


Allan Bruce wrote:
"baumann@pan" <ba*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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).

--
Er*********@sun.com

Nov 14 '05 #4

P: n/a
"Allan Bruce" <no****@btinternet.com> wrote:

"baumann@pan" <ba*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.
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


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

Richard
Nov 14 '05 #5

P: n/a
On 13 Apr 2005 07:45:02 -0700, in comp.lang.c , "baumann@pan"
<ba*********@gmail.com> wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #6

P: n/a
On Wed, 13 Apr 2005 15:07:08 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #7

P: n/a
"baumann@pan" <ba*********@gmail.com> writes:
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.

--
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.
Nov 14 '05 #8

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
[...]
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.

--
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.
Nov 14 '05 #9

P: n/a
Mark McIntyre <ma**********@spamcop.net> wrote:
On Wed, 13 Apr 2005 15:07:08 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
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.


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
Nov 14 '05 #10

P: n/a
On Thu, 14 Apr 2005 13:55:37 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Mark McIntyre <ma**********@spamcop.net> wrote:
On Wed, 13 Apr 2005 15:07:08 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>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.


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...
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-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 =----
Nov 14 '05 #11

P: n/a
On Wed, 13 Apr 2005 23:23:03 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
[...]
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.


Quite right.

*smack*
there /is/ more than one encoding than ascii
*smack*

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-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 =----
Nov 14 '05 #12

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
On Thu, 14 Apr 2005 13:55:37 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:

[...]
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.)

--
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.
Nov 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.