Thread: undefined symbols error when using itoa on macbook pro

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    44

    undefined symbols error when using itoa on macbook pro

    does anybody know why this piece of code is not compiling?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int palindromic(int number);
    
    int main() {
    	int greatest = 0;
    /*	for (startValue1 = 999; startValue1 >= 0; startValue1--) {
    		for (startValue2 = 999; startValue2 >= 0; startValue2--) {
    			if (palindromic(startValue1 * startValue2))
    				greatest = startValue1 * startValue2;
    		}
    	} */
    	int i = 1021;
    	palindromic(i);
    	return 0;
    }
    
    int palindromic(int number) {
    	char numstr[7]; // '\0'
    	itoa(number, numstr, 10);
    	printf("%s\n", numstr);
    	return 0;
    }
    the error should be located in the palindromic function

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    itoa() is a non-standard function. My gcc doesn't have it and I guess your compiler doesn't either. Instead of using it I would suggest sprintf().
    Code:
    int palindromic(int number) {
    	char numstr[7]; // '\0'
    	sprintf(numstr, "%d", number);
    	printf("%s\n", numstr);
    	return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    44
    oh thank you very much thought it was part of stdlib..
    how come i can compile atoi but can't compile itoa lol

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, I don't even like atoi() because it doesn't do much error checking. strtol() does much the same thing and it's more flexible. But I agree that it's strange that atoi() made it into the standard and itoa() did not . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just for fun, does it work with _itoa, instead of itoa?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM