Home > OS >  Getting a warning "[-Wimplicit-int]"
Getting a warning "[-Wimplicit-int]"

Time:11-09

Getting an error saying

calculator.c:11:9: warning: type of 'num1' defaults to 'int' [-Wimplicit-int] 11 | int addition(num1, num2)"

The code will still run but wondering why this comes up?

#include <stdio.h>

int main()
{
    int num1;
    int num2;

    int addition(num1, num2)
    {
        return num1 num2;
    }

    int subtraction(num1, num2)
    {
        return num1-num2;
    }

    int multiply(num1, num2)
    {
        return num1*num2;   
    }

    float devide(num1, num2)
    {
        return num1/num2;
    }

    printf("Still works");

}

CodePudding user response:

First, do not put your functions inside main. Each function should be standalone:

#include <stdio.h>

int addition(num1, num2)   // Function BEFORE main, not inside main
{
    return num1 num2;  
}


int main()
{
    int num1;
    int num2;
    ....
}

Secondly, when declaring your functions, give a type to each parameter:

int addition(int num1, int num2)   // Declared each parameter as "int"
{
    return num1 num2;  
}

Finally, for long term code-usability, be consistent and correct with names.

// You named the first two functions addition and subtraction
// so this one should be "multiplication", not "multiply"!
// A sudden change in naming convention means poor design!
int multiply(num1, num2)


// The proper spelling of the operation is "divide" or "division"
// You will make other programmers jobs extremely difficult
// if they cannot tell what function this is, because of a misspelling.
float devide(num1, num2)

Putting all these changes together:

#include <stdio.h>


int addition(int num1, int num2)
{
    return num1 num2;
}

int subtraction(int num1, int num2)
{
    return num1-num2;
}

int multiplication(int num1, int num2)
{
    return num1*num2;   
}

float division(int num1, int num2)
{
    return (float)num1/num2;
}

int main()
{
    int num1;
    int num2;

    printf("Still works");
    return 0;
}
  •  Tags:  
  • c
  • Related