简体   繁体   中英

How can I fix the typeconflict?

I wrote this program and get the following error messages:

sub.h:1:6: warning conflicting types for built-in function 'isdigit' bool isdigit(char c)

In file included from sub.c:1:0:
sub.h:1:1: error: unknown type name 'bool'
 bool isdigit(char c)

sub.h:1:6: warning: conflicting types for built-in function 'isdigit'
 bool isdigit(char c)

sub.c:7:6: error: conflicting types for 'isdigit'
 bool isdigit(char c)

In file included from sub.c:1:0:
sub.h:1:6: note: previous declaration of 'isdigit' was here
 bool isdigit(char c)
//main.c
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<stdlib.h>
#include "sub.h"

#define N 120

int main (void){

    char eingabe[N+1]={0};
    int zahlen[10]={0};

    fgets(eingabe,N,stdin);

    for(int i=0; i<N; i++){
        if(isdigit(eingabe[i]))
        {
            int zahl = toNumber(eingabe[i]);
            zahlen[zahl]= zahlen[zahl]+1;
        }
    }

    for (int j=0; j<10; j++)
    {
        printf("%d: %d \n",j, zahlen[j]);
    }
    return 0;
}
// sub.h
bool isdigit(char c);
int toNumber(char c);
//sub.c
#include "sub.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

bool isdigit(char c){
    bool Antwort = false;
    for(int i='0'; i<= '9'; i++){
        if(c==i){
            Antwort = true;
        }
    }
    return Antwort;
}

int toNumber (char c){
    int num =0;
    num = c- '0';
    return num;
}

As the error messages state, your function isdigit is conflicting with a built-in function with the same name.

This means you need to rename this function to something else that doesn't conflict with the built-in function.

The first error tells you that there already is an isdigit function, it's a built-in function of the C standard library. You must change the identifier (name). Also include stdbool.h in your sub.h file. Otherwise include stdbool.h in all your C files before including sub.h . This is to fix the unknown type bool error, by including stdbool.h before sub.h the type is defined and you can use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM