Display a float or double

Nico -  
 Nico -
Hello.

Can someone make me a simple little program that uses the write function to display a float or double with or without a sign?

For example, I would like to display a float a = -3.52.

I have a program that does this kind of thing, but it only works with ints.

Since I don't know how floats and doubles are coded... I'm a bit in trouble.

Thank you in advance.

11 answers

Nico
 
Uh but LOL, answers 6-7 years later :).

That's nice but I've been duly graduated for a while, thanks anyway ;).

By the way, I don't remember how I did it.... hehe
54
Bob
 
#include <stdio.h>
#include <stdlib.h>

signed int main()
{
float x;
x = -3.52;
printf("%f", x); // either "%f" or "%e" or "%g"
return EXIT_SUCCESS;
}

If you're interested, floating-point numbers are defined by the IEEE 754 standard. Do some research, and you'll learn more about how these numbers are coded, particularly their precision. There's no need to define the sign as they are all signed in their encoding.</stdlib.h></stdio.h>
5
Nico
 
LOL, that's exactly it, we don't have the right to use printf, it's too easy...

The goal of our exercise is precisely to recode the printf from the C library.
That's why I want to redo the display of floats.
2
Bob
 
Without hands too? :-D
1
Bob
 
Doing C without C is like coding without a keyboard, so unless you're having fun with interrupts in ASM, I don't see how to write standard code without printf.
1
Nico
 
And yet... I have to do it.

Otherwise, how can I display the character "%" with printf?

Actually, I'm looking for a manual page in French if possible for the options of printf.
1
Lifely
 
Ah ah, Epitech people are grilled from kilometers away :D

So for the others, educate yourselves about Epitech instead of saying that coding without the C library is worthless; it’s educational and you can never argue against it!!

Redo as many system functions as we do and then we can talk!!
I don’t think you would always think the same way!

%% --> Print %

And for floats, good question, there must be a way but personally for how many times you’re going to use it :D
In short, it’s the thing I do last!!
1
Lancazar Posted messages 11 Status Member 2
 
"Ah Ah, the people from Epitech are grilled from kilometers away :D"

You mean, because of their inflated ego?
How were we supposed to know that they don't want to use basic functions?

For floats, it's not very complicated: check out IEEE 754, you can then see that some bits contain certain information.
https://fr.wikipedia.org/wiki/IEEE_754

If you rewrite your printf function in the way that gcc does, for example, you have to use double precision floats.

Does that help you?
0
Inkognito
 
LoL it’s clear!!!

To display floats, you redo your my_put_nbr with floats indeed ;)
1
Nico
 
Oups, I forgot to specify that I needed it coded in C.
0
Nico
 
Yeah thanks, that helps me a lot.

But for my float display, does anyone have any ideas?
0
kiki
 
To display a float
float x;
x = -3.52;
printf("%f", x);

To display the %
printf("\%");
0
lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571 > kiki
 
Hello,

also
printf("%%");

--
lami20j
0