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

Lingaraj said:   1 decade ago
@Shilpa.

In Linux the output will be zero.

Shilpa said:   1 decade ago
What will be the result in LINUX?

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

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.

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

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.

ADDY said:   1 decade ago
@Deepak.

Yes a function can be defined another function.

Deepak said:   1 decade ago
Can a function be declared inside another function?

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.

Jayanthi said:   1 decade ago
What is the difference between return; and return 0; and also return();?

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


Post your comments here:

Your comments will be displayed after verification.