Home > Blockchain >  How can I fix the typeconflict?
How can I fix the typeconflict?

Time:09-04

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

CodePudding user response:

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.

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related