先前的隐式声明:(将int转换为unsigned long)

时间:2015-09-19 20:04:15

标签: c casting unsigned

在我的项目的这个特定领域,我正在接受信息前端命令行做不同的事情。即使程序按原样正确执行,我也会收到此错误。

lab2.c:86: error: conflicting types for ‘rrotate’
lab2.c:67: error: previous implicit declaration of ‘rrotate’ was here

这是函数及其调用的位置。

CALL:

  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");
    }

2 个答案:

答案 0 :(得分:1)

rrotate函数之前移动main的定义,或在main函数之前添加声明。当你调用它时,你的编译器实际上会声明rrotate

答案 1 :(得分:1)

在C99标准之前,允许通过在没有任何先前声明(函数原型)的情况下调用来隐式声明函数。然后,编译器将通过检查调用中使用的类型来猜测函数参数。

但是,由于C99标准不允许这样做,因此您需要在调用之前声明一个函数。这可以通过在调用之前添加函数原型来非常简单地完成,因此您应该具有类似

的内容
// Function *declaration*
unsigned long rrotate(unsigned long x, int n);

int main(...)
{
    ...
}

// Function *definition*
unsigned long rrotate(unsigned long x, int n)
{
    ...
}