Thread: Inline keyword

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    113

    Inline keyword

    Hi everyone,

    I couldn't decide where I should use the inline keyword: in function prototype of the function definiton.

    Code:
    #include <stdio.h>
    int func(int x, int y);
    
    int main(void){
        int a,b;
        printf("Enter values for x and y\n");
        scanf("%d",&a);
        scanf("%d",&b);
        int c=func(a,b);
        printf("%d",c);
        return 0;
    }
    
    inline int func(int x, int y){
        return (x+y)*(x+y);
    }
    Code:
    #include <stdio.h>
    inline int func(int x, int y);
    
    int main(void){
        int a,b;
        printf("Enter values for x and y\n");
        scanf("%d",&a);
        scanf("%d",&b);
        int c=func(a,b);
        printf("%d",c);
        return 0;
    }
    
    int func(int x, int y){
        return (x+y)*(x+y);
    }
    These both works but when I use inline in both of them it doesn't work.

    Code:
    #include <stdio.h>
    inline int func(int x, int y);
    
    int main(void){
        int a,b;
        printf("Enter values for x and y\n");
        scanf("%d",&a);
        scanf("%d",&b);
        int c=func(a,b);
        printf("%d",c);
        return 0;
    }
    
    inline int func(int x, int y){
        return (x+y)*(x+y);
    }
    As my understanding I should use it either in prototype or definetion but not both. However is there any difference between using it in prototype or definition?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ditch the prototype and just use the function definition as a declaration.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Quote Originally Posted by laserlight View Post
    Ditch the prototype and just use the function definition as a declaration.


    Thank you very much for your reply. All the examples I have seen is like you said. However I just wanted to learn the detail for the use of both.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    To avoid some other complexities, you may also want to make it static (i.e., "internal" instead of "external").
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by GokhanK
    All the examples I have seen is like you said. However I just wanted to learn the detail for the use of both.
    Well, if you declare that a function to be inline, it means that you're hinting to the compiler that inlining it is a good idea. For the compiler to take the hint, the function definition must be available, hence the usual approach of having a prototype in a header and the definition in a source file no longer makes sense.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    By the way, if you want to keep your original organization (with prototype), static inline should work

    Code:
    static inline int func(int x, int y);
     
    int main(void) {
        int a,b;
        printf("Enter values for x and y\n");
        scanf("%d",&a);
        scanf("%d",&b);
        int c=func(a,b);
        printf("%d",c);
        return 0;
    }
    
    static inline int func(int x, int y)
    {
        return (x+y)*(x+y);
    }
    The advantage of this way is that you can place several prototypes together for documentation purposes into one header, and then place all of the bodies into a different header. This way makes it easier to use in my opinion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'new' keyword
    By pktcperlc++java in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2005, 09:31 PM
  2. asm keyword
    By misplaced in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2004, 04:35 PM
  3. asm keyword???
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 12-18-2003, 12:36 PM
  4. the far keyword
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2002, 08:27 PM
  5. new keyword
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-31-2001, 11:58 AM