Re: C code
- From: Richard <rgrdev@xxxxxxxxx>
- Date: Mon, 12 Nov 2007 02:08:14 +0100
"Default User" <defaultuserbr@xxxxxxxxx> writes:
Bill Cunningham wrote:
Richard can you or anyone else tell me what's wrong with this code.
It's just a simple error I'm sure.
Which "Richard"? There are least four regulars I can think of.
#include <stdio.h>
int main(int argc,char *argv[]){
char name[]="Enter Code -> ";
printf("%s",&name);
Why do you have an & before name?
fflush(stdout);
char input[40];
scanf("%s",&input);
scanf() stops reading a string at the first whitespace. It's also
Slight correction/clarification : scanf() reads upto the first white
space in THIS instance because of the format qualifier passed in which
is "%s".
unsafe to use in this situation because its input isn't bounded.
printf("%s",&input);}
Now if I enter something like niceday it is printed but nice day then
only nice is printed. The space is the question.
Your format string is not upto what you what. Google up some scanf
examples. (The safety part isn't really relevant to your question).
See above. Use the standard fgets() or one of the fine third-party
"getline" type functions posted here occasionally.
Try and stick with the standard functions at this stage IMO. You learn
more.
Brian.
- References:
- C code
- From: Bill Cunningham
- Re: C code
- From: Default User
- C code
- Prev by Date: Keith Thompson's messages dont come through
- Next by Date: Re: Keith Thompson's messages dont come through
- Previous by thread: Re: C code
- Next by thread: Re: C code
- Index(es):
Relevant Pages
|