0
我的程序由於某種原因沒有編譯。我有2個C文件,我編譯它們以下列方式:gcc showxbits.c xbits.c -o showxbits -lm
。而且,我得到以下錯誤:無法編譯C文件
showxbits.c: In function ‘main’:
showxbits.c:18:3: warning: incompatible implicit declaration of built-in function ‘strcpy’ [enabled by default]
我的第一個文件名爲XBITS,它下面是:
/*
* stubs for functions to study
* integer-hex conversions
*
*/
#include <stdio.h>
#include <math.h>
#include "xbits.h"
/*#include "/home/carl/Programs/C/lab2/cheungr/hw2/solution/reverse.c"
/* function represents the int n as a hexstring which it places in the
hexstring array */
void itox(char hexstring[], int n) {
hexstring[2*sizeof(n) + 1];
int ratio, remainder;
int i = 0;
while(ratio != 0)
{
ratio = n/16;
remainder = n % 16;
if (remainder == 10){
hexstring[i] = 'A';
i++;
}
else if (remainder == 11){
hexstring[i] = 'B';
i++;
}
else if (remainder == 12){
hexstring[i] = 'C';
i++;
}
else if (remainder == 13){
hexstring[i] = 'D';
i++;
}
else if (remainder == 14){
hexstring[i] = 'E';
i++;
}
else if (remainder == 15){
hexstring[i] = 'F';
i++;
}
else
hexstring[i] = remainder;
}
i++;
hexstring[i] = '\0';
printf("in itox, processing %d\n",n);
}
/* function converts hexstring array to equivalent integer value */
int xtoi(char hexstring[]) {
int i, integer;
int length = strlen1(hexstring);
for (i = length-2 ; i >= 0 ; i--)
{
integer += (int) pow((float) hexstring[i] , (float) i);
}
/*printf("in xtoi, processing %s\n", hexstring);*/
return integer;
}
int strlen1 (char line[])
{
int i;
i=0;
while (line[i])
++i;
return i;
}
和我的第二個文件是如下:
/*
* stub driver for functions to study integer-hex conversions
*
*/
#include <stdio.h>
#include "xbits.h"
#define ENOUGH_SPACE 1 /* not really enough space */
int main() {
char hexstring[ENOUGH_SPACE];
int m=0, n = 0x79FEB220;
itox(hexstring, n);
/* for stub testing: create a fake input string */
strcpy(hexstring, "6BCD7890");
m= xtoi(hexstring);
printf("\t%12d %s %12d\n", n,hexstring, m);
return 0; /* everything is just fine */
}
你有什麼錯誤?警告不會失敗編譯 – tristan
每當您第一次使用庫函數時,對Google'man function_name'來說是很好的選擇。不僅有助於教您如何使用函數本身,還可以引導您包含正確的頭文件集,這是BTW正確使用庫函數的一部分。 – smRaj
使用字符串函數時,請添加#include頭文件。 –
vkulkarni