Divided By Zero: What Happens When it Occurs on a System

“Divided by Zero” exception, what is this? This is a good question and you must know the answer which will lead you to know a system better. When I was a beginner and learning Embedded Systems Design, I came across this situation when I was programming in C. Now I got a clearer picture on this and I can explain you better.

divided by zero exception in c programming

What really happens When the processor encounters a Divided By Zero statement:

What is a divided by zero statement?

See here:
int a;
a = a/0;

The programmer is trying to store a value of itself after dividing by zero. Any value divided by zero is an undefined value. There is no answer for this.
I used gcc version 4.8.2 (GCC) to compile both the below code.

Lets see how the compiler behaved to some of the divided by zero scenario:

Code #1:


int main () {
    int a;

    a = a/0;

    return 0;

When I compiled the above code I got the following WARNING from GCC:

a.c: In function 'main':
a.c:6:10: warning: division by zero [-Wdiv-by-zero]
     a = a/0;

Here you can see that the compiler has caught the C statement that involves a zero in the denominator. It generated a Warning but created the executable a.out.
See the output when I run the program:

Floating point exception

Code #2:

Now see another case where I am not taking the 0 in the denominator part in a C statement directly as in case #1. But I will take a variable stored with a value of zero and I will use this variable as a denominator and see how the compiler behaves to this.


main () {
    int a = 0, b = 5;

     * The compiler should have seen that the denominator has a value of 0 in the variable
     * and no other statement is updating the value of the variable a in between.
     * So the compiler should should have caught this but it did not.
    a = b/a;

    return 0;

When I compiled the above code the compilation was SUCCESS and I got NO WARNING at all from GCC, wohoo… Now what was that?
The compiler was unable to catch the divided zero statement at all.

But the divided by zero exception is still there. See the output when I run the program:

Floating point exception

I believe these are two such simple scenarios where as a programmer we can figure out the conditions easily but the compiler was able to catch only the first scenario. Even there are complex situations where you can encounter the divided by zero at runtime conditions, which is definitely not the case the compiler should handle.

The complex scenarios might be common with global or static variables where the variables are exposed to multiple functions. Lets think that int global is one such global variable and is accessed from function a() and b(). Think that inside a() the int global is set to zero for some condition and the function b() uses the int global as a denominator in some statement. What will happen when the division statement is encountered in function b(): a divided by zero exception.

Who generates Divided By Zero Exception and how is it handled in Linux:

A Divided by Zero Exception is generated by a division instruction. All division instructions are passed to the Floating Point Unit (FPU) to get the result.
Why to the FPU only?? Because when you divide two things you get a floating point value. For example: 5/2 will give you 2.50 upto two decimals which is a floating value.

This Exception is delivered to the kernel via an Exception. As part of this exception handling the kernel does two things:

  • If the Divided by Zero instruction was executed on behalf a user application then the program will be terminated.
  • If the Divided by Zero instruction was executed on behalf a kernel module or inside kernel (for example inside a Device Driver or some Subsystem etc.) then the kernel will generate a pnic or the kernel will crash.

I think this will give you basic idea on Divided By Zero exception and how is this handled in Kernel. If you still have some doubt on this then you can place it on the comment box below.

Leave a Comment