printf()错误,带有long double和null指针。 [英] printf() error with long double and null pointer.

查看:93
本文介绍了printf()错误,带有long double和null指针。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


在陈述我的问题之前,我应该提一下我对C的新手。


现在,我尝试了一个小型演示,打印出C'的数字

类型的值,两者都是未初始化的,并且在为它们分配了它们最大定义的

值之后。然而,在初始化之后,printf()的输出为long double''ld''和

指针类型为void''v_p'',似乎不对。


使用的编译器是gcc(mingw),带有''-Wall'',' - std = c99''和

''-pedantic''开关。没有发出任何警告。顺便说一句MS

Visual C ++ 2003编译器的输出似乎没问题。我在下面给他们两个:


gcc'的输出:

ld == -1。#QNAN0

或 - 1.#QNAN

v_p == FFFFFFFF

msvc'的输出:

ld == 17976931348623157000000000000000000000000000000000 0000000000000000
00000000000000000000000000000000000000000000000000 0000000000000000000000
$ b $ 00 00000000000000000000000000000000000000000000000000 0000000000000000000000
$ b $ 00 00000000000000000000000000000000000000000000000000 0000000000000000000000

00000000000000000000000000000.000000

或1.79769e + 308 < br $>
v_p == 00000000


我认为空指针的值为零。为什么gcc'的可执行文件

打印ffffffffh? ld的值似乎也是错误的。


演示的代码是:

#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;

双d;

long double ld;

void * v_p;


printf (" c ==%c\\\
\tor%d\\\
uc ==%c\\\
\\\ t%u \ ns ==%hd \ nus ==%hu \ n"

" i ==%d \ nui ==%u \ nl ==%ld \ nul ==%lu \ nf ==%f \ n \\\ %% g \ n"

" d ==%lf \ n \\\%%\\ t%\ nd} ==%Lf \ n \%%Lg \ nv_p ==%p \\ \\ n",

c,c,uc,uc,s,us,i,ui,l,ul,f,f,d,d,ld,ld,v_p);


put(初始化它们与他们的最大允许值...);

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(" Initialising v_p with NULL ...");

v_p = NULL;


printf(" c ==%c\\\
\\\ id%d \ nuc ==%c \ n \\\ t%u \ ns ==%hd \ nus ==%hu \ n"

" i ==%d \ nui ==%u \ nl ==%ld\\\
ul ==%lu \ nf ==%f \ n \\ n%\\ n \\ n%\ n \\ n>

" d = =%lf \ n \%%\\ t \\ t%\ nd} ==%Lf \ n \\\%%Lg \ nv_p ==%p \ n",

c,c,uc ,uc,s,us,i,ui,l,ul,f,f,d,d,ld,ld,v_p);

返回0;

}


错误在哪里?

感谢所有帮助。

解决方案

< blockquote> fieldfallow< fi*********@gmail.com>在新闻中写道:dtmt1n


1g3


1

@ emma.aioe.org:

我认为空指针的值为零。为什么gcc的可执行文件打印ffffffffh?



http://c-faq.com/null/index.html

思南

-

A. Sinan Unur< 1u ** @ llenroc.ude.invalid>

(反转每个组件并删除.invalid for email address)


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 == 17976931348623157000000000000000000000000000000000 0000000000000000
00000000000000000000000000000000000000000000000000 0000000000000000000000
00000000000000000000000000000000000000000000000000 0000000000000000000000
00000000000000000000000000000000000000000000000000 0000000000000000000000
000000000000000000000000000.000000
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("Initialising 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("Initialising 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.

解决方案

fieldfallow <fi*********@gmail.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
--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)


这篇关于printf()错误,带有long double和null指针。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