![](/img/trans.png)
[英]Casting an unsigned long long int to signed long long int Is it possible?
[英]previous implicit declaration: (casting int to unsigned long)
在我項目的這個特定區域中,我負責信息發布,他在命令行中執行不同的操作。 即使程序按原樣正確執行,我也會收到此錯誤。
lab2.c:86: error: conflicting types for ‘rrotate’
lab2.c:67: error: previous implicit declaration of ‘rrotate’ was here
這是函數及其調用位置。
呼叫:
else if(strcmp(action, compare_r) == 0)
{
/* unsigned long rrotate(unsigned long x, int n)
this function will implement a right rotate function
that returns the value of the unsigned long x rotated to the
right by n bit positions*/
strcpy(e,argv[3]);
int n = strtol(e,0,10);
hex_num = (unsigned long)hex_one;
unsigned long number = rrotate(hex_num,n);
print_bits(number);
}
功能:
unsigned long rrotate(unsigned long x, int n){
unsigned long number = x >> n;
return number;
}
我通過使用gcc lab2.c -o lab2進行編譯來運行程序,然后像lab2 -r 0x5 3一樣運行
程序:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char*argv[])
{
char action[5];
char compare_p[] = "-p";
char compare_i[] = "-i";
char compare_u[] = "-u";
char compare_c[] = "-c";
char compare_r[] = "-r";
char compare_s[] = "-s";
char compare_m[] = "-m";
char hex[10],e[5];
char *ptr;
unsigned long i,hex_num;
int hex_one,hex_two;
/*argc is the argument count-http://www.tutorialspoint.com/c_standard_library/c_function_atol.htm*//*It is the number of arguments passed into the program from the command line, including the name of the program.*/
if (argc < 3)
{
printf("NADA");
}
else
{
strcpy(action, argv[1]);
strcpy(hex,argv[2]);
hex_one = strtol(hex, &ptr, 16);
if(strcmp(action, compare_p) == 0)
{
print_bits(hex_one);
}
else if(strcmp(action, compare_u) == 0)
{
printf("do this instead");
}
else if(strcmp(action, compare_i) == 0)
{
/*create an intersection function*/
}
else if(strcmp(action, compare_c) == 0)
{/*complement funtion
0x7 -->> 1111 1111 1111 1111 1111 1111 1111 1000*/
hex_one = ~hex_one;
print_bits(hex_one);
}
else if(strcmp(action, compare_r) == 0)
{
/* unsigned long rrotate(unsigned long x, int n)
this function will implement a right rotate function
that returns the value of the unsigned long x rotated to the
right by n bit positions*/
strcpy(e,argv[3]);
int n = strtol(e,0,10);
hex_num = (unsigned long)hex_one;
unsigned long number = rrotate(hex_num,n);
print_bits(number);
}
else if(strcmp(action, compare_s) == 0)
{
}
else if(strcmp(action, compare_m) == 0)
{
}
}/*END ELSE*/
return 0;
}
unsigned long rrotate(unsigned long x, int n){
unsigned long number = x >> n;
return number;
}
print_bits(unsigned int i)
{
int x;
int count = 0;
for(x=(sizeof(int)*8)-1; x>=0; x--)
{
(i&(1<<x))?putchar('1'):putchar('0');
count+=1;
if(count == 4)
{
printf(" ");
count = 0;
}
}
printf("\n");
}
將rrotate
的定義rrotate
main
函數之前,或者在main
函數之前添加一個聲明。 編譯器實際上在調用rrotate
時聲明了它。
在C99標准之前,允許通過調用而隱式聲明函數,而無需任何先前的聲明(函數原型)。 然后,編譯器將通過檢查調用中使用的類型來猜測函數的參數。
但是由於C99標准實際上是不允許的,因此您需要在調用函數之前聲明一個函數。 可以通過在調用之前添加函數原型來非常簡單地完成此操作,因此您應該具有以下內容:
// Function *declaration*
unsigned long rrotate(unsigned long x, int n);
int main(...)
{
...
}
// Function *definition*
unsigned long rrotate(unsigned long x, int n)
{
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.