Zack Zack - 1 month ago 5
C Question

What is the run time error in this program?

When Running this program it crushes at some point and i can't find why any help? I've been literally looking at it for the past 30 min and i can't quite find it

#include <stdio.h>

void main(void)
{
int loop_counter = -7;
int input = 9;
char c1 = '9';
char c2 = 43;

while(input == 9)
{
printf("%d", loop_counter+1);
printf("%d", loop_counter+2);

printf("%d", loop_counter);
printf("%d", loop_counter+1);
printf("%d", loop_counter+2);

int b = 4*loop_counter;
int a = 4/loop_counter;
double c = loop_counter / 9;
printf("%d", loop_counter);
printf("%d", loop_counter+1);
printf("%d", loop_counter+2);

if (loop_counter > 10)
{
input = 10;
}

loop_counter++;
}

printf("loop exit\n\n");
getchar();
}

Answer

You have loop_counter starting at -7 which then increases on each iteration. When loop_counter is equal to 0, you then do this:

int a = 4/loop_counter;

Which is division by zero, and causes a floating point exception.

Either add a check for 0 at this point, or remove the line altogether since the value of a is never being used.

Comments