0

I have to write a program which "reads" a character string, an integer and a floating-point number. Then using the printf() command it must appear at least 3 but no more than 7 characters or digits for each input. How can this be accomplished?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Alice1nw0
  • 69
  • 1
  • 10

2 Answers2

-1

Would the section "Precision" of this page be usefull for you?

It says that you could use printf( "%.3f", 1.2348 ); to get the following output:

1.235

In your case you could use the precision specifier f with 7.

statox
  • 2,827
  • 1
  • 21
  • 41
  • That may produce 9 characters or more depending on the actual value of the floating point number. – chqrlie Apr 18 '15 at 13:48
-1

You can use fgets to limit the character read in input from the keyboard. Try this:

char string[7];
fgets(string, sizeof(string),stdin);
printf("%s\n",string);
nap.gab
  • 451
  • 4
  • 19
  • How are you going to read the integer and the floating point value? – chqrlie Apr 18 '15 at 13:47
  • i show you the way to read a string and limit its number of character. you can read the integer and the floating point in any way. – nap.gab Apr 18 '15 at 13:56
  • No, you merely show a way to stop reading from stdin after **6** characters... That's a long shot from the OP's goal. The question is: read a string, an integer and a floating point value from stdin, and output each in 3 to 7 characters using `printf()`. The question might be a little imprecise and confusing, but you are not addressing it at all. – chqrlie Apr 18 '15 at 15:27
  • Sorry, maybe i'm misunderstanding the question "How can I use printf command at C so that it appears only a specific number of digits or characters?" ........ – nap.gab Apr 18 '15 at 19:07