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? 
How can I use printf command at C so that it appears only a specific number of digits or characters?
            Asked
            
        
        
            Active
            
        
            Viewed 65 times
        
    0
            
            
        - 
                    1You could perhaps use `sprintf` to check that the integer fits. – Basile Starynkevitch Apr 18 '15 at 13:26
- 
                    You might need to give an example *Athena*. – Iharob Al Asimi Apr 18 '15 at 13:27
- 
                    1possible duplicate of [Avoid trailing zeroes in printf()](http://stackoverflow.com/questions/277772/avoid-trailing-zeroes-in-printf) – Ivan Aksamentov - Drop Apr 18 '15 at 13:32
- 
                    How is the *character string* delimited? – chqrlie Apr 18 '15 at 13:49
2 Answers
-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
- 
                    
- 
                    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
 
    