473,883 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf() error with long double and null pointer.

Hello all,

Before stating my question, I should mention that I'm fairly new to C.

Now, I attempted a small demo that prints out the values of C's numeric
types, both uninitialised and after assigning them their maximum defined
values. However, the output of printf() for the long double 'ld' and the
pointer of type void 'v_p', after initialisation don't seem to be right.

The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
'-pedantic' switches. No warnings were emitted. Incidentally the MS
Visual C++ 2003 compiler's output seems okay. I give them both below:

gcc's output:
ld == -1.#QNAN0
or -1.#QNAN
v_p == FFFFFFFF

msvc's output:
ld == 179769313486231 570000000000000 000000000000000 000000000000000 000000
000000000000000 000000000000000 000000000000000 000000000000000 000000000000
000000000000000 000000000000000 000000000000000 000000000000000 000000000000
000000000000000 000000000000000 000000000000000 000000000000000 000000000000
000000000000000 000000000000.00 0000
or 1.79769e+308
v_p == 00000000

I thought null pointers had a value of zero. Why is gcc's executable
printing ffffffffh? Also the value of 'ld' seems to be wrong.

The code for the demo is:
#include <stdio.h>
#include <limits.h>
#include <float.h>

int main(void) {
char c;
unsigned char uc;
short s;
unsigned short us;
int i;
unsigned int ui;
long l;
unsigned long ul;
float f;
double d;
long double ld;
void *v_p;

printf("c == %c\n\tor %d\nuc == %c\n\tor %u\ns == %hd\nus == %hu\n"
"i == %d\nui == %u\nl == %ld\nul == %lu\nf == %f\n\tor %g\n"
"d == %lf\n\tor %g\nld == %Lf\n\tor %Lg\nv_p == %p\n",
c,c,uc,uc,s,us, i,ui,l,ul,f,f,d ,d,ld,ld,v_p);

puts("Initialis ing them with their maximum allowed values...");
c = CHAR_MAX;
uc = UCHAR_MAX;
s = SHRT_MAX;
us = USHRT_MAX;
i = INT_MAX;
ui = UINT_MAX;
l = LONG_MAX;
ul = ULONG_MAX;
f = FLT_MAX;
d = DBL_MAX;
ld = LDBL_MAX;
puts("Initialis ing v_p with NULL...");
v_p = NULL;

printf("c == %c\n\tor %d\nuc == %c\n\tor %u\ns == %hd\nus == %hu\n"
"i == %d\nui == %u\nl == %ld\nul == %lu\nf == %f\n\tor %g\n"
"d == %lf\n\tor %g\nld == %Lf\n\tor %Lg\nv_p == %p\n",
c,c,uc,uc,s,us, i,ui,l,ul,f,f,d ,d,ld,ld,v_p);
return 0;
}

Where is the mistake?
Thanks for all the help.
Feb 24 '06
69 5620
Jordan Abel wrote:
On 2006-02-24, Richard G. Riley <rg****@gmail.c om> wrote:
On 2006-02-24, Jordan Abel <ra*******@gmai l.com> wrote:
On 2006-02-24, Richard G. Riley <rg****@gmail.c om> wrote:
On 2006-02-24, A. Sinan Unur <1u**@llenroc.u de.invalid> wrote:
> "Richard G. Riley" <rg****@gmail.c om> wrote in news:468k3hF9qh djU3
> @individual.net :
>
>> On 2006-02-24, A. Sinan Unur <1u**@llenroc.u de.invalid> wrote:
>>> fieldfallow <fi*********@gm ail.com> wrote in news:dtmt1n$1g3 $1
>>> @emma.aioe.org:
>>>
>>>> I thought null pointers had a value of zero. Why is gcc's executable
>>>> printing ffffffffh?
>>> http://c-faq.com/null/index.html
>>>
>>> Sinan
>>
>> Which part?
> Start from 5.1, continue until 5.20. The null pointer FAQs are organized
> very well, starting with the definition, and answering each question in a
> way that builds on the previous discussion.
>
> Sinan

Firstly, I'm not trying to be purposefully counter productive
here. But I am confused as to your answer : especially when the OPs
problem was correctly disgnosed elsewhere as nothing to do with the
pointer value.

How would you disagree that the NULL pointer has a value of 0 for the
purposes of comparison and display? Which is what the OP asked and to
suggest he is wrong on this is a misleading thing in the context of
his problems isnt it?

It might not for the purpose of display. Given that the _only_ thing
meaningful you can do with a pointer as far as 'display' is to print it
with the format specifier %p, the "value" of any pointer, including the
null pointer, for the purpose of display is an implementation-defined
string of characters.
Not completely true. You could use an unsigned char pointer to read the
bytes of its representation one at a time and print them.
It was very explicit that NULL and 0 are the same in the context of
pointers and comparisons. Note this is different to internal
representation as covered in other posts in the thread.

But anyhwere else that a NULL pointer is used in terms of being used
for mathematical manipulation of any time its value is 0.
You can't "mathematic ally manipulate" the null pointer. The only

thing

Sorry. Nomenclature. I (think) meant a pointer which is NULL. Or a
NULLed pointer : is this not a correct term? e.g.

ptr=NULL;


I'd say "null pointer" - "NULL pointer" is not technically correct, but
i knew what you meant. How do you think you can "mathematic ally
manipulate" it? multiply or divide it by something? take its logarithm
base 10?


Well, you could try addition/subtraction on it, although that invokes
undefined behaviour.
you can do is compare it to another pointer. If you compare it to 0 or
to the macro NULL, that 0 is converted to a pointer before the
comparison.

you mean if I

if(0==ptr)

then 0 is cast to a pointer of same type as ptr? I would expect that.

Hypothetical situation :

a char * which needs to point to memory address 0. What is the
thoughts on this? In this case the pointer must "hold 0" but is not
necessarily a NULL pointer?


Conversion of an integer (i.e. an integer that is not a constant
expression equal to 0) to a pointer is implementation-defined.

If you really needed this, an implementation would probably go about it
by having the internal representation of a null pointer be some other
value, and you'd do (char *)(int)0 [with the cast to int to make the 0
no longer be a null pointer constant].


If you are talking about accessing a HW register or something hard coded
at address 0 then yes, that might be used. Or some other system
dependent memory. If, on the other hand, address 0 is just a normal
address and the compiler happens to put a char array at that address,
you would just access it as you would any other char array.

I.e.,

#include <stdio.h>

int main(void)
{
char fred[] = "my string";
/* compiler places fred at location 0 */
char *derf=0; /* derf does *not* point to fred */
if (derf == fred)
puts("This never happens.");
derf = fred; /* derf now contains address 0 */
return 0;
}

Basically, 0 in the source code is a "magic" symbol. When used in a
pointer context it does *not* mean 0, it means "null pointer constant".
It just happens to be spelt "0".

Another regular here has said he wishes C had a keyword "nil" as the one
and only null pointer constant, and I agree. Where this the case you
could not assign 0 to a pointer or compare a pointer with 0 (except by
using a cast) and the confusion would go away.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Feb 25 '06 #61
In article <5k************ *************** *****@4ax.com>,
Jack Klein <ja*******@spam cop.net> wrote:
gcc, on the other hand, uses the Intel 80-bit extended precision type
for long double.


That seems like a really bad decision on a system whose libraries use
64-bit long doubles. Microsoft's decision may have been unwise, but
it's Mingw's decision that breaks correct programs. As you can see
from this thread, it has the potential to (at the very least) waste
a lot of people's time.

-- Richard
Feb 26 '06 #62
"Richard G. Riley" <rg****@gmail.c om> wrote:
On 2006-02-24, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Richard G. Riley" <rg****@gmail.c om> wrote:
Apart from his uninitiliased issues, his comments about null pointers
having a value of zero is fairly common isnt it?


Very common, but also wrong.


Would you agree that a "null pointer" had a value of NULL?


No. NULL is a macro. A null pointer has a null value.

Many of the problems newbies seem to have with null pointers, and
particularly with null pointers in combination with 0, seem to me to
stem from a confusion between a source code and the running executable;
that is, between a null pointer constant and a pointer object which
happens to have a null value. That is why it is IMO important not to
confuse the issue any more by claiming that null pointers have either
zero, or NULL value, or that NULL is zero (while keeping in mind that
NULL _may_ be a (source-level!) 0, but need not be).

