全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 1021|回复: 13

[求助]C语言有一个小问题

[复制链接]
发表于 2017-8-19 16:03:42 | 显示全部楼层 |阅读模式
本帖最后由 小百无一 于 2017-8-19 16:08 编辑

Ubuntu 16.04下

  1. #include<stdio.h>

  2. int main (void)
  3. {
  4.         int old;
  5.         float old_f=old*3.156e7;
  6.         double old_f2=old*3.156e7;
  7.         long double old_f3=old*3.156e7;

  8.         printf("Please enter your age:");
  9.         scanf("%d",&old);
  10.         printf("%d years is equal to %f seconds in float....%f seconds in double....%Lf seconds in long double",old,old_f,old_f2,old_f3);

  11.         return 0;
  12. }
复制代码


输出结果

  1. root@ubuntu:/home/user/c# ./a.out
  2. Please enter your age:111
  3. 111 years is equal to 0.000000 seconds in float....0.000000 seconds in double....0.000000 seconds in long double

  4. root@ubuntu:/home/user/c# cc -v
  5. Using built-in specs.
  6. COLLECT_GCC=cc
  7. COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper
  8. Target: x86_64-linux-gnu
  9. Configured with: ../src/configure -v --with-pkgversion='Ubuntu 6.3.0-12ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
  10. Thread model: posix
  11. gcc version 6.3.0 20170406 (Ubuntu 6.3.0-12ubuntu2)
复制代码


请问哪里出了问题?
发表于 2017-8-19 16:05:47 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| 发表于 2017-8-19 16:07:18 | 显示全部楼层
创梦网络科技 发表于 2017-8-19 16:05
我都 不C语言    请问一下哪里有视频可看

视频mooc之类的
发表于 2017-8-19 16:12:57 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| 发表于 2017-8-19 16:13:52 | 显示全部楼层
hellfires 发表于 2017-8-19 16:12
这是很简单的问题,你前面定义old没有初值,然后你就定义old_f=3倍它,这时old_f就是0,后面你又没对old_f ...


windows下这个运行就没有问题

之前没加long double时,ubuntu也可以运行

点评

那我的vs2017是盗版哈哈,楼主买本谭浩强来看看吧,学c老实dev c++这种初级软件走起吧  发表于 2017-8-19 16:21
发表于 2017-8-19 16:14:09 | 显示全部楼层
本版块讨论的话题果然内容丰富
发表于 2017-8-19 16:14:35 | 显示全部楼层
那个,你变量都没都赋值就做运算,初始值是0来着,当然运算结果是0了。。。
 楼主| 发表于 2017-8-19 16:15:02 | 显示全部楼层
kyotrue 发表于 2017-8-19 16:14
那个,你变量都没都赋值就做运算,初始值是0来着,当然运算结果是0了。。。 ...

windows下这个运行就没有问题

之前没加long double时,ubuntu也可以正常运行
发表于 2017-8-19 16:18:11 | 显示全部楼层
小百无一 发表于 2017-8-19 16:15
windows下这个运行就没有问题

之前没加long double时,ubuntu也可以正常运行

你在说玄幻问题,Windows只是初始值不会自动初始化为0而已,但是输出结果肯定是错的。
发表于 2017-8-19 16:19:38 | 显示全部楼层
  1. #include<stdio.h>

  2. int main (void)
  3. {
  4.         int old;
  5.         float old_f;
  6.         double old_f2;
  7.         long double old_f3;

  8.         printf("Please enter your age:");
  9.         scanf("%d",&old);
  10.         old_f=old*3.156e7;
  11.         old_f2=old*3.156e7;
  12.         old_f3=old*3.156e7;
  13.         printf("%d years is equal to %f seconds in float....%f seconds in double....%Lf seconds in long double",old,old_f,old_f2,old_f3);

  14.         return 0;
  15. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-5-5 18:41 , Processed in 0.069110 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表