LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2005, 11:15 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
inline function means


Hi can u plz explain me what inline function means in c ?
Whats the difference with extern?
Can u provide me some examples when to use the one or the other?
 
Old 08-22-2005, 11:27 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Hmm, I assume inline functions are the same in C as they are in C++. When you inline a function, wherever that function is called, the code is copied there as if you had written it yourself. For example:

Code:
int Double(int x)
{
      return 2*x;
}

int main()
{
    int x = 2;
    int y = Double(x);
    return 0;
}
If you had declared Double() to be inline, the compiler would change your main() method to look like:

Code:
int x = 2;
y = 2 * x;
Generally, you only declare functions as inline if they contain a small number of statements (1 or 2, possibly a few more).

Last edited by Nylex; 08-22-2005 at 11:30 AM.
 
Old 08-22-2005, 11:39 AM   #3
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Inline code is also important in functions that will get called quite frequently. You can get quite an advantage by avoiding the function calls (pushing flags, pointers on stack, creating new stack, jumping, reversing the procedure, etc.)

This is common in loops where the same function may be called in multiple ways... it avoids functions calls, but allows you to update the same code all in once place.
 
Old 08-22-2005, 03:42 PM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
And how about extern? When we use it?
 
Old 08-26-2005, 02:47 AM   #5
mehuljv
Member
 
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
hi alaios,
extern can be used to declare a function whose definition is some where else. so that compiler will not give you any warning.

Mehul.
 
Old 08-26-2005, 01:08 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
You should not use extern with functions (as far as I'm aware).

Externs should be used for VARIABLES defined in another c file.
For example:
Code:
/* file1.c */
#include <stdio.h>
int status;
void checkstatus();

int main(int argc,char **argv){
    status=argc;
    checkstatus();
    return 0;
}
Code:
/* file2.c */
#include <stdio.h>
extern int status;
void checkstatus(){
    printf("%d\n",status);
}
 
Old 08-29-2005, 11:44 AM   #7
mehuljv
Member
 
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
hi
there is nothing like you should not use extern for function, just traverse linux source code, u will find extern declartion of function at so many places.

Mehul.
 
Old 08-29-2005, 11:55 AM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Sorry, I should have been more clear.

The keyword 'extern' is mandatory when referring to variables (as a variable definition and a variable declaration are the same) but optional when referring to functions. Generally, extern is used for a function from another file, the extern keyword is left off if the function is in the same file.
 
Old 08-29-2005, 12:17 PM   #9
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
functions can be defined as "extern inline", which has a special meaning: http://www.greenend.org.uk/rjk/2003/03/inline.html

It's also worth noting that, even when you declare a function inline, gcc *won't* inline it unless you give at least -O.
 
Old 08-31-2005, 07:03 AM   #10
prasanram
LQ Newbie
 
Registered: Jul 2002
Location: india(tirunelveli(tamilnadu)))
Posts: 3

Rep: Reputation: 0
alaios ,
Do remember that the member functions defined inside the class definition scope in c++, will be treated as a inline function Only !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
inline function and macros in C++ carthyc Linux - General 2 05-14-2005 12:16 AM
Defining a non-inline function inside class definition (tm) Programming 6 05-11-2005 09:14 AM
what does it means with..."too many argument to function..."? ams Programming 2 03-10-2005 12:49 AM
[SOLVED] C++ inline function: Use or don't use? druuna Programming 7 09-13-2004 04:08 PM
gcc: compilation with extern inline function philipsyyy Linux - Software 0 10-13-2002 11:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration