Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Very very very basic question

Reply
Thread Tools

Very very very basic question

 
 
Peter
Guest
Posts: n/a
 
      02-08-2005
#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.


 
Reply With Quote
 
 
 
 
Peter
Guest
Posts: n/a
 
      02-08-2005
"those who know me have no need of my name" <(E-Mail Removed)>
wrote in message news:(E-Mail Removed)...
> 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.


Thanks.

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


 
Reply With Quote
 
 
 
 
those who know me have no need of my name
Guest
Posts: n/a
 
      02-08-2005
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.

--
a signature
 
Reply With Quote
 
John Valko
Guest
Posts: n/a
 
      02-08-2005
Peter wrote:

> 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
 
Reply With Quote
 
Dmitry
Guest
Posts: n/a
 
      02-08-2005
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

Peter wrote:
> "those who know me have no need of my name" <(E-Mail Removed)>
> wrote in message news:(E-Mail Removed)...
>
>>in comp.lang.c i read:
>>
>>
>>>scanf("%s", input_string);

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


 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      02-08-2005
Peter <(E-Mail Removed)> wrote:
> #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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      02-08-2005
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.

> Peter wrote:
>
>> "those who know me have no need of my name"
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>
>>> in comp.lang.c i read:
>>>
>>>
>>>> scanf("%s", input_string);

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


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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-09-2005
Flash Gordon wrote:
>> Peter wrote:
>>

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

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

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-09-2005
Flash Gordon <(E-Mail Removed)> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-09-2005
(E-Mail Removed)-berlin.de writes:
[...]
>> 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


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

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I found some very odd behaviour in Python's very basic types Sunjay Varma Python 4 03-10-2011 05:05 PM
A very **very** basic question mdh C Programming 57 09-26-2008 03:25 PM
very very basic question aghazalp Python 6 04-02-2006 09:35 PM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments