0

This short c program has a divide by zero error.

#include <stdio.h>

    int main( int argc, const char* argv[] )
    {
        printf( "start\n" );
        int x = 0;
        printf("%d", 2/x);
        printf("end\n");    
    }

When I compile this with cl /Od /Zi /EHsc main.c and run it in the console the output is just 'start'. No error message or popup or any information about the error. If I run it in visual code debugger it traps the divide by zero fine. I also compiled it using tcc and it is exactly the same.

Is this normal behaviour? If so is there any way I can at least display that the divide by zero has happened (outside of a debugger). If not what on my Windows 10 machine could be preventing the errors being shown?

Edit: Just to make it clear:

I would have thought I would have seen something like "Unhandled exception at 0x00f5144b in main.exe: 0xC0000094: Integer division by zero." (or similar) Exactly the same thing happens when I deref a null pointer, the program just stops with no error message.

Pajh
  • 33
  • 3
  • 4
    C is a very free language. It give you all the rope you need to make something very useful, or to hang yourself. That also means it's up to you, as the programmer, to make sure that the program doesn't do anything bad (like dividing by zero). – Some programmer dude Mar 16 '19 at 10:04
  • 1
    [See this](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division), in particular an italicized part in the end of the accepted answer. Integer division by zero is undefined behaviour, so you can't really do anything meaningful if it's happened - you're no longer in control. – Michail Mar 16 '19 at 10:37
  • Sorry, misunderstood the question. A windows valgrind analog ([here're](https://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows) some options) would probably be the best choice, otherwise [this](https://stackoverflow.com/questions/4672572/how-can-i-configure-windows-to-generate-a-core-dump-from-an-application) seems like it could work. I can't really comment on any of these, I have no windows machine to test them hands-on. – Michail Mar 16 '19 at 10:56

0 Answers0