為什么我收到warning: implicit declaration of function 'itoa'錯誤?我檢查了頭檔案和代碼。stdlib.h 已添加到指令中。我完全不知道為什么警告/錯誤不斷發生。
我需要將格式為 MM/YY 的給定 CHAR 日期更改為 YYYYMMDD 的數字。我使用了一個函式,它將 MM/YY 變數作為引數并將其分解為單獨的變數,然后將它們連接成一個變數。
#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;
}
uj5u.com熱心網友回復:
itoa標準 C 中沒有函式。您可以snprintf改用。
此外,atoi在標準 C 中可用,但不是一個好主意,因為它無法可靠地檢測錯誤。 strtol建議改為。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/356319.html
標籤:C
下一篇:有人可以向我解釋為什么c=1嗎?
