Re: i need help about Turbo C



sarathy wrote:
use
scanf ("%[^\n]",name);
scanf ("%[^\n]",address);

Now your name and address can contain any number of spaces..

%[^\n] is good advice, but you should also include a number to limit the number of characters it will attempt to read and put in the array.

Otherwise if the user inputs a line that is too long, it will overflow the array and cause undefined behaviour - something bad might happen.

For example:

char name[128];
scanf("%127[^\n]", name);

Note how the number is one less than the size of the array, to allow for the null character to be stored.

--
Simon.
.



Relevant Pages

  • Re: How to best parse a CSV data file and do a lookup in C?
    ... > and build an array for each line seperating on the commas. ... that you either change its type to "static char" (otherwise the ... making fscanfread a maximum number of 128 characters as long ... that must be a comma or a newline. ...
    (comp.lang.c)
  • Re: Sorry, newbie question about generating a random string
    ... string grows to a max of 10 characters. ... The real problem is that you are not terminating the string. ... string is an array of characters ending in a null character, ... char myChar; ...
    (comp.lang.c.moderated)
  • Re: regex
    ... I suppose pedantry has its place. ... I was simply talking about looping through the characters in the ... array of char. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to zero-initialize a C string (array of wchar_t)?
    ... you need to fill the array with '\0' because if the array is of dimension ... because the 5-6-7-8 characters were not set and are still using unset ... is almost impossible to overflow... ... if you allocated 10 char and the user input ...
    (comp.lang.cpp)
  • Re: difference for array in c and c++?
    ... functions take an 8-byte char array to indicate which module you ... remainder of the array with null characters is wasteful. ... The strncpy function copies not more than n characters ...
    (comp.lang.c)