extern for functions


 
Thread Tools Search this Thread
Top Forums Programming extern for functions
# 1  
Old 05-28-2007
extern for functions

Hi,

Please let me know if the extern keyword is necessary for using functions which is defined and declared in another file and and used in a different file where both these files are linked together.

thanks
# 2  
Old 05-28-2007
No. You don't have to do this.
Code:
# cat test.c
#include<stdio.h>

int main(int argc, char *argv[]){
        adder(5,6);
        exit(0);
}
# cat test1.c
#include<stdio.h>
void adder(int a,int b) {
        fprintf(stdout,"%d\n",a+b);
}
# cc test.c test1.c
# ./a.out
11

A rudimentary example, but works.
# 3  
Old 05-28-2007
extern is not needed for functions, but is needed when refering to external data to avoid duplicate declaration of statics which can be very hard to track down.

Prototypes however are a good idea and required even by K&R if the function does not return an int.

It is good practice and warms you up for when you start mixing C with C++.
# 4  
Old 06-01-2007
Oh God ! I always thought that if a function in a file file1.c calls another function func2 in file file2.c, then we must include prototype of func2 in file2.h and include it in file1.c

file1.c
Code:
#include<stdio.h>
#include "file2.h"
main()
{
   func2();
}

file2.c
Code:
#include<stdio.h>
func2()
{

}

file2.h
Code:
void func2();

# 5  
Old 06-01-2007
Quote:
Originally Posted by the_learner
Oh God ! I always thought that if a function in a file file1.c calls another function func2 in file file2.c, then we must include prototype of func2 in file2.h and include it in file1.c
It is good practice to do so.

Also, file2.c should include file2.h to confirm the actual function agrees with your public interface.

I also recommend compiling with maximum warnings on to help catch errors, with gcc this would be "-Wall -Werror". While some may claim you don't technically need to in many cases, the errors that it catches outweigh any time saved not using prototypes.

Last edited by porter; 06-01-2007 at 05:29 PM..
# 6  
Old 06-09-2007
Quote:
Originally Posted by the_learner
Oh God ! I always thought that if a function in a file file1.c calls another function func2 in file file2.c, then we must include prototype of func2 in file2.h and include it in file1.c

file1.c
Code:
#include<stdio.h>
#include "file2.h"
main()
{
   func2();
}

file2.c
Code:
#include<stdio.h>
func2()
{

}

file2.h
Code:
void func2();

Also, don't put #include <stdio.h> in file1. and file2.c. Just make it so they both include file2.h, and #include <stdio.h> in file2.h.
# 7  
Old 06-09-2007
Quote:
Originally Posted by Octal
Just make it so they both include file2.h, and #include <stdio.h> in file2.h.
You could do, but you don't need to put every include file under the sun in file2.h, only put include files required to support the types used in file2.h.

Typically you have an include file common to the project which includes all the include files required by most files and common types.

Typically with UNIX you try and include the minimum number of include files you need to support your project, where as on Windows, windows.h includes almost everything you could think of.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Extern keyward on function in C

I saw a header (.h) file with mixture of "regular" function declarations and other extern function declarations. As I was told all function declarations are implicitly external and the extern on functions declarations is superfluous. Here my focus is on function declaration, not variable yet. int... (2 Replies)
Discussion started by: yifangt
2 Replies

2. Programming

C header file and extern

In the header file data.h i got: const char ack_msg = "ack: received your msg\n"; In the code file server.c i got: extern const char ack_msg; And else it is only used in a function call: user$ grep ack_msg *c *h server.c:extern const char ack_msg; server.c: n = write(clientsfd,... (5 Replies)
Discussion started by: tornow
5 Replies

3. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

4. Programming

Functions

Hi All, Can any one help me. I am calling in a function2 with string as parameter from function1, the function1 gives 3 values. how i get the 3 values from funciton2 to function1. i have to give a return or something. thanks in advance. (2 Replies)
Discussion started by: uday.sena.m
2 Replies

5. Shell Programming and Scripting

i think i need functions ?

Hi, im making a little script but need some help Code i have so far is read -p 'Bot Nickname:' ecnick read -p 'Bot Username:' ecusername read -p 'Bot Realname:' ecrealname read -p 'Your Email:' ecemail echo '' echo Your bots nickname is set to $ecnick echo Your bots username is set to... (2 Replies)
Discussion started by: Gemster
2 Replies

6. Programming

segmentation fault for extern

Why this is happening when both of them compiled together and run? I am getting segmentation fault SIGSEGV. File1.c: int arr; File2.c: extern int *arr; int main() { arr = 100; } (3 Replies)
Discussion started by: royalibrahim
3 Replies

7. UNIX for Dummies Questions & Answers

fetchmail and forward to an extern address

Hi, I'm searching for an solution for the following problem. I want fetch some mails via pop3 from a@a.com with fetchmail. That works perfectly. Now any incoming mail should forwarded to b@b.com via smtp obv. But I don't know how to configure that. All online tutorials describe forwarding to... (0 Replies)
Discussion started by: mcW
0 Replies

8. Linux

Problem mounting extern hd (fedora 9)

Hi there, I'm having a bit of a strange problem which I would appreciate some help with. The Problem: I have two external hard drives, but I'm borrowing one off my parents to copy data too (one of mine, which is identical to theirs - WD MyBook 300g - is on its way out). Fedora 9 recognizes... (3 Replies)
Discussion started by: lasthidingplace
3 Replies

9. Programming

Extern variable.

file1.c int a1; int main() { a1 = 2; printf("\na1 = %d\n", a1); next(); printf("\na1 = %d\n", a1); next1(); printf("\na1 = %d\n", a1); } file2.c #include <stdio.h> int b1 = 0; void next(void) (1 Reply)
Discussion started by: Tanvirk
1 Replies

10. Shell Programming and Scripting

Regarding functions

Hi, I have a function or script like this. show() { echo "Hi" } | tee -a log show This creates a logfile and prints Hi in it. Now when I try to do the same for sql like this: show() { sqlplus -s scott/tiger<<! select * from details; ! } | tee -a log show Then it gives me a... (2 Replies)
Discussion started by: sendhilmani
2 Replies
Login or Register to Ask a Question