Menu

Code is givin me the most baffling output eve

padfootpak
2010-04-29
2012-09-26
  •  padfootpak

    padfootpak - 2010-04-29

    include<iostream>

    #include<conio.h>
    #include<stdio.h>
    
    using namespace std;
    
    int main()
    {
    
        float divided;
        divided = 9/10;
        cout<<"divided = "<<divided;
        getch();
        return 0;
    }
    

    why is this code giving me output 0. i am using Dev C++ ver. 4.9.9.2

     
  • cpns

    cpns - 2010-04-29

    Because

    9 / 10
    

    is an integer operation with result zero. Zero implicitly cast to a float is
    still zero!

    Correction:

    divided = 9.0f / 10.0f ;
    

    Note the f suffix explicitly makes the literal constants type float,
    otherwise they are double, and there would be an implicit cast in the
    assignment.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.