这个函数如何工作? [英] How does this function foo works?

查看:55
本文介绍了这个函数如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此C程序将获取存储在变量a中的值并逐个打印。



This C program will take the value stored in the variable a and print them one by one.

#include <stdio.h>
   void foo(int n, int sum)
   {
     int k = 0, j = 0;

     if (n == 0)
       return;

     k = n % 10;
     j = n / 10;

     sum = sum + k;

     foo (j, sum);

     printf ("%d, ", k);
   }

   int main ()
   {
     int a = 2048, sum = 0;
     foo (a, sum);
     printf("\n");
     return 0;
   }





输出:





Output:

2, 0, 4, 8,







有人可以解释一下这个程序有效:



1)如何将打印值存储在< a 中?



2)按此顺序:2,0,4,8,?



3)为什么总和的值 k 的值时,b>正在改变吗?



我尝试了什么:



当函数foo执行时:



1)第一次:n = 2048, k = 8 ,j = 204,sum = 8



2)第二次:n = 204, k = 4 ,j = 20,sum = 12



3)第三次:n = 20, k = 0 ,j = 2,sum = 12



4)第四次:n = 2, k = 2 ,j = 0,sum = 14



如果我更换线(存在于foo函数中):






Can someone please explain how this program works:

1) How it's printing value stored in <a?

2) And in this order: 2, 0, 4, 8, ?

3) Why is the value of sum is changing when we're printing values of k?

What I have tried:

When the function foo executes:

1) For the first time: n = 2048, k = 8, j = 204, sum = 8

2) For the second time: n = 204, k = 4, j = 20, sum = 12

3) For the third time: n = 20, k = 0, j = 2, sum = 12

4) For the fourth time: n = 2, k = 2, j = 0, sum = 14

If I replace the line (present in the foo function):

printf ("%d, ", k);





这个:





with this:

printf ("%d | %d, ", k, sum);





输出:





Output:

2 | 14, 0 | 12, 4 | 12, 8 | 8,

推荐答案

引用:

有人可以解释一下这个程序是如何工作的:

Can someone please explain how this program works:



当你看到时,为什么用文字向你解释自己做什么程序以及如何使用调试器,它是一个令人难以置信的学习工具。



当你不明白你的代码在做什么或为什么它完成了它的工作,答案是调试器

使用调试器查看你的代码在做什么。只需设置一个断点,看看你的代码是否正常运行,调试器允许你要执行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ] <无线电通信/>
掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [< a href =https://www.youtube.com/watch?v=C0vDKXIq_9Atarget =_ blanktitle =New Window> ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助您。当代码没有达到预期的效果时,你就会接近一个错误。


Why explain to you with words when you can see by yourself what is doing the program and how. Use the debugger, it is an incredible learning tool.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于这个函数如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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