Beginner question - printf and scanf 
Author Message
 Beginner question - printf and scanf

I am getting input from the user with scanf - and I don't want to limit the
number of characters he/she is inputting, ie, I am allowing up to 15
characters.  Then I want to put that data in a printf statement *but* only
send to the screen the first character that the user inputted...  how the
heck would I do that?

I've read the manual and searched on the web high & low, albeit perhaps not
in the right direction!

Thanks folks.



Fri, 15 Nov 2002 03:00:00 GMT  
 Beginner question - printf and scanf

Quote:

> I am getting input from the user with scanf - and I don't want to limit the
> number of characters he/she is inputting, ie, I am allowing up to 15
> characters.  Then I want to put that data in a printf statement *but* only
> send to the screen the first character that the user inputted...  how the
> heck would I do that?

Don't use scanf() for user input, if the input isn't what you expect,
you can get into serious trouble. It's better to use fgets():

char buffer[40]; /* let's be generous */
if(fgets(buffer, sizeof buffer, stdin) != NULL)
{
    putchar(buffer[0]);
    /* or: */
    printf("%c", buffer[0]);

Quote:
}

Gergo

--
Heisenberg might have been here.



Fri, 15 Nov 2002 03:00:00 GMT  
 Beginner question - printf and scanf
Solar Max is of the opinion:

Quote:
>I am getting input from the user with scanf - and I don't want to limit the
>number of characters he/she is inputting, ie, I am allowing up to 15
>characters.  Then I want to put that data in a printf statement *but* only
>send to the screen the first character that the user inputted...  how the
>heck would I do that?

Don't do it with scanf - you'll leave yourself irrevocably open to bufer
overflow attacks and other such interesting possibilites.

As to printing out the first character, %c and * come to mind.

--
-----------------------------------------------------------------------
#include <disclaimer.h>
Matthew Palmer



Sat, 16 Nov 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

2. Simple scanf/printf question

3. tiny question on format of scanf and printf

4. Beginner printf, cprintf question - HELP!

5. Beginner printf, cprintf question - HELP!

6. skipped printf/scanf ???

7. Examples of format strings for scanf/printf ?

8. scanf/printf conversions

9. pointers in scanf and printf

10. printf() and scanf()

11. casting printf/scanf

12. Q:printf() and scanf()....

 

 
Powered by phpBB® Forum Software