Thread: ERROR: undefined reference to 'counter'

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    12

    ERROR: undefined reference to 'counter'

    it return an error as topic!!
    yet this code is not complete.
    Code:
    #include <stdio.h>
    int counter(int num);
    int x,digits;
    
    main()
    {
        int i;
            for(i=1;;i++)
                {
                    scanf("%d",&x);
                    digits = counter(x);
                     //   if(digits==5)
                      //      break;
                        //else
                          //  printf("enter 5 digits number!\n");
                            //continue;
                            printf("%d",digits);
                }
    
       int counter(int num)
            {
                int i,a=0;
                for(i=1;num>0;i++){
                    num/=10;
                    return i;
            }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Indent your code properly. You will find that counter is defined local to main (which is not standard C, I believe). Define it after defining the main function.
    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
    Feb 2012
    Posts
    12
    counter is a function which its prototype is b4 main!!! it should works any way ):

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    12
    i got it..yeah i should wrote my code better... tnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker Error - Undefined Reference
    By DaNxTh3xMaNx in forum C Programming
    Replies: 3
    Last Post: 09-07-2011, 05:49 PM
  2. gcc error, undefined reference
    By drshmoo in forum C Programming
    Replies: 2
    Last Post: 04-04-2011, 09:33 PM
  3. Undefined reference error
    By pshirishreddy in forum C Programming
    Replies: 11
    Last Post: 08-02-2009, 06:34 PM
  4. Undefined Reference Error
    By NuNn in forum C Programming
    Replies: 12
    Last Post: 01-15-2009, 01:34 PM
  5. undefined reference error
    By gcctest in forum C Programming
    Replies: 3
    Last Post: 12-19-2008, 08:17 AM