Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: printf scanf problem

Reply
Thread Tools

Re: printf scanf problem

 
 
code_wrong
Guest
Posts: n/a
 
      08-02-2003

"herrcho" <(E-Mail Removed)> wrote in message
news:bgg40n$emg$(E-Mail Removed)...
> Hi ~ i started to learn C a few weeks ago.
> and i hope i can get much help from this group.
>
> #include <stdio.h>
> main()
> {
> char name[50];
>
> printf("Input a string\n");
> scanf("%s",name);
> printf("%s has %d byte length \n", name,
> strlen(name));
> }
>
> i've got gcc compiler.
> when i run the above code, and enter
> 'I am learning C'
> the output is 'I has 1 byte length' .
> i expected 'I am learning C has 15 byte length'


scanf() stops reading at the space, effectively turning your string into
several strings, one for each word. You have only provided scanf() with a
pointer to one string.
You should look into sscanf() or fgets()


 
Reply With Quote
 
 
 
 
/dev/null
Guest
Posts: n/a
 
      08-02-2003
>> Hi ~ i started to learn C a few weeks ago.
>> and i hope i can get much help from this group.
>>
>> #include <stdio.h>
>> main()
>> {
>> char name[50];
>>
>> printf("Input a string\n");
>> scanf("%s",name);
>> printf("%s has %d byte length \n", name,
>> strlen(name));
>> }
>>
>> i've got gcc compiler.
>> when i run the above code, and enter
>> 'I am learning C'
>> the output is 'I has 1 byte length' .
>> i expected 'I am learning C has 15 byte length'

>
> scanf() stops reading at the space, effectively turning your string into
> several strings, one for each word. You have only provided scanf() with a
> pointer to one string.
> You should look into sscanf() or fgets()
>


It might also help if there was a "#include<string.h>" somewhere in there
too.

 
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
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
problems writing a printf/scanf function that handles both int and char input from the console Dawn Minnis C Programming 6 02-08-2005 02:49 PM
why to use '&' in scanf( ) but not in printf( ) sushant C Programming 4 01-10-2005 04:22 PM
Re: printf scanf problem Dan Pop C Programming 0 08-04-2003 01:54 PM



Advertisments