473,656 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printf("%d")

Hi all,

Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything.

-13361016

Any help would be appreciated.

Thanks,
MJ

Nov 15 '05 #1
9 2786
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
geek <me********@gma il.com> wrote:
Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything. -13361016


It is undefined behaviour to have a format element for which the
parameter is missing or not of the matching type.
What your system is probably doing is converting whatever happens
to be on the stack, which probably happens to be a pointer with
value 0xff342088. If the same pointer happened to be on the stack
when you tried the %c format element, then probably your system
would attempt to convert one of the bytes as a character; exactly
which one would depend upon your internal system architecture.
Likely it is not true that it doesn't print anything: if you were
to save the output to a file and look at the file in detail,
you would probably find the character with value 255 (0xff) or
value 136 (0x88) or with value 32 (0x20). The first two of those
do not produce any visible graphics in ASCII or ISO-8859-1;
the last of those happens to correspond to the space character
in ASCII and ISO-8859-1.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Nov 15 '05 #2
Walter Roberson <ro******@ibd.n rc-cnrc.gc.ca> wrote:
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
geek <me********@gma il.com> wrote:
Why does a printf("%d") statement prints this as output and when I do
printf("%c" ) it doesn't print anything.

-13361016


It is undefined behaviour to have a format element for which the
parameter is missing or not of the matching type.
What your system is probably doing is converting whatever happens
to be on the stack, which probably happens to be a pointer with
value 0xff342088. If the same pointer happened to be on the stack


Mind you, but why would printf() convert a pointer instead of a value
here? I strongly doubt, that any implementation (though I might be
wrong) is doing it as you explain.

--
Z (zo**********@w eb.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 15 '05 #3
"geek" <me********@gma il.com> wrote in news:1128916418 .735484.182050
@g14g2000cwa.go oglegroups.com:

Hi all,

Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything.

-13361016


Are you trying to print that integers as strings? See if your compiler
supports itoa() and ltoa() functions. (Look in stdlib.h.)
Nov 15 '05 #4
In article <ne************ ********@news.d aimlerchrysler. com>,
Zoran Cutura <zo**********@w eb.de> wrote:
Why does a printf("%d") statement prints this as output and when I do
printf("%c ") it doesn't print anything. -13361016
What your system is probably doing is converting whatever happens
to be on the stack, which probably happens to be a pointer with
value 0xff342088. If the same pointer happened to be on the stack
Mind you, but why would printf() convert a pointer instead of a value
here?


It's converting (printing) the value on the stack, which probably *is*
a pointer, as if it were an integer.

-- Richard
Nov 15 '05 #5

"Dale" <da***@gas.oran ge> wrote in message
news:Xn******** *************** **@204.153.244. 156...
"geek" <me********@gma il.com> wrote in news:1128916418 .735484.182050
@g14g2000cwa.go oglegroups.com:

Hi all,

Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything.

-13361016


Are you trying to print that integers as strings? See if your compiler
supports itoa() and ltoa() functions. (Look in stdlib.h.)


Oh, you wait - someone will say sprintf! Besides me of course.
Nov 15 '05 #6
In article <ne************ ********@news.d aimlerchrysler. com>,
Zoran Cutura <zo**********@w eb.de> wrote:
Walter Roberson <ro******@ibd.n rc-cnrc.gc.ca> wrote:
It is undefined behaviour to have a format element for which the
parameter is missing or not of the matching type. What your system is probably doing is converting whatever happens
to be on the stack, which probably happens to be a pointer with
value 0xff342088. If the same pointer happened to be on the stack
Mind you, but why would printf() convert a pointer instead of a value
here? I strongly doubt, that any implementation (though I might be
wrong) is doing it as you explain.


Let me re-state with a minor wording variation for clarity:

"What your system is probably doing is converting whatever
happens to be on the top of the stack, which is a value which
is 0xff342088, which is probably a pointer."
There is some question about the endianness of the original poster's
machine, so the integer value that printed out with value 0xff342088
might have been stored internally as 0x34, 0xff, 0x88, 0x20
or 0x88, 0x20, 0x34, 0xff or 0x20, 0x88, 0xff, 0x34 or some other
byte order. This confuses the determination of the likely type
of the stray value that happened to be printed out.

The original value was unlikely to have been an IEEE float or
double, as the values implied by any of the possible orderings
are fairly far away from 0 -- on the order of 1e-52 for the largest
of them.

The value is unlikely to have been a string: 0xff and 0x88 are
unlikely as desired ISO-8859-1 characters, and the values look
unlikely for UTF-8 or for any of the UTF variations that call for
"shift codes".

The value is unlikely to have been a random int lying around
in memory -- -13361016 is just a bit too random for that.

Scrambled variations on the value look -unlikely- as virtual
memory pointers.

I have, though, seen stray values in the 0xff342088 range on the
stack, mostly as stack pointers. Well, not quite: on the architecture
I normally use, 0x7f342088 is a plausible virtual stack pointer,
which happens to be upper memory as the high address bit of
memory addresses happens to be reserved in this architecture. But
on an architecture that didn't have that reservation, 0xff342088
could be a plausible stack pointer.

The one last thing I can think of as plausible, is that the
0xff342088 could be a relative branch address to return from the
present function.

Perhaps some day the OP will mention the architecture involved
and supply a short program to reproduce the output, and someone
might get bored enough to analyze the flow to find out exactly
how that -particular- value ended up being the value sitting
on the stack at the time of the undefined behaviour.
--
Many food scientists have reported chocolate to be the single most
craved food. -- Northwestern University, 2001
Nov 15 '05 #7

"geek" <me********@gma il.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hi all,

Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything.

-13361016

Any help would be appreciated.


printf is what's called a variadic function - it has one fixed arg (const
char *), which can be followed by [as far as anyone is really concerned] any
number of other args.

How does printf know how many args it's been passed then?

The first arg is used to inform printf about the further args ... %c says
there's character, %s says there's a string %d an int etc.

So, you're basically lying to printf - you say there's either an int or a
char on the stack following the format string - but they're not there
really - well, the ones you've been expected to pass aren't!

Nov 15 '05 #8
Dale wrote:
"geek" <me********@gma il.com> wrote in news:1128916418 .735484.182050
@g14g2000cwa.go oglegroups.com:
Hi all,

Why does a printf("%d") statement prints this as output and when I do
printf("%c" ) it doesn't print anything.

-13361016


Are you trying to print that integers as strings? See if your compiler
supports itoa() and ltoa() functions. (Look in stdlib.h.)


All os which are completely non-standard. Since sprintf can do what
these do there is absolutely no good reason to use them, always use the
printf family of functions instead.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #9
"geek" <me********@gma il.com> writes:
Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything.

-13361016

Any help would be appreciated.


As others have said, both statements invoke undefined behavior. In
both cases, you're lying to printf(), promising to provide an
additional argument that isn't there. It then (probably) grabs
something from wherever the argument *would* have been if you had
provided it, possibly on the stack.

The solution is quite simple: Don't do that.

But it's very likely that printf("%c") *is* printing something; you're
just not seeing it. The "%c" format tells printf to grab an int value
and print it as a character. If the resulting character happens not
to be printable, you may not see any output. If you're curious,
depending on your system, you may be able to capture the output of the
program and examine it (<OT>on Unix, "./myprogram | cat -A"</OT>).

But again, the solution is simple: Don't do that.

--
Keith Thompson (The_Other_Keit h) 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 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3799
by: sugaray | last post by:
does the expression int a=printf("%d\n",a); implementation dependent ? I supposed it would produced the result 2, but i got 4206596 (result may vary on your machine), i wonder if the value produced was a memory address and anything special with this expression ?
188
17316
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
12
2344
by: baumann | last post by:
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) {
19
5452
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
12
43431
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: printf("\n%20s\t%7u\t%13i\t%13i","signed int",sizeof(signed int),INT_MIN,INT_MAX);
10
2353
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi, (void *)pj, sizeof *pi);*/ /*size of what?*/ By the way, will the 3rd argument of memcpy follow the size of the 1st or the 2nd argument? If it follows the size of the 1st argument, unwanted data in the memory area pointed by the 2nd parameter...
26
6848
by: Yevgen Muntyan | last post by:
Hey, It was mentioned elsewhere that printf("%d", INT_MAX); is implementation-defined. Why? Is it because INT_MAX value is implementation-defined so output depends on implementation, or is it something more subtle so that output of the following is also implementation-defined: if (INT_MAX == 65535) printf("%d", INT_MAX);
7
5206
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. http://groups.google.co.in/group/comp.std.c/browse_thread/thread/a656169cb5941cbf/?hl=en# But I want to know what was the conclusion that has been reached. It is unusually long with 146 posts. Therefore it is hard to follow the
29
11080
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.