Richard
Feb 27 '06 #63
On 2006-02-27, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Richard G. Riley" <rg****@gmail.c om> wrote:
On 2006-02-24, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
> "Richard G. Riley" <rg****@gmail.c om> wrote:
>
>> Apart from his uninitiliased issues, his comments about null pointers
>> having a value of zero is fairly common isnt it?
>
> Very common, but also wrong.
Would you agree that a "null pointer" had a value of NULL?


No. NULL is a macro. A null pointer has a null value.


I must remember to be more careful

that should read:

" .. had a value of null"
Many of the problems newbies seem to have with null pointers, and
particularly with null pointers in combination with 0, seem to me to
Well, I'm certainly no newbie but would admit to taking some short
cuts in the past with regard to null pointers. Not something I would
be overly proud avout, but something very prevalent in C systems the
world over.
stem from a confusion between a source code and the running executable;
that is, between a null pointer constant and a pointer object which
happens to have a null value. That is why it is IMO important not to
confuse the issue any more by claiming that null pointers have either
zero, or NULL value, or that NULL is zero (while keeping in mind that
NULL _may_ be a (source-level!) 0, but need not be).

Richard


What is a null pointer to you? In standard "conversati on" it is for me
a pointer which has a null value or ptr==NULL or (!ptr);
--
Remove evomer to reply
Feb 27 '06 #64
Richard G. Riley wrote:

On 2006-02-27, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Richard G. Riley" <rg****@gmail.c om> wrote:
On 2006-02-24, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
> "Richard G. Riley" <rg****@gmail.c om> wrote:
> >> having a value of zero is fairly common isnt it?
>
> Very common, but also wrong.
that should read:

" .. had a value of null"


The meaning of "null" is not sufficiently distinct from
the meaning of "zero", in order for the meaning of
"a value of zero" to be different from the meaning of
"a value of null".

--
pete
Feb 27 '06 #65
On 2006-02-27, pete <pf*****@mindsp ring.com> wrote:
Richard G. Riley wrote:

On 2006-02-27, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
> "Richard G. Riley" <rg****@gmail.c om> wrote:
>
>> On 2006-02-24, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
>> > "Richard G. Riley" <rg****@gmail.c om> wrote:
>> > >> >> having a value of zero is fairly common isnt it?
>> >
>> > Very common, but also wrong.

that should read:

" .. had a value of null"


The meaning of "null" is not sufficiently distinct from
the meaning of "zero", in order for the meaning of
"a value of zero" to be different from the meaning of
"a value of null".


Yes. I know. We covered all that. But a pointer has a value. Its
something. So NULL is out of the question because it's a macro
..... zero, because it isnt't, and now null because its not
sufficiently distinct? Do I detect a vicious circle here again?

Tell me, what terminology would you use to describe a pointer that has
just had NULL assigned to it?

Or did I misunderstsnd your meaning?

--
Remove evomer to reply
Feb 27 '06 #66

"Richard G. Riley" <rg****@gmail.c om> wrote in message
news:46******** ****@individual .net...
On 2006-02-27, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Richard G. Riley" <rg****@gmail.c om> wrote:
On 2006-02-24, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
> "Richard G. Riley" <rg****@gmail.c om> wrote:
>
>> Apart from his uninitiliased issues, his comments about null pointers >> having a value of zero is fairly common isnt it?
>
> Very common, but also wrong.

Would you agree that a "null pointer" had a value of NULL?


No. NULL is a macro. A null pointer has a null value.


I must remember to be more careful

that should read:

" .. had a value of null"
Many of the problems newbies seem to have with null pointers, and
particularly with null pointers in combination with 0, seem to me to


Well, I'm certainly no newbie but would admit to taking some short
cuts in the past with regard to null pointers. Not something I would
be overly proud avout, but something very prevalent in C systems the
world over.
stem from a confusion between a source code and the running executable;
that is, between a null pointer constant and a pointer object which
happens to have a null value. That is why it is IMO important not to
confuse the issue any more by claiming that null pointers have either
zero, or NULL value, or that NULL is zero (while keeping in mind that
NULL _may_ be a (source-level!) 0, but need not be).

Richard


What is a null pointer to you? In standard "conversati on" it is for me
a pointer which has a null value or ptr==NULL or (!ptr);


