ok...i'm new to this c language and i was wondering what am i supposed to do if the user enters a string that is longer than my array size. for our assignment, we're supposed to ask for the user's name and our array size is supposed to be 20 characters. if the user enter something more than 20, we're supposed to give an error message saying that only the first 20 characters will be stored. for my code, if the user enters more than 20 characters, the additional character would be assigned to another variable. what am i supposed to do.
i'm using fscanf to accept the string but i read that it's better to use gets.

fgets() is always better than scanf() because fgets() will not accept more characters than will fit in the buffer. But neither scanf() nor fgets() will meet the requirements of your assignment. You need a function that will get only one key at a time from the keyboard so that your program can do the validations. getchar() will do that.

#include <stdio.h>
#include <malloc.h>
void main()
{
   char name[20];   
   int i=0;

  
   printf("please enter the name ");
   for (name[i]=getchar();name[i]!='\n';name[i]=getchar()) 
   {                                               
    i+=1;
   }
   if (i>=20)
   {
       printf ("You entered more than 20 characters");
   }
   printf("your name is %s and has %d characters",name,i); 
   free( name );                 
}

this is what i came up with...but with what i have, the variable will still store what ever the user entered. how am i supposed to do it so that only 20 characters are stored

you're trying to put too much at the top of that loop. Break it down, pub the getchar() inside the loop and on each loop iteration check if the value of i exceeds 19 (need one byte for the string's null terminator, or make the size of the buffer 21.

BTW: that doesn't look like it is your code -- not something a beginner would write.

i found it somewhere and altered it...but this is not what i plan to put in my assignment...i'm trying to understand how the function works first. sorry for sounding ignorant but what do you mean by pub the getchar()

#include <malloc.h>
free ( name );

Not need it here.

void main() should be int main(void)

i found it somewhere and altered it...but this is not what i plan to put in my assignment...i'm trying to understand how the function works first. sorry for sounding ignorant but what do you mean by pub the getchar()

Sorry about that -- it was a typo which should have read "put " not "pub ". Example:

for( i = 0; i < sizeof(name)-1; ++i)
{
    name[i] = getchar();
}

fgets() is always better than scanf() because fgets() will not accept more characters than will fit in the buffer. But neither scanf() nor fgets() will meet the requirements of your assignment. You need a function that will get only one key at a time from the keyboard so that your program can do the validations. getchar() will do that.

I disagree. You can use fgets() to read into a buffer that is much larger than you need, then if the number of characters is over the max, output an error. If not, move the input to your name array.

thanks...i've decided to use fgets and finally got it to work but i'm still having problems detecting if the value entered is more than 20 characters...what am i supposed to do? For now the code i have is something like

for (index2=0;index2<=19;index2++)
                {
                    if (name[index2] == '\0')
                    {
                        name_valid=1;
                        break;
                    }
                    else
                    {
                        name_valid=0;
                    }
                }

but i think if the user enter exactly 20 letters the error message would also be displayed

the baseline behavior of fgets() is that it will read in the newline char in addition to the alphanumeric char and other whitespace char if there's enough room in the array. So I here's what I would try. Declare the target string to include two more than the max number of alphanumeric char allowed. Once the string has been read in then check the length with strlen(). If it's less than the max length desired then you know that nothing extra was entered. If strlen is max + 1 and the second to last element in the string is newline then you know that the number of char entered was exactly the limit. If the second to last char isn't newline, then too many char were entered by the user and you'll need to remove the second to last char to keep it at the limit. Remember, since fgets() won't remove the newline char you'll need to discard it if it's there.

This article has been dead for over six months. Start a new discussion instead.