问题
I am working in c after a long time.Here i have to achieve three functionality which includes
- get a number and show half
2.Get the square of the number
3.Get two number and show their summation and sabtraction.
I am using devC++ and when i compile the code i get the error i mentioned in the title which conflict type if squareInput
.What is wrong here:
#include<stdio.h>
#include<conio.h>
int main(){
float x;
printf("enter a number\n");
scanf("%f",&x);
//TASK 1 : display half of the number
pirntf("half of x is = %.3f",x);
//TASK 2 : square of number
squareInput(x); //call square function from here
// TASK 3 : get two numbers and display both summation and sabtraction
float num1,num2; // declare two floating number( floating numbers can hold decimal point numbers
printf("enter num1 \n");
scanf("num1 is =%f",&num1);
printf("enter num2 \n");
scanf("num2 is =%f",num2);
calculate(num1,num2);// call calculate function
getch();
}
float squareInput(float input){
float square=input*input;
printf("\n square of the number is %.3f \n",square);
return 0;
}
float calculate(float num1,float num2){
//summation
float summation= num1+num2; // declare antoher variable called summation to hold the sum
//sabtraction
float sabtraction=num1-num2;
printf("summation is %.2f \n",summation);
printf("sabtraction is %.2f \n",sabtraction);
return 0;
}
回答1:
Things will go wrong without prototypes. Add
float squareInput(float input);
float calculate(float num1,float num2);
in front of int main()
.
If you don't declare a function before it's called, the compiler assumes it as a int-returning function. However, squareInput()
return float, so the compiler(or linker, maybe) complains to you.
Also note that definitions are declarations(but not vice versa, obviously), so moving the definitions of squareInput()
and calculate()
in front of where they are called works too.
回答2:
At the time you call squareInput
and calculate
, they haven't been defined yet. So C assumes an implicit declaration of int squareInput()
and int calculate()
. These implicit declarations conflict with the definitions of these functions.
You can fix this by either adding declarations for each of these functions before main
:
float squareInput(float input);
float calculate(float num1,float num2);
Or by simply moving the functions in their entirety before main
.
回答3:
Be sure to add prototypes when you use a function. That way you do not need to worry too much about the order in which you call them.
Also try to separate your problems into smaller bits if you can. A comment like TAKS1 shows you that you actually want a function with that name.
#include <stdio.h>
//prototypes
void AskUserForOneNumer(float * number, const char * question );
void TASK_1(float x);
void TASK_2(float x);
void TASK_3(float a, float b);
int main()
{
float x, a, b;
AskUserForOneNumer(&x, "enter x");
AskUserForOneNumer(&a, "enter a");
AskUserForOneNumer(&b, "enter b");
TASK_1(x);
TASK_2(x);
TASK_3(a, b);
}
void TASK_1(float x)
{
printf("x = %g\n", x);
printf("0.5 * x = %g\n", 0.5 * x);
}
void TASK_2(float x)
{
printf("x = %g\n", x);
printf("x * x = %g\n", x * x);
}
void TASK_3(float a, float b)
{
printf("a = %g\n", a);
printf("b = %g\n", b);
printf("a + b = %g\n", a + b);
printf("a - b = %g\n", a - b);
}
void AskUserForOneNumer(float * number, const char * question)
{
float x;
printf("%s\n", question);
scanf("%f", &x);
printf("your input was %g\n", x);
*number = x;
}
来源:https://stackoverflow.com/questions/35435953/confliction-type-for-a-user-defined-function-in-c