473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
12 2315

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


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
"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
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
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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lu | last post by:
Hello, I am wondering how to protect a global variable in a header file from external access. So I googled and found: "The keyword 'static' has two different uses, depending on whether it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.