Very very very basic question

P

Peter

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {

char input_string[50];

printf("Please enter conversion: ");
scanf("%s", input_string);

printf("The output is %s\n", input_string);

exit(0);
}


Why does this code ignore any input text after a space?

e.g. If I enter "Hello World" it only stores (and prints) "Hello" in
input_string.

Sorry for the stupid question, it's my first C program. :)
 
P

Peter

those who know me have no need of my name said:
in comp.lang.c i read:



because that's what it's supposed to do:

s Matches a sequence of bytes that are not white-space characters.

Thanks.

What's the function I'm looking for then to take in any length of string
(whether white space or not)?
 
T

those who know me have no need of my name

in comp.lang.c i read:
scanf("%s", input_string);
Why does this code ignore any input text after a space?

because that's what it's supposed to do:

s Matches a sequence of bytes that are not white-space characters.
 
J

John Valko

Peter said:
char input_string[50];

printf("Please enter conversion: ");
scanf("%s", input_string);
Why does this code ignore any input text after a space?

e.g. If I enter "Hello World" it only stores (and prints) "Hello" in
input_string.

Using %s in scanf() will read characters up until it hits any whitespace
character. If you want to read a whole line including spaces, use fgets():

fgets(input_string, sizeof input_string, stdin);

Mind that fgets() will leave the \n on the end, assuming there is one.

--John
 
D

Dmitry

Using GNU libc, 'getline()' is a best choice (IMHO).

Another way is 'scanf' with the next pattern:

scanf("%[^\n\r]", input_string);

i.e "everything before newline or carriage return symbol". Beware of
buffer overflow ;)
 
J

Jens.Toerring

Peter said:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

You don't seem to need the last one here.
int main(void) {
char input_string[50];
printf("Please enter conversion: ");

You need here additionally

fflush( stdout );

to make sure that that string is printed out immediately. Strings
that don't end in a '\n' can stay in the internal output buffers
of the printf() function and only the fflush() makes sure it gets
written to the screen in this case.
scanf("%s", input_string);
printf("The output is %s\n", input_string);
exit(0);

A "return 0;" or "return EXIT_SUCCESS;" will do here perfectly well,
no reason to kill the program;-)
Why does this code ignore any input text after a space?

Because scanf() always stops at spaces when reading in a string
(unless you tell it otherwise), assuming that that's the end of
the input string. The way to tell scanf() not to stop at spaces
is using

scanf("%[^\n]", input_string);

The "%[^\n]" tells scanf() only to stop on a newline character. But
than you still have a potential problem: if the user enters more
than 49 characters scanf() will happily write them past the end of
your 'input_string' array and then in principle everything can
happen (it may even seem to work). So you better make that

scanf("%49[^\n]", input_string);

to tell scanf() to accept not more than the 49 characters fitting
into the buffer (don't forget about the trailing '\0' that's needed
at the end of the string).

Since you seem to want to read a simple line it should be a lot
simpler to use fgets() here instead of scanf() (and never, ever
use gets(), it's horribly broken!). scanf() is rather difficult
to use correctly for reading user input and typically it's a lot
easier to simply read in a whole line with fgets() and then to
take that apart as necessary. That's also true if you e.g. want
an integer as input from the user - if the user types in some-
thing else it's difficult to catch that correctly with scanf()
and way much easier to deal with if you have the whole line and
can analyze it carefully. It's rumored that there are only very
few people who got the hang of using scanf() to savely read in
user input;-)
Regards, Jens
 
F

Flash Gordon

Dmitry wrote:

Please don't top post. Replies belong after or interleaved with the text
you are replying to.
Using GNU libc, 'getline()' is a best choice (IMHO).

Not round here it isn't. Here we deal with standard C, not
implementation specific extensions. After all (or any of), what makes
you think the all the platforms the OP wants to use this on have GNU
libc? Especially since the post was from a Windows machine?
Another way is 'scanf' with the next pattern:

scanf("%[^\n\r]", input_string);

i.e "everything before newline or carriage return symbol". Beware of
buffer overflow ;)

Nor accurate would be don't do that ever. You should *never* read a
string from stdin without limiting the length. Also, fgets is designed
to do what the OP wants, i.e. read a line of text.

Look up fgets in your C reference, and if you don't have one buy a copy
of K&~R2. Also read the comp.lang.c FAQ which you can find easily enough
with Google.
 
C

CBFalconer

Flash said:
Look up fgets in your C reference, and if you don't have one buy
a copy of K&~R2. Also read the comp.lang.c FAQ which you can find
easily enough with Google.

The OP can also download and use the freely available ggets
routine, written in purely standard C, which avoids most of the
nuisances involved with fgets and the insecurities of gets.

<http://cbfalconer.home.att.net/download/ggets.zip>
 
K

Keith Thompson

Flash Gordon said:
Look up fgets in your C reference, and if you don't have one buy a
copy of K&~R2. Also read the comp.lang.c FAQ which you can find easily
enough with Google.

Surely Mr. Ritchie deserves to be complimented, not complemented.
 
K

Keith Thompson

A "return 0;" or "return EXIT_SUCCESS;" will do here perfectly well,
no reason to kill the program;-)

Within main(), "return 0;" and "exit(0);" are very nearly identical.
I tend to prefer return, but there's nothing wrong with using exit().
 
C

Chris Croughton

Surely Mr. Ritchie deserves to be complimented, not complemented.

Ooh, nice wordplay! Although I keep reading K&R2 as K2R2 and thinking
it's a robot...

Chris C
 
D

Dave Thompson

What's the function I'm looking for then to take in any length of string
(whether white space or not)?
Others have already answered what you probably meant to ask, which is
how to input (up to) a line of data possibly containing space, or
perhaps "linear whitespace" or "horizontal whitespace" which includes
tab also. However, the definition of whitespace in C, and also in
(most?) Internet standards, includes newline, and CR and VT and FF. So
your question as stated is actually to read an entire (text) file into
memory -- or an entire TCP (e.g. HTTP) datastream, if on a system
where sockets are interchangeable with files (i.e. Unix) or otherwise
supported by stdio. If that's what you want, the simplest way is just

char buf [BIGENUF];
size_t len;
len = fread (buf, 1, sizeof buf /* or BIGENUF */, fp_eg_stdin);
/* if you want to use the result as a string in C: */
/* do the read for (sizeof buf) -1 (parens not required
but shown for clarity) aka BIGENUF -1 and then */
buf [len] = '\0' /* or just 0 */

If you can't determine in advance (at compile time, or in C99 or GCC
for a local=automatic buffer at declaration which must be before the
read) a buffer size that will be sufficiently large, you must either:

- determine file size, which can't be done fully portably, FAQ 19.12
at the usual places and http://www.eskimo.com/~scs/C-faq/top.html ,
then malloc that much space, checking for failure (return == NULL) and
handling as appropriate, otherwise read the data

- or, malloc a buffer of some "normal" size and try reading that much,
and if that doesn't reach EOF, realloc to a larger size and read more
etc. until either you get it all or realloc fails (out of memory)

In both cases plus a byte for a null terminator if you want a string.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top