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 4 of 5.

Ganesh said:   1 decade ago
sir _ax variable is not declared in function how can it executes

Kiran said:   1 decade ago
How can prototype declaration of fun() done in main function ?

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.

KinG said:   1 decade ago
In C, _AX Denotes the Accumulator Where the result of Airthmatic & Logical Operation is stored.

Rathika.B said:   1 decade ago
What is _AX, same type of similar models & their explanations?

MANSIH said:   1 decade ago
What is _AX ?

Supriya said:   1 decade ago
In above given program, there is no return type in called fn. then how it return the value to calling fn?

Ravi said:   1 decade ago
Nice answer sundar.

Vishal said:   1 decade ago
Thanks Sundar for explaining in detail.

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!


Post your comments here:

Your comments will be displayed after verification.