0

From a Fortran Code, I intend to run a C code (to read a file) and fetch the read information. The main program is the Fortran code, which uses a function written in C to do the processing. In this C code, is it necessary to run a main function?

curious
  • 3
  • 1
  • You mean, to use a function from a library written in C from Fortran? (I haven't really touched Fortran, but I have no reason to think you wouldn't be able to.) – Hasturkun Aug 19 '15 at 12:23
  • 3
    possible duplicate of [Calling C Code from FORTRAN](http://stackoverflow.com/questions/354182/calling-c-code-from-fortran) – jpw Aug 19 '15 at 12:25

2 Answers2

5

If Fortran only calls the C function, then the C code does not need a main() function.

The main() function of C is the program entry point. The system loads the .exe, transfers control to the startup code, which address is mentioned in the .exe file (the startup code is called crt, C run-time start-up). The run-time startup does initialization and then transfers control to the user code. The user code entry point is main().

Since Fortran is the main program, no C run-time start-up is needed and so no main() is needed.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
2

No, you don't need a main in your C code. The linker will use the main from the FORTRAN code, or rather, the FORTRAN equivalent of main, when linking your C functions to the FORTRAN program.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82