Home > Software design >  C program error: warning: implicit declaration of function 'itoa'
C program error: warning: implicit declaration of function 'itoa'

Time:11-12

Why am I getting the warning: implicit declaration of function 'itoa' error? I've checked the header files and code. stdlib.h already added in directives. I'm totally lost as to why the warning/error keeps occurring.

I need to change a given CHAR date of format MM/YY to a NUMBER of YYYYMMDD. I used a function that takes the MM/YY variable as a parameter and breaks it down into separate variables, then concatenates them into a single variable.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int getNumDate (char *date);
    

int main()
{
    char prodDate[5   1];
    strcpy(prodDate,"03/12");
    
    printf("%d", getNumDate(prodDate));
    
    return 0;
}


int getNumDate (char *date)
{
  static char orig_mmyy[5   1];
  static char *str_ptr;
  static char *c_yy = NULL;
  static char *c_mm = NULL;
  static char *c_dd = NULL;
  char new_yy[4   1];
  char new_mm[2   1];
  char new_dd[2   1];
  strcpy(new_dd, "01");
  char finalDate_chars[8   1];
  int finalDate;
 
  strcpy(orig_mmyy, date);//store original MM/YY
  str_ptr = orig_mmyy;//copy MM/YY to str_ptr
  c_mm = strtok(str_ptr, "/");//store MM
  c_yy = strtok(NULL, "/");//store YY
  c_dd = "01";//set DD
  if( atoi(c_yy) < 65 )//1965 is the Go Date -- less than 65 must be 21st Century
  { 
      //****ERROR OCCURS HERE****//
      itoa(atoi(c_yy)   2000, new_yy, 10);//convert YY to YYYY and copy to new YYYY
  }
  else
  {
     itoa(atoi(c_yy)   1900, new_yy, 10);//convert YY to YYYY for 20th Century (1900s)
  }
  memcpy(new_mm, c_mm, sizeof(new_mm));//copy new MM
  memcpy(new_dd, c_dd, sizeof(new_dd));//copy new DD
  
  strcat(finalDate_chars,new_yy);
  strcat(finalDate_chars,new_mm);
  strcat(finalDate_chars,new_dd);
  finalDate = atoi(finalDate_chars);
  
  return finalDate;
}   

CodePudding user response:

There is no itoa function in standard C. You can use snprintf instead.

Also, atoi is available in standard C, but not a good idea to use because it has no way to reliably detect errors. strtol is recommended instead.

  •  Tags:  
  • c
  • Related