How can i see "divide error" ?

This is a discussion on How can i see "divide error" ? within the C Programming forums, part of the General Programming Boards category; hello to all, I am new to c programming and i have encountered some very frustrating problem. so i was ...

  1. #1
    bkd
    bkd is offline
    Registered User
    Join Date
    Sep 2013
    Posts
    10

    Question How can i see "divide error" ?

    hello to all,
    I am new to c programming and i have encountered some very frustrating problem.
    so i was suppose to write a program that gives a divide error but output is not showing anything like it.
    I am using orwell devc++ 5.4.2 and all the default built tools(gcc compiler etc.) with default settings.
    so, what should i do to see this error?

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        int a,b,c;
        float x;
    
        a=250;
        b=85;
        c=25;
        x=a/(b-c);
        printf("x = %f\n",x);
    
        a=300;
        b=70;
        c=70;
        x=a/(b-c);
        printf("x = %f\n",x);
    
        getch();
        return 0;
    
    }
    thanks for reading it!

    any suggestions are welcome!
    Update-
    output by orwell devc++
    Name:  Dev-C++ 5.4.2_2013-09-23_01-51-30.png
Views: 162
Size:  2.0 KB
    output by borlands turbo c++ ver3
    Name:  clip_image002.jpg
Views: 155
Size:  11.7 KB
    As you can see borland's tc++ compiler does generate this error. But i still don't know why gcc doesn't do this.
    Attached Images Attached Images  
    Last edited by bkd; 09-22-2013 at 02:38 PM. Reason: added new elements

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Run fails at my pc, as I expected to do, because of the division by zero. What is your problem exactly? You want to track down this kind of division? If so, just use an if statement and check if denominator is zero. If yes, print out division error, else do the division.

    Welcome to the forum.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if you want to make a float division you need to cast at least one of participating ints to float

    Code:
    x=a/(b - (float)c);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    bkd
    bkd is offline
    Registered User
    Join Date
    Sep 2013
    Posts
    10
    @std10093
    thank for reply!
    nah! Its just an exercise in 1st chapter of the book that i am reading and the solution code is suppose to generate this error, that's it.
    by the way shouldn't be a compiler like gcc would do this.
    Last edited by bkd; 09-22-2013 at 03:08 PM.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    What Vart said is true, but I did not mentioned it, because your code (it happened now, but maybe not next time you want to do floating division) works as integer division, because the number do not have any decimal points.

    "-Wdiv-by-zero
    Warn about compile-time integer division by zero. This is default. To inhibit the warning messages, use -Wno-div-by-zero. Floating point division by zero is not warned about, as it can be a legitimate way of obtaining infinities and NaNs."
    Source: http://gcc.gnu.org/
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Floating point divide errors don't generally "throw" an exception unless you change the default behavior.
    Since you're just looking for a "divide error", by which I assume you mean a runtine "arithmetic exception" then you could use integers instead of floats.
    Or do you have your heart set on doing it with floats?

    EDIT: Oh ... you are using integers.
    Last edited by oogabooga; 09-22-2013 at 02:49 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    bkd
    bkd is offline
    Registered User
    Join Date
    Sep 2013
    Posts
    10
    hey, thanks for all the replies guys!
    but the post is looking broken. I tried to re-edit it but nothing happens.
    sorry about that.

  8. #8
    bkd
    bkd is offline
    Registered User
    Join Date
    Sep 2013
    Posts
    10
    @oogabooga

    its okay!
    I just need to know that is it the way gcc compiler behaves?
    or
    I accidentally made any changes.(because borland did show that error)
    only then i could move on...lol

  9. #9
    bkd
    bkd is offline
    Registered User
    Join Date
    Sep 2013
    Posts
    10
    @std10093

    -Wdiv-by-zero
    Its suppose to be default, as you mentioned but i gave the command explicitly to compiler and still no success.
    I think i should move on to keep up with my reading.......maybe its my settings or ide issue etc.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Firstly, the -Wdiv-by-zero warning's effect is only at compile time, not runtime. Secondly, it will only warn for divisions by zero that it can spot.

    So this gives a "division by zero" warning from the compiler.
    Code:
    int main(void)
    {
        int a = 1 / 0;
        return 0;
    }
    This doesn't give a compiler warning, but will cause an arithmetic exception at runtime (assuming no optimizations).
    Code:
    int main(void)
    {
        int a, b = 1, c = 0;
        a = b / c;
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  2. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  3. "Segment Violation" error, fopen("r+")
    By jiboso in forum C Programming
    Replies: 1
    Last Post: 03-10-2011, 08:57 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21