Results 1 to 4 of 4

Thread: What is value of printf("%d",12.5);

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Aug 2007
    Answers
    8

    Post What is value of printf("%d",12.5);

    Dear friends,
    I am facing a problem in my small code. I was just wondering that what is the output of the code and why did it happen.

    The code is

    #include<stdio.h>
    main()
    {
    float i=12.5;
    printf("%d",i);
    getchar();
    }


    It gives me 0 as output.
    But i am confused why is it so.

    Please help me in finding out the reason behind it.


  2. #2
    Junior Member
    Join Date
    Mar 2008
    Answers
    5

    Re: What is value of printf("%d",12.5);

    Its giving as 0 that is due to the change in formats specified in both declaration and printf statements...
    It is to be noted if u declare an integer variable and in printf if u give %f then also a garbagge value is output.


  3. #3
    Junior Member
    Join Date
    Aug 2007
    Answers
    8

    Re: What is value of printf("%d",12.5);

    but can I know what exactly happened in the storage of floats? how are floats stored in memory?


  4. #4
    Junior Member
    Join Date
    Mar 2008
    Answers
    5

    Wink Re: What is value of printf("%d",12.5);

    actually when u give
    float i=12.5;
    4 byte of memory is allocated for the float variable.But when u give
    printf("%d",i); it accesses the first 2 bytes of the float variable (as it is now asked to o/p the integer i .Hence you get to see a 0 not 12.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact