Linux.com

Home Learn Linux Linux Answers Internet For a simple C program that contains bothe main and one function , can i write the main in a file and the function in a different file then the main will call the function and gives the appropriate result?
Answers
  • Assuming I understand you correctly, you can include a header and then be sure to include your implementation .c file in your compilation

    Answered by katrinaniolet
    5 years ago
    0 0
  • Little example:

    main.c:
    ------------------------
    #include"sum.h"
    int main()
    {
    int a = sum(5,5);
    }
    ------------------------

    sum.h:
    -------------------------
    int sum(int,int);
    -------------------------

    sum.c
    -------------------------
    int sum(int a,int b)
    {
    return a+b;
    }
    -------------------------

    g++ main.c sum.c -o sum

    Answered by thomasf
    5 years ago
    0 1
  • yes that's it.

    Answered by acoomans
    5 years ago
    0 0
  • [quote name="Thomas Fritz"]Little example:
    Hi thomas thanks for your answer but regarding the { sum.h} , where iam supposed to write it, i wrote the main in a file and the function in a separate file but dont know about the sum.h

    Answered by linuxstarr
    5 years ago
    0 0
  • You need three files

    main.c with the main function
    sum.h which is included into the main.c file
    In this file you declare the function int sum(int,int);
    The compiler needs this information to compile main.c
    it is a kind of promise that he will later find a file (in our case sum.c) where the
    function sum is implemented. Without the header file sum.h
    the compiler would complain not to know the function sum().

    Answered by thomasf
    5 years ago
    0 0
  • Just put the sum.c and the sum.h in the same directory than the main.c for now.
    Later, if you want to have them in different directories, you'll have to write a makefile or to include sum.h with the right path.

    Answered by vashou
    5 years ago
    0 0
  • An awesome website for advanced programming is [url]http://stackoverflow.com[/url]

    Answered by twoelectric
    5 years ago
    0 0
  • Sure you can,
    you can make 2 .C files, one include .H file and something like:
    #include "secondfile.c" in the first one.
    then you can compile them

    Answered by ben
    5 years ago
    0 0
Please register/login to answer this question. Click here to login
Similar Questions Found
Questions from category Internet

Upcoming Linux Foundation Courses

  1. LFD320 Linux Kernel Internals and Debugging
    03 Nov » 07 Nov - Virtual
    Details
  2. LFS416 Linux Security
    03 Nov » 06 Nov - Virtual
    Details
  3. LFS426 Linux Performance Tuning
    10 Nov » 13 Nov - Virtual
    Details

View All Upcoming Courses

Community Answers - Search


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board