Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make function local to main only?

Tags:

c++

c

objective-c

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?

like image 581
Sadique Avatar asked Dec 13 '25 04:12

Sadique


1 Answers

  1. Mark the function as static (this makes it local to the current translation unit).

  2. For the love of god, do not include .c files! (read me)

like image 174
fredoverflow Avatar answered Dec 14 '25 18:12

fredoverflow