鱼C论坛

 找回密码
 立即注册
查看: 2968|回复: 11

[已解决]带你学C带你飞中,while语句一节计算1加到100的结果

[复制链接]
发表于 2018-6-25 19:47:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如代码:        忘记给sum和i赋值
  1. #include <stdio.h>

  2. int main ()

  3. {
  4.         int sum,i;//忘了给赋值
  5.         while(i<=100)
  6.         {
  7.         sum = sum + i ;
  8.         i = i + 1;
  9.         }
  10.         printf("结果是:%d",sum);
  11.         return 0;
  12. }
复制代码

结果却能输出 4190,请问这是怎么做到的
最佳答案
2018-6-25 21:15:27
1.png

这是我的运行结果

  1. F:\IDE\Dev-Cpp\tmp>gdb tmp.exe
  2. GNU gdb (GDB) (Cygwin 7.12.1-2) 7.12.1
  3. Copyright (C) 2017 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  7. and "show warranty" for details.
  8. This GDB was configured as "x86_64-pc-cygwin".
  9. Type "show configuration" for configuration details.
  10. For bug reporting instructions, please see:
  11. <http://www.gnu.org/software/gdb/bugs/>.
  12. Find the GDB manual and other documentation resources online at:
  13. <http://www.gnu.org/software/gdb/documentation/>.
  14. For help, type "help".
  15. Type "apropos word" to search for commands related to "word"...
  16. Reading symbols from tmp.exe...done.
  17. (gdb) l
  18. 1       #include <stdio.h>
  19. 2
  20. 3       int main(void)
  21. 4       {
  22. 5               int sum, i;             // &#9618;&#9618;&#9618;&#760;&#9618;&#9618;&#9618;&#1461;
  23. 6
  24. 7               while(i <= 100)
  25. 8               {
  26. 9                       sum = sum + i;
  27. 10                      i = i + 1;
  28. (gdb) disass main
  29. Dump of assembler code for function main:
  30.    0x00000000004014f0 <+0>:     push   %rbp
  31.    0x00000000004014f1 <+1>:     mov    %rsp,%rbp
  32.    0x00000000004014f4 <+4>:     sub    $0x30,%rsp
  33.    0x00000000004014f8 <+8>:     callq  0x402320 <__main>
  34.    0x00000000004014fd <+13>:    jmp    0x401509 <main+25>
  35.    0x00000000004014ff <+15>:    mov    -0x8(%rbp),%eax
  36.    0x0000000000401502 <+18>:    add    %eax,-0x4(%rbp)
  37.    0x0000000000401505 <+21>:    addl   $0x1,-0x8(%rbp)
  38.    0x0000000000401509 <+25>:    cmpl   $0x64,-0x8(%rbp)
  39.    0x000000000040150d <+29>:    jle    0x4014ff <main+15>
  40.    0x000000000040150f <+31>:    mov    -0x4(%rbp),%eax
  41.    0x0000000000401512 <+34>:    mov    %eax,%edx
  42.    0x0000000000401514 <+36>:    lea    0x2ae5(%rip),%rcx        # 0x404000
  43.    0x000000000040151b <+43>:    callq  0x402bc8 <printf>
  44.    0x0000000000401520 <+48>:    mov    $0x0,%eax
  45.    0x0000000000401525 <+53>:    add    $0x30,%rsp
  46.    0x0000000000401529 <+57>:    pop    %rbp
  47.    0x000000000040152a <+58>:    retq
  48. End of assembler dump.
  49. (gdb) b main
  50. Breakpoint 1 at 0x4014fd: file main.c, line 7.
  51. (gdb) r
  52. Starting program: /cygdrive/f/IDE/Dev-Cpp/tmp/tmp.exe
  53. [New Thread 12252.0xa590]
  54. [New Thread 12252.0xb51c]

  55. Thread 1 hit Breakpoint 1, main () at main.c:7
  56. 7               while(i <= 100)
  57. (gdb) disass main
  58. Dump of assembler code for function main:
  59.    0x00000000004014f0 <+0>:     push   %rbp
  60.    0x00000000004014f1 <+1>:     mov    %rsp,%rbp
  61.    0x00000000004014f4 <+4>:     sub    $0x30,%rsp
  62.    0x00000000004014f8 <+8>:     callq  0x402320 <__main>
  63. => 0x00000000004014fd <+13>:    jmp    0x401509 <main+25>
  64.    0x00000000004014ff <+15>:    mov    -0x8(%rbp),%eax
  65.    0x0000000000401502 <+18>:    add    %eax,-0x4(%rbp)
  66.    0x0000000000401505 <+21>:    addl   $0x1,-0x8(%rbp)
  67.    0x0000000000401509 <+25>:    cmpl   $0x64,-0x8(%rbp)
  68.    0x000000000040150d <+29>:    jle    0x4014ff <main+15>
  69.    0x000000000040150f <+31>:    mov    -0x4(%rbp),%eax
  70.    0x0000000000401512 <+34>:    mov    %eax,%edx
  71.    0x0000000000401514 <+36>:    lea    0x2ae5(%rip),%rcx        # 0x404000
  72.    0x000000000040151b <+43>:    callq  0x402bc8 <printf>
  73.    0x0000000000401520 <+48>:    mov    $0x0,%eax
  74.    0x0000000000401525 <+53>:    add    $0x30,%rsp
  75.    0x0000000000401529 <+57>:    pop    %rbp
  76.    0x000000000040152a <+58>:    retq
  77. End of assembler dump.
  78. (gdb) info reg
  79. rax            0x1      1
  80. rbx            0x1      1
  81. rcx            0x1      1
  82. rdx            0x7147a0 7423904
  83. rsi            0x1b     27
  84. rdi            0x714740 7423808
  85. rbp            0x62fe50 0x62fe50
  86. rsp            0x62fe20 0x62fe20
  87. r8             0x712460 7414880
  88. r9             0x7147a0 7423904
  89. r10            0x0      0
  90. r11            0x246    582
  91. r12            0x1      1
  92. r13            0x8      8
  93. r14            0x0      0
  94. r15            0x0      0
  95. rip            0x4014fd 0x4014fd <main+13>
  96. eflags         0x202    [ IF ]
  97. cs             0x33     51
  98. ss             0x2b     43
  99. ds             0x2b     43
  100. es             0x2b     43
  101. fs             0x53     83
  102. gs             0x2b     43
  103. (gdb) x/48bx $rbp-48
  104. 0x62fe20:       0x01    0x00    0x00    0x00    0x00    0x00    0x00    0x00
  105. 0x62fe28:       0xc9    0x16    0x40    0x00    0x00    0x00    0x00    0x00
  106. 0x62fe30:       0x1b    0x00    0x00    0x00    0x00    0x00    0x00    0x00
  107. 0x62fe38:       0x1b    0x00    0x00    0x00    0x00    0x00    0x00    0x00
  108. 0x62fe40:       0xa0    0x3c    0xa8    0x23    0xfa    0x7f    0x00    0x00
  109. 0x62fe48:       0x01    0x00    0x00    0x00    0x00    0x00    0x00    0x00
  110. (gdb) print i
  111. $1 = 1
  112. (gdb) print sum
  113. $2 = 0
  114. (gdb) print &i
  115. $3 = (int *) 0x62fe48
  116. (gdb) print &sum
  117. $4 = (int *) 0x62fe4c
  118. (gdb)
复制代码



(gdb) print &i
$3 = (int *) 0x62fe48
变量 i 的地址 0x62fe48
0x62fe48:       0x01    0x00    0x00    0x00    0x00    0x00    0x00    0x00


(gdb) print &sum
$4 = (int *) 0x62fe4c
0x62fe48:       0x01    0x00    0x00    0x00    0x00    0x00    0x00    0x00

编译器替你初始化了,把变量 i 初始化成了 1,把变量 sum 初始化成了 0
结果.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-6-25 19:52:14 | 显示全部楼层
看一下反汇编
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-25 20:05:34 | 显示全部楼层

("▔□▔)呃........Dev-c++怎么做到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-25 20:21:44 | 显示全部楼层
很神奇呢,我用Dev-C++ 打印结果5050
按道理自动存储类别不会自动初始化值到。,但是结果的确正确,但是不安全
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-25 20:26:10 | 显示全部楼层
关键是感觉 发表于 2018-6-25 20:21
很神奇呢,我用Dev-C++ 打印结果5050
按道理自动存储类别不会自动初始化值到。,但是结果的确正确,但是不 ...

嗯,如果我给sum赋值0,i赋值1的话输出的结果也是5050,但是我没赋值的时候竟然也能出来答案(虽然不对)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-25 20:26:21 | 显示全部楼层
关键是感觉 发表于 2018-6-25 20:21
很神奇呢,我用Dev-C++ 打印结果5050
按道理自动存储类别不会自动初始化值到。,但是结果的确正确,但是不 ...

看一下反汇编
我没有dev
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-25 21:15:27 | 显示全部楼层    本楼为最佳答案   
1.png

这是我的运行结果

  1. F:\IDE\Dev-Cpp\tmp>gdb tmp.exe
  2. GNU gdb (GDB) (Cygwin 7.12.1-2) 7.12.1
  3. Copyright (C) 2017 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  7. and "show warranty" for details.
  8. This GDB was configured as "x86_64-pc-cygwin".
  9. Type "show configuration" for configuration details.
  10. For bug reporting instructions, please see:
  11. <http://www.gnu.org/software/gdb/bugs/>.
  12. Find the GDB manual and other documentation resources online at:
  13. <http://www.gnu.org/software/gdb/documentation/>.
  14. For help, type "help".
  15. Type "apropos word" to search for commands related to "word"...
  16. Reading symbols from tmp.exe...done.
  17. (gdb) l
  18. 1       #include <stdio.h>
  19. 2
  20. 3       int main(void)
  21. 4       {
  22. 5               int sum, i;             // &#9618;&#9618;&#9618;&#760;&#9618;&#9618;&#9618;&#1461;
  23. 6
  24. 7               while(i <= 100)
  25. 8               {
  26. 9                       sum = sum + i;
  27. 10                      i = i + 1;
  28. (gdb) disass main
  29. Dump of assembler code for function main:
  30.    0x00000000004014f0 <+0>:     push   %rbp
  31.    0x00000000004014f1 <+1>:     mov    %rsp,%rbp
  32.    0x00000000004014f4 <+4>:     sub    $0x30,%rsp
  33.    0x00000000004014f8 <+8>:     callq  0x402320 <__main>
  34.    0x00000000004014fd <+13>:    jmp    0x401509 <main+25>
  35.    0x00000000004014ff <+15>:    mov    -0x8(%rbp),%eax
  36.    0x0000000000401502 <+18>:    add    %eax,-0x4(%rbp)
  37.    0x0000000000401505 <+21>:    addl   $0x1,-0x8(%rbp)
  38.    0x0000000000401509 <+25>:    cmpl   $0x64,-0x8(%rbp)
  39.    0x000000000040150d <+29>:    jle    0x4014ff <main+15>
  40.    0x000000000040150f <+31>:    mov    -0x4(%rbp),%eax
  41.    0x0000000000401512 <+34>:    mov    %eax,%edx
  42.    0x0000000000401514 <+36>:    lea    0x2ae5(%rip),%rcx        # 0x404000
  43.    0x000000000040151b <+43>:    callq  0x402bc8 <printf>
  44.    0x0000000000401520 <+48>:    mov    $0x0,%eax
  45.    0x0000000000401525 <+53>:    add    $0x30,%rsp
  46.    0x0000000000401529 <+57>:    pop    %rbp
  47.    0x000000000040152a <+58>:    retq
  48. End of assembler dump.
  49. (gdb) b main
  50. Breakpoint 1 at 0x4014fd: file main.c, line 7.
  51. (gdb) r
  52. Starting program: /cygdrive/f/IDE/Dev-Cpp/tmp/tmp.exe
  53. [New Thread 12252.0xa590]
  54. [New Thread 12252.0xb51c]

  55. Thread 1 hit Breakpoint 1, main () at main.c:7
  56. 7               while(i <= 100)
  57. (gdb) disass main
  58. Dump of assembler code for function main:
  59.    0x00000000004014f0 <+0>:     push   %rbp
  60.    0x00000000004014f1 <+1>:     mov    %rsp,%rbp
  61.    0x00000000004014f4 <+4>:     sub    $0x30,%rsp
  62.    0x00000000004014f8 <+8>:     callq  0x402320 <__main>
  63. => 0x00000000004014fd <+13>:    jmp    0x401509 <main+25>
  64.    0x00000000004014ff <+15>:    mov    -0x8(%rbp),%eax
  65.    0x0000000000401502 <+18>:    add    %eax,-0x4(%rbp)
  66.    0x0000000000401505 <+21>:    addl   $0x1,-0x8(%rbp)
  67.    0x0000000000401509 <+25>:    cmpl   $0x64,-0x8(%rbp)
  68.    0x000000000040150d <+29>:    jle    0x4014ff <main+15>
  69.    0x000000000040150f <+31>:    mov    -0x4(%rbp),%eax
  70.    0x0000000000401512 <+34>:    mov    %eax,%edx
  71.    0x0000000000401514 <+36>:    lea    0x2ae5(%rip),%rcx        # 0x404000
  72.    0x000000000040151b <+43>:    callq  0x402bc8 <printf>
  73.    0x0000000000401520 <+48>:    mov    $0x0,%eax
  74.    0x0000000000401525 <+53>:    add    $0x30,%rsp
  75.    0x0000000000401529 <+57>:    pop    %rbp
  76.    0x000000000040152a <+58>:    retq
  77. End of assembler dump.
  78. (gdb) info reg
  79. rax            0x1      1
  80. rbx            0x1      1
  81. rcx            0x1      1
  82. rdx            0x7147a0 7423904
  83. rsi            0x1b     27
  84. rdi            0x714740 7423808
  85. rbp            0x62fe50 0x62fe50
  86. rsp            0x62fe20 0x62fe20
  87. r8             0x712460 7414880
  88. r9             0x7147a0 7423904
  89. r10            0x0      0
  90. r11            0x246    582
  91. r12            0x1      1
  92. r13            0x8      8
  93. r14            0x0      0
  94. r15            0x0      0
  95. rip            0x4014fd 0x4014fd <main+13>
  96. eflags         0x202    [ IF ]
  97. cs             0x33     51
  98. ss             0x2b     43
  99. ds             0x2b     43
  100. es             0x2b     43
  101. fs             0x53     83
  102. gs             0x2b     43
  103. (gdb) x/48bx $rbp-48
  104. 0x62fe20:       0x01    0x00    0x00    0x00    0x00    0x00    0x00    0x00
  105. 0x62fe28:       0xc9    0x16    0x40    0x00    0x00    0x00    0x00    0x00
  106. 0x62fe30:       0x1b    0x00    0x00    0x00    0x00    0x00    0x00    0x00
  107. 0x62fe38:       0x1b    0x00    0x00    0x00    0x00    0x00    0x00    0x00
  108. 0x62fe40:       0xa0    0x3c    0xa8    0x23    0xfa    0x7f    0x00    0x00
  109. 0x62fe48:       0x01    0x00    0x00    0x00    0x00    0x00    0x00    0x00
  110. (gdb) print i
  111. $1 = 1
  112. (gdb) print sum
  113. $2 = 0
  114. (gdb) print &i
  115. $3 = (int *) 0x62fe48
  116. (gdb) print &sum
  117. $4 = (int *) 0x62fe4c
  118. (gdb)
复制代码



(gdb) print &i
$3 = (int *) 0x62fe48
变量 i 的地址 0x62fe48
0x62fe48:       0x01    0x00    0x00    0x00    0x00    0x00    0x00    0x00


(gdb) print &sum
$4 = (int *) 0x62fe4c
0x62fe48:       0x01    0x00    0x00    0x00    0x00    0x00    0x00    0x00

编译器替你初始化了,把变量 i 初始化成了 1,把变量 sum 初始化成了 0
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2018-6-25 21:53:02 | 显示全部楼层
如果你没有赋值,编译器会给你初始化一个值的,不同的编译器初始化的值不一样。
我记得Dev初始化的值是0,OJ上一般是随机初始化的。。。
以前好像是这样的,时间有点久,记不太清了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-25 21:53:22 | 显示全部楼层
int sum,i;//忘了给赋值

必须得先初始化,这是常识。有些编译器可能会帮你初始化。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-25 23:54:21 From FishC Mobile | 显示全部楼层
有的编译器会帮你初始化一个值。
比如dev-c++
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-26 10:12:25 | 显示全部楼层
人造人 发表于 2018-6-25 21:15
这是我的运行结果

谢谢谢谢,晚上不在没有看到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-26 10:13:23 | 显示全部楼层
ba21 发表于 2018-6-25 21:53
int sum,i;//忘了给赋值

必须得先初始化,这是常识。有些编译器可能会帮你初始化。

谢谢O(∩_∩)O
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 06:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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