1

I am a new C developer (I am used to programming in Java), and have tried create, what I thought was a simple bool function. Although I am getting an error which I don't understand how to fix:

#include <stdio.h>
#include <stdlib.h>
typedef enum { false, true } bool;
int main() {
    int currentNumber, round = 1;
    printf("Numbers generated will be between 1 and 20. \n");
    currentNumber = rand() % 20;
    bool validNumber = false;
    do {
        if(currentNumber != 0) {
            validNumber == true;
        } else {
            currentNumber = rand() % 20;
        }
    }while(validNumber == false);
    printf("You're on round" + ("%d", round));
    printf("You're current number is: " + ("%d", currentNumber));
    printf("Higher or Lower (H/L)?");
    char userInput [20];
    scanf("%s", &userInput);
    if((userInput[0] == 'h') || (userInput[0] == 'H')) {
        completeRound(round, 'H', currentNumber);
    } else if((userInput[0] == 'l') || (userInput[0] == 'L')) {
        completeRound(round, 'L', currentNumber);
    }
}

void completeRound(int round, char input, int currentNumber) {
    int initialVal = currentNumber, newVal;
    if(input == 'H') {
        newVal = rand() % 20;
        bool checkResult(initialVal, newVal, input);
    } else {
        newVal = rand() % 20;
        bool checkResult(initialVal, newVal, input);
    }
}

bool checkResult(int initialVal, int finalVal, char input);
bool checkResult(int initialVal, int finalVal, char input) {
    if(input == 'H') {
        if(initialVal <= finalVal) {
            return true;
        } else {
            return false;
        }
    }
    if(input == 'L') {
        if(initialVal >= finalVal) {
            return true;
        }else {
            return false;
        }
    }
    printf("An error has occurred! Aborting game...");
    return false;
}

The error is as follows:

\main.c|39|error: conflicting types for 'checkResult'

At first, I thought that for some reason, in C you could only pass certain data types as arguments to a bool method, although I can not find a straight answer to this on Google. Other than that; I can not understand what it means by "conflicting types" (this is the first time I've debugged a C program.

The function I have used to call checkResult is as follows:

9
  • 1
    Do you have some other definition of checkResult somewhere? In Java you can have a checkResult(arg1, arg2) and a checkResult(arg1, arg2, arg3) function. In C this is not allowed. Commented Jul 21, 2015 at 9:23
  • Nope, just the one, with those arguments. Commented Jul 21, 2015 at 9:23
  • Show the code around your call to checkResult, especially the declarations of initialVal, newVal and input. BTW your checkResult function doesn't compile, there are typos. Commented Jul 21, 2015 at 9:35
  • @MichaelWalz I have just added to function used to call checkResult. Commented Jul 21, 2015 at 9:38
  • 1
    The call should be just value = checkResult(initialVal, newVal, temp). With bool checkResult(...) you actually declare a prototype with all arguments using a default type of int. (It is legal in C to declare functions inside other functions. It is more usual to declare them in headers or at the beginning of the file.) Commented Jul 21, 2015 at 9:41

3 Answers 3

3

Before calling the function you need to write its prototype also. By default compiler is considering it as return type of int but actually it is bool.

so write bool checkResult(int initialVal, int finalVal, char input) before calling checkResult.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your quick response! Although I am still getting the same error when I write the prototype?
where did you write the prototype? is it before the definition? does all your argument type are same as definition and prototype?
as per latest update from you , i would suggest you to write code in below order - prototype for checkResult, definition of checkResult and then completeRound
please put bool checkResult(int initialVal, int finalVal, char input); above main.
2

You probably have a typo in your code. The line

bool checkResult(initialVal, newVal, temp);

implicitly creates a prototype for a bool function. The types of the arguments are omitted and default to int in C versions prior to C99. This declaration is in conflict with the actual declaration, whose third parameter is of type char.

You probably meant something like this:

bool okay = checkResult(initialVal, newVal, temp);

This defines a bool variable okay and initialises it with the result of the function call. (But note that this variable is local to the current scope, so in your example you'd lose the result immediately.)

It is legal in C to declare a function inside a function body, although it is not good practice. It is more usual to declare them in headers or at the beginning of the file.

As of C99, implicit function declarations are invalid. There also isn't a default argument or function return type of int. You might consider to enforce the C99 standard (eg with -std=c99in gcc) to avoid falling into the implicit-declaration trap.

Comments

1

You have called functions before declaring them.So is the error. Because by default the return type of a c function is "int". Add

void completeRound(int , char , int );

and

 bool checkResult(int , int , char);

after your typedef (better this way than declaring them in body of the calling function).

And since checkResult() is returning a value of type bool you better assign it to a variable of type bool like

bool okay = checkResult(initialVal, newVal, temp); this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.