#include<stdio.h>
void function(int);
int main()
{
int x;
printf("Enter x:");
scanf("%d", &x);
function(x);
return 0;
}
void function(int x)
{
float fx;
fx=10/x;
if(10 is divided by zero)// I dont know what to put here please help
printf("division by zero is not allowed");
else
printf("f(x) is: %.5f",fx);
}
Asked
Active
Viewed 1.4k times
8

Aziz Shaikh
- 16,245
- 11
- 62
- 79

user244775
- 95
- 1
- 1
- 5
4 Answers
9
#include<stdio.h>
void function(int);
int main()
{
int x;
printf("Enter x:");
scanf("%d", &x);
function(x);
return 0;
}
void function(int x)
{
float fx;
if(x==0) // Simple!
printf("division by zero is not allowed");
else
fx=10/x;
printf("f(x) is: %.5f",fx);
}

Carlos
- 5,405
- 21
- 68
- 114
6
This should do it. You need to check for division by zero before performing the division.
void function(int x)
{
float fx;
if(x == 0) {
printf("division by zero is not allowed");
} else {
fx = 10/x;
printf("f(x) is: %.5f",fx);
}
}

Eric G
- 4,018
- 4
- 20
- 23
4
By default in UNIX, floating-point division by zero does not stop the program with an exception. Instead, it produces a result which is infinity
or NaN
. You can check that neither of these happened using isfinite
.
x = y / z; // assuming y or z is floating-point
if ( ! isfinite( x ) ) cerr << "invalid result from division" << endl;
Alternately, you can check that the divisor isn't zero:
if ( z == 0 || ! isfinite( z ) ) cerr << "invalid divisor to division" << endl;
x = y / z;

Potatoswatter
- 134,909
- 25
- 265
- 421
-
It's not a floating point divide by zero though - it's integer (the result of the integer division expression is cast to a float afterwards). – Paul R Mar 21 '10 at 10:20
-
@Paul: That is true in his code, but I didn't replicate his code. I added a comment, does that help? – Potatoswatter Mar 21 '10 at 11:29
1
With C99 you can use fetestexcept(2)
et alia.

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
-
1That's only for floating point exceptions though ? The example above is for an integer divide by zero. – Paul R Mar 21 '10 at 10:18