Explain this C programme?

  • Thread starter rclakmal
  • Start date
  • Tags
    Explain
In summary: print "3" ... again, x was "3" at this level. print "4" ... again, x was "4" at this level.
  • #1
76
0
explain this C programme??

#include<stdio.h>
void func (int x);
void func(int x)
{
if (x>0)
func(x-1);
printf("%d",x);
}
int main()
{
func(4);
return 0;
}

this C programme gives the output of

0 1 2 3 4

can anyone explain me how this happens I am totally confused ...
 
Technology news on Phys.org
  • #2


No explanation will be nearly as useful as you working it out yourself. Trace the execution of your program by hand. What is the very first thing it does when it's started?
 
  • #3


yr i tried like that ,...but according to my dry run

first function get 4 and its greater than 0 and coz of that function get 3 like wise it goes until 1 then when the function get 1 and as it is also greater than 0 function get 0.Then 0 is NOT greater than 0 .so if loop is skipped then the 0 will be printed .but how on the Earth these 1234 gets print out ....i don't how it happens please help me ...please ignore my dumbness if this is a very easy one ...:D
 
  • #4


Think about flow of control. How do these functions get called exactly? Ever heard of recursion?
 
  • #5


Need more explanation .!Badly needed...!yr of course i have heard about recursion ...its about calling a function inside it self which is same as here ...but can u please help me with the dry run ...it will be a great help ...coz I am doing 6 subjects and don't have enough time to think about 1 for a long ...exam is coming on the way ...any help would be greatly appreciated.thanks!
 
  • #6


Speak English, please; save the txtk for texting.


I think you haven't internalized the notion of an execution stack. You've realized that func gets called five times, but it looks like you've mentally merged all of them together.

But that's not what happens -- each of the five calls to func give five completely different "frames", and you have to keep track of them all and keep them separate.
 
  • #7


Here's a simple example... call func with 2.

Code:
func(2)
...x=2
   x>2?
      yes, so...func(2-1)
                   ...x=1 ... this is a new "x" variable for this level.
                   ...x>0?
                         yes, so...func(1-1)
                                      ...x=0 ... this is another new "x" variable.
                                         x>0? no.
                                         print "0"
                   print "1"... remember, x was "1" at this level.
   print "2" ... again, x was "2" at this level.
 

1. What is the purpose of this C program?

The purpose of a C program can vary depending on the specific code, but in general, C programs are used for developing software and applications. They are also commonly used in operating systems and embedded systems.

2. Can you explain the syntax of this C program?

The syntax of a C program follows a specific structure, with a main function that acts as the starting point of the program. Statements and expressions are used to perform actions, and variables are used to store data. The code is written in a specific format and must be free of errors to run successfully.

3. How does this C program execute?

C programs are compiled into machine code before they can be executed. This means that the code is translated into instructions that the computer's processor can understand and execute. Once the code is compiled, the program can be run and the desired output will be produced.

4. What is the role of the header files in this C program?

Header files in C are used to provide the compiler with necessary information about functions and data types used in the program. They contain declarations for functions and variables, as well as any necessary preprocessor directives. Including the appropriate header files ensures that the program runs smoothly and without errors.

5. How do I debug this C program if it is not running correctly?

There are several tools and techniques that can be used to debug a C program, such as using a debugger, adding print statements to track the flow of the program, and analyzing the code for logical errors. It is also helpful to have a strong understanding of the code and its purpose in order to effectively troubleshoot any issues.

Suggested for: Explain this C programme?

Replies
2
Views
691
Replies
36
Views
3K
Replies
2
Views
6K
Replies
2
Views
412
Replies
1
Views
201
Replies
19
Views
885
Back
Top