Thread: implicit declaration of function warning

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    implicit declaration of function warning

    here is a rough view of my code (original is far to big to post here)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int break_loop = 1;
        
        for (;;)
        {
            while (break_loop == 1)
            {
                //ask player for old co-ordinates
                //ask user for new co-ordinates
                //do stuff with co-ordinates
                //if everything ok break_loop = 0
            }
            //call a function
        }
        return 0;
    }
    when i compile i get a warning implicit declaration of function although everything works fine as far as i can tell.

    apart from being an oxymoron what does the warning mean and should i be worried about it.
    coop

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is good to simplify your code so that you find the smallest and simplest program that demonstrates the warning... but your simplified code has to demonstrate the warning. Yours doesn't.

    The warning means you called a function without declaring it. You should either include the relevant header or provide a forward declaration of the function, or define the function before calling it.
    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
    Apr 2019
    Posts
    808
    oops forgot to include the header file...... duh!!!!
    that's why when i tried to mimic the issue i didn't get it
    thanks for the help
    coop

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    This isn't what you were asking about, but there are a few better ways you could arrange those loop.

    Code:
    for (;;) {
        for(int loop = 1; loop;) {
            // ...
            if(ok) loop = 0;
        }
        func();
    }


    Move the variable that controls the loop in as far as you can get it. Limiting the scope of your variables as much as possible is always a good thing. I also renamed it, boolean variables should have a positive meaning. If we want to keep looping, loop is true. Having a variable called "break loop" that breaks the loop when it's 0 is more than a little confusing, someone (possibly future you) is going to come along and make an incorrect assumption about the purpose of that variable and possibly cause a bug.

    Or... well, just the break keyword.

    Code:
    for (;;) {
        for(;;) {
            // ...
            if(ok) break;
        }
        func();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: implicit declaration of function 'scanf_s'
    By Bucephalus01 in forum C Programming
    Replies: 3
    Last Post: 08-21-2016, 06:54 AM
  2. Replies: 2
    Last Post: 08-23-2010, 09:18 PM
  3. gcc warning: implicit declaration of function
    By anatoly in forum C Programming
    Replies: 4
    Last Post: 05-02-2010, 04:32 PM
  4. warning: implicit declaration of function ‘malloc’
    By yougene in forum C Programming
    Replies: 3
    Last Post: 01-09-2009, 03:18 AM
  5. Replies: 12
    Last Post: 10-24-2007, 08:10 AM

Tags for this Thread