Jump to content

c float

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Nyfer

Nyfer

    Newbie

  • Members
  • PipPip
  • 19 posts
hey guys please help me in this

in the below code

#include<stdio.h>


void main()

{

float a=10.5;

float x=20.78;

printf("a=%d",a);

printf("x=%d",x);

getch();

}

am getting a=0 and garbage value for x. why is that can anyone please explain me
thanks

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
use %f in the printf() format specifier, not %d, which is for integers.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
Nyfer

Nyfer

    Newbie

  • Members
  • PipPip
  • 19 posts
ya i know that, but can you explain me why am getting a=0 and x=37833827 value??

#4
voral

voral

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Because the variables of different types have different representation in memory, and can also vary in size.
This function does not convert the input parameters, and reads as an int.
Your code is not equivalent to "printf("x=%d",(int)x);"

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
floating point data types store information differently than integer ones. Since, as far as I know, C can't tell of which type a variable is (in va_list, which is used in printf function), the function figures it out by looking at the format specifier.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
It may become clearer if you run this:

#include <iostream>

#include <stdio.h>


using namespace std;


int main()

{

    float a=10.5;

    float x=20.78;


    printf("Size of int....: %d\n", sizeof(int));

    printf("Size of double.: %d\n\n", sizeof(double));


    printf("a=%d\n",a);

    printf("a=%f\n",a);

    printf("x=%d\n",x);

    printf("x=%f\n",x);

    std:getchar();


    return 0;

}


#7
Nyfer

Nyfer

    Newbie

  • Members
  • PipPip
  • 19 posts
sizeof int and float is same.
can you please explain me

#8
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
My bad, I put double. The problem is not the size of the variable in memory, the problem is how printf handles variables. Since printf does not do any conversions on it's own, the results are based on what you send to it in the text. The results might possibly be different on different machines. In memory, the representation for a float looks nothing like the representation for a signed int.

#9
Nyfer

Nyfer

    Newbie

  • Members
  • PipPip
  • 19 posts
ok thank you:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users