This is absolute. But null pointer constants are magic. They violate the
normal rules of expression evaluation. In a world with null pointer
constants, we can have
(char *)expr1 != (char *)expr2
even though expr1 == expr2. And if ptr == expr it's not necessarily safe to
say that the value of ptr is the same as the value of expr. You have to
pick your way through the minefield very delicately

--
RSH

Feb 27 '06 #67
Richard G. Riley wrote:

On 2006-02-27, pete <pf*****@mindsp ring.com> wrote:
Richard G. Riley wrote:

On 2006-02-27, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
> "Richard G. Riley" <rg****@gmail.c om> wrote:
>
>> On 2006-02-24, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
>> > "Richard G. Riley" <rg****@gmail.c om> wrote:
>> >

>> >> having a value of zero is fairly common isnt it?
>> >
>> > Very common, but also wrong.

that should read:

" .. had a value of null"


The meaning of "null" is not sufficiently distinct from
the meaning of "zero", in order for the meaning of
"a value of zero" to be different from the meaning of
"a value of null".


Yes. I know. We covered all that. But a pointer has a value. Its
something. So NULL is out of the question because it's a macro
.... zero, because it isnt't, and now null because its not
sufficiently distinct? Do I detect a vicious circle here again?

Tell me, what terminology would you use to describe a pointer that has
just had NULL assigned to it?


It's a "null pointer".

--
pete
Feb 27 '06 #68
"Richard G. Riley" <rg****@gmail.c om> wrote:
On 2006-02-27, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Richard G. Riley" <rg****@gmail.c om> wrote:
On 2006-02-24, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
> "Richard G. Riley" <rg****@gmail.c om> wrote:
>
>> Apart from his uninitiliased issues, his comments about null pointers
>> having a value of zero is fairly common isnt it?
>
> Very common, but also wrong.

Would you agree that a "null pointer" had a value of NULL?


No. NULL is a macro. A null pointer has a null value.


I must remember to be more careful

that should read:

" .. had a value of null"


In that case, no; I wouldn't complain if someone else called it that,
but I wouldn't phrase it like that myself.
Many of the problems newbies seem to have with null pointers, and
particularly with null pointers in combination with 0, seem to me to


Well, I'm certainly no newbie but would admit to taking some short
cuts in the past with regard to null pointers. Not something I would
be overly proud avout, but something very prevalent in C systems the
world over.


I certainly have done so myself, when I was considerably more of a
newbie than I was now.
stem from a confusion between a source code and the running executable;
that is, between a null pointer constant and a pointer object which
happens to have a null value. That is why it is IMO important not to
confuse the issue any more by claiming that null pointers have either
zero, or NULL value, or that NULL is zero (while keeping in mind that
NULL _may_ be a (source-level!) 0, but need not be).


What is a null pointer to you? In standard "conversati on" it is for me
a pointer which has a null value or ptr==NULL or (!ptr);


Yes; but a NULL is something else. A null pointer _compares_ equal to
both NULL and zero - any mathematical zero, including 0.0!), but that's
only because it's an explicit exception, and NULL, where it is literally
a 0, is converted to a null pointer - _not_ the other way 'round.

Richard
Feb 28 '06 #69
On 2006-02-28, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Yes; but a NULL is something else. A null pointer _compares_ equal to
both NULL and zero
yes.
- any mathematical zero, including 0.0!)
No. 0.0 is not a null pointer constant.
but that's only because it's an explicit exception, and NULL, where it
is literally a 0, is converted to a null pointer - _not_ the other way
'round.

Richard

Feb 28 '06 #70

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

Similar topics

3
3236
by: Hans Ginzel | last post by:
Hello, let us consider function int foo(char *name, void *data) { ... printf(name, data); /* Should be *data? */ ... }
188
17512
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
16
19717
by: Jordan Abel | last post by:
I have written this function and was wondering if anyone else could point out if there's anything wrong with it. The purpose is to substitute for printf when in a situation where low-level I/O has to be used for whatever reason, and memory allocation cannot be used - mainly it's become for me an exercise in interpreting printf-like format strings, and i thought i'd post it here to see if anyone can point out any bugs in it - i.e. anything...
0
9933
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9781
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11123
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...
1
7960
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
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5794
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
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3230
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.