C Programming - Functions - Discussion

1. 

What will be the output of the program in 16 bit platform (Turbo C under DOS)?

#include<stdio.h>

int main()
{
    int fun();
    int i;
    i = fun();
    printf("%d\n", i);
    return 0;
}
int fun()
{
    _AX = 1990;
}

[A]. Garbage value
[B]. 0 (Zero)
[C]. 1990
[D]. No output

Answer: Option C

Explanation:

Turbo C (Windows): The return value of the function is taken from the Accumulator _AX=1990.

But it may not work as expected in GCC compiler (Linux).


Amit said: (Sep 16, 2010)  
What is accumulator?

Amit said: (Dec 1, 2010)  
Accumulator is a place where the results are stored after calculation.

Rakesh said: (Feb 23, 2011)  
But the function doesnt have any return value / statement so how could this be possible according to me there must be a garbage value can give a fulfledged explanation if this ques?

Sundar said: (Feb 24, 2011)  
Compile and Run the following program in Turbo C, you will get 35 as output. It may not work in other compilers.

#include<stdio.h>
int main()
{
int a = 0;
a = 35;
printf("%d");
}


Assume the address of a = 1200.
Assume the address of video memory = 5500;

MOV AH, 35
MOV [1200], AH
MOV [5500], AH // prints on the screen.

This is the way of execution takes place. After copying the value 35 to location 1200, AH retains the value 35.

Then printf("%d") tries to get the value from AH and sends to video memory to display it on screen.

If we use printf("%d %d", age, salary), the value of age transfered to AH before using that value to send to video memory. Then the value of salary is moved to AH then send to video memory.

Assume,
Address of age = 1200;
Address of salary= 1202;
Address of video memory = XXXX; (It will changed according to no. of chars printed on the screen, dont think much about this address value)

MOV AH, [1200]
MOV [XXXX], AH
MOV AH, [1202]
MOV [XXXX], AH

I hope this will help to understand the solution for the given program.

Have a nice day!

Vishal said: (Jun 11, 2011)  
Thanks Sundar for explaining in detail.

Ravi said: (Aug 14, 2011)  
Nice answer sundar.

Supriya said: (Sep 3, 2011)  
In above given program, there is no return type in called fn. then how it return the value to calling fn?

Mansih said: (Sep 26, 2011)  
What is _AX ?

Rathika.B said: (Jan 11, 2012)  
What is _AX, same type of similar models & their explanations?

King said: (Feb 19, 2012)  
In C, _AX Denotes the Accumulator Where the result of Airthmatic & Logical Operation is stored.

Nrb said: (Jun 11, 2012)  
How compiler knows _AX is accumulator and there is no return value to calling function. As explained above there is no need get the any value from outside functions. So it is clear for particular code and if same program will run in GCC compiler I got output 1. Please explain clearly. Thanks in advance.

Kiran said: (Jul 23, 2012)  
How can prototype declaration of fun() done in main function ?

Ganesh said: (Aug 25, 2012)  
sir _ax variable is not declared in function how can it executes

Jayanthi said: (Sep 16, 2012)  
What is the difference between return; and return 0; and also return();?

Where to use void main(), int main() and main()?

Tanishka said: (Oct 3, 2012)  
void main() is used when we dont have to return value because void is a return type and it does not return any value.

int main() is used when we want to return any value this will return integer type value by the os.

You can also use float before main() function if you do so it returns float type value.

Deepak said: (Oct 6, 2012)  
Can a function be declared inside another function?

Addy said: (Oct 18, 2012)  
@Deepak.

Yes a function can be defined another function.

Nitish Anand said: (Sep 1, 2013)  
There should be a return type in the function and is it possible to declare a function inside the main function. As per me during compilation when function is encountered in the main then compiler will have no information of the function at that time. So it will give compile error.

Abhayraj said: (Oct 28, 2013)  
Accumulator, data transfer bet memory locations are used in micro-controllers, IC chips, devices like washing machine & so many.

These instructions are namely used in hardware prog -ALP (assembly prog lang). AM I right ?

