C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 1)
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;
}
Garbage value
0 (Zero)
1990
No output
Answer: Option
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).

Discussion:
43 comments Page 1 of 5.

Sundar said:   1 decade ago
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!

Adu said:   1 decade ago
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.

Tanishka said:   1 decade ago
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.

NRB said:   1 decade ago
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.

Richik said:   1 decade ago
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?

BhavadipGothadiyaBG said:   8 years ago
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.
(2)

Nitish anand said:   1 decade ago
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.

Ruku said:   7 years ago
@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).
(1)

Sanju said:   9 years ago
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?

Swapnil said:   9 years ago
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.


Post your comments here:

Your comments will be displayed after verification.