Find   


An Affiliate of AIHorizon




This tip submitted by Romnico on 2005-07-05 21:56:42. It has been viewed 35169 times.
Rating of 3.5180 with 139 votes

Dont't use scanf() when reading sentences



If we use this code:

 
printf("ENTER SENTENCE: "); scanf("%s",string); 
printf("string is =%s",string); 


And if we run it and type a sentence:

ENTER SENTENCE: This sentence.

The output would be: "string is =This"

A second reason to avoid using scanf with strings is that it allows someone to enter too much text and thereby overflow the buffer. The best choice for reading in strings is to use fgets as described in this explanation the problems with gets.




More tips


Add a tip!



-----
Interested in advertising with us?
Please read our privacy policy.
Copyright © 1997-2005 Cprogramming.com. All rights reserved.