Re: C code



"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
.



Relevant Pages

  • Re: Ambiguous/debatable errata
    ... white space character in the format string, ... eventually produces a '\n' on the input stream, scanf() ...
    (comp.lang.c)
  • Re: reading in multiple strings with scanf
    ... scanf, and fscanf with always the same arguments ... from my struct and I use it in the same order they appear in the ... function calls where the format string is always a bit differant but the ...
    (comp.lang.c)
  • Re: Discarding unread data after scanf()
    ... What scanf leaves on the input stream depends on the format string. ... getchar() to EOF, not by calling feofor ferror), and you can ...
    (comp.lang.c)
  • Re: Ex 7-5
    ... "scanf ignores blanks and tabs in it's format string. ... The format string is NOT the input that scanf is scanning. ...
    (comp.lang.c)
  • Re: converting variables
    ... >>%s stops at the first whitespace). ... > There's more to scanf than %s... ... Shall we also discard printf for ... > floating point values because %d expects an int value? ...
    (comp.lang.c)