Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1045235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:57:12+00:00 2026-05-16T15:57:12+00:00

Let us say i have File1.c: #include<stdio.h> #includeFile2.c void test(void) { sum(1,2); } int

  • 0

Let us say i have

File1.c:

#include<stdio.h>
#include"File2.c"

void test(void)
{
sum(1,2);
}

int main(void)
{
int sum(int a,int b);
test();
sum(10,20);
return 0;
}

File2.c:

int sum(int x,int y)
{
printf("\nThe Sum is %d",x+y);
}

Now as far as my understanding goes test() calling sum() should give a Compile-Time Error since i have made/declared sum() local to main, which i am not getting, and the program is running fine without any errors.

My main purpose is to define sum in File2.c and make it local to main() so that no other function has visibility to this function sum().

Where am i going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:57:13+00:00Added an answer on May 16, 2026 at 3:57 pm

    Prototypes are helpful when compiling as they tell the compiler what a function’s signature is. They are not a means of access control, though.

    What you want to do is put sum() into the same source file as main() and give it static linkage. Declaring it static means it will only be available in that one .c file, so functions in other source files will be unable to call it.

    Then move test() to another source file. That’ll let main() call test() but not let test() call sum() since it’s now in a different source file.

    File1.c

    #include <stdio.h>
    
    /* NO! Do not #include source files. Only header files! */
    /*** #include "File2.c" ***/
    
    /* Prototypes to declare these functions. */
    static int sum(int a, int b);
    void test(void);
    
    int main(void)
    {
        test();
        sum(10, 20);
        return 0;
    }
    
    /* "static" means this function is visible only in File1.c. No other .c file can
     * call sum(). */
    static int sum(int x, int y)
    {
        printf("\nThe Sum is %d", x + y);
    }
    

    File2.c

    void test(void)
    {
        /* Error: sum() is not available here. */
        sum(1, 2);
    }
    

    Notice, by the way, that I commented out the line #include "File2.c". You should never use #include for .c source files, only for .h header files. Instead you will be compiling the two source files separately and then linking them together to make the final program.

    How to do that depends on your compiler. If you’re using an IDE like Visual C++ on Windows then add the two source files to a project and it will take care of linking them together. On Linux you’d compile them with something like:

    $ gcc -o test File1.c File2.c
    $ ./test
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have 2 source files: main.c: #include <stdio.h> #define i 2 int
I have this code below #include <stdio.h> int main(){ int i=0; for(i=0; i<1000; i++)
Let's say I compiled the application below and stripped it's symbols. #include <stdio.h> int
Let's say I have a php file, test.php with 2 functions: test1() and test2().
Let's say I have a test.html file with some example content: <table> <tr> <td>'Test'</td>
Let's say I have a file located at C:\foo\bar.js and I want to include
Let's say i have a header file Snake.h: #include SnakeBodyPart.h #include GUI.h //... And
Let's say I have a header file called inclusions.h that has all the #include
Let's say that I have a binary that I am building, and I include
Let's say I have a singleton class like this: class Settings include Singleton def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.