Adu said: (Jan 20, 2014)  
I have no idea why people call it accumulator what I think is this: There are many registers in the cpu and %EAX is just one among them which is used to place return values and store the syscall number while performing a syscall.

Look at this

int foo(){
__asm__("mov $0xff, %eax;");
}
main(){
printf("%d",foo());
}

Here I have put a inline asm instruction in it and what it does is moving the value 255 to register eax and you get 255(0xff) printed in main :).

That means EAX register is the place where the return value is stored.. instead of 'return value' they placed the value in the AX register directly(by _AX=blabla) ..nothing much.. this is what I think ..may be wrong.

Moon said: (Mar 25, 2014)  
Why this is not working? if we use any kind of variable assume 'p' instead of _AX then also the result is 0. Why it is? please give an explanation of this program.

Shilpa said: (Feb 25, 2015)  
What will be the result in LINUX?

Lingaraj said: (May 4, 2015)  
@Shilpa.

In Linux the output will be zero.

Richik said: (Aug 24, 2015)  
In function 'fun':

Line 13: error: '_AX' undeclared (first use in this function).

Line 13: error: Each undeclared identifier is reported only once.

Line 13: error: For each function it appears in.

Encountering these errors, on copying the given code. How did you guys get the answer as 1990?

Neeraj said: (Oct 11, 2015)  
How can a function be called in main without mentioning its prototype before the main()?

Thamizharasi said: (Feb 16, 2016)  
Actually it shows an error because the function is defined outside the main function. To show an output the function will be declared before main function.

Raju said: (Jul 15, 2016)  
'_AX' is undeclared so it should not give any output.

Sanju said: (Aug 19, 2016)  
First of all, in the function definition, there is no return statement. So error1===no return statement.

_AX is not declared. So error2=='_AX' undeclared (first use in this function).

How will it give output?

Can anyone help me?

Ahmedhalim said: (Nov 5, 2016)  
This code will print garbage value because it isn't has return.

Syntaxerror said: (Jan 4, 2017)  
In the above given program, there is no return type in called fn. Then how it return the value to calling function?

And why this c compiler showing an error?

Ashok said: (Jan 27, 2017)  
How can prototype declaration of fun() done in main function?

Swapnil said: (Feb 18, 2017)  
First, declare the _AX as integer variable in fun function then, return the _AX from fun Function in order to get the same output. But _AX is an accumulator which we can not access directly in GCC compiler. Why so? Please explain.

Manish Kulkarni said: (Apr 3, 2017)  
As there is no return statement then how it will return value to calling the function?

Gulshan Kumar said: (May 21, 2017)  
In this program, fun has no any return type so according to me, it returns garbage value.

Jit said: (Aug 6, 2017)  
The answer is wrong. [Error] '_AX' was not declared in this scope.

Prerna said: (Sep 3, 2017)  
In the function, fun we didnot write return ax so how will the value be passed in main?

Bhavadipgothadiyabg said: (Sep 26, 2017)  
Here If we assume the int _AX.

Then output of the program is 1.


#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int fun();
int i;
i = fun();
cout<<i;
return 0;
}
int fun()
{
int _AX = 1990;
}

How 1990? Explain Please.

Sam said: (Nov 1, 2017)  
Function "fun" is defined after the main function. So where is the function prototype. Also there is no return statement available present in the fun function.

Harish Mahajan said: (Oct 16, 2018)  
If the compiler is GCC Linux then what will be the output?

Please, anyone, tell me.

Harish Mahajan said: (Oct 16, 2018)  
What is the difference between return 0, return 1?

What is void main(), int main(), main()?

Ruku said: (Dec 4, 2018)  
There is no need to define the return (); keyword here because the return value of the function is taken from the Accumulator _AX=1990 itself internally.

Ruku said: (Dec 4, 2018)  
@All.

Read the question carefully they have mentioned: "Turbo C under DOS" that means there is no need to define return in windows c. The return value of the function is taken from the Accumulator _AX=1990.

But it may not work as expected in GCC compiler (Linux).

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.