Search code examples
arrayscstringgets

I have problem in string, in using the gets function, in c language


I have a problem with string in the c language we know, the string is known as a null-terminated character** array, in the string, we declare how many characters this string is store. and the memory of the character is 1byte. when we declare char name[5]; it means the string store at least 4 characters value and null value.

but there is a problem withstrong text the gets function it. when we have entered a name that contains more than 5 characters it is accepted and prints all characters. In this case, the compiler does not throw the warning.

Please tell me the solution to it....... the code is below.

#include<stdio.h>

int main()
{
    char name[4]="smti prajapati";

    printf("Print the name througth the initilizing : ");
    puts(name);


    printf("Enter the name : ");
    gets(name);
    printf("Print the name througth the user : ");
    puts(name);

    return 0;
}

In the terminal program trow the look like this error

rough.c: In function 'main':
rough.c:5:18: ***warning: initializer-string for array of chars is too long***
     char name[1]="smti prajapati";
                  ^~~~~~~~~~~~~~~~
Print the name througth the initilizing : s3
Enter the name : smit prajapati
Print the name througth the user : smit prajapati

Solution

  • The declaration and assignment:

    char name[1] = "smti prajapati";
    

    Can never work because you assign a string with 14 characters plus the null byte to a char array that only has space for 1 character.

    The reason why you don't get errors or warnings with some ill-formed code can be explained by leeway given by C to the programmer, compilers are not mandated to issue warnings due to some types of ill-formed code like your own, this falls in the category of undefined behavior, and it's up to the programmer to avoid it.

    You could use:

    char name[] = "smti prajapati";
    

    This way the size of the array will be deduced when initialized.

    Or if you want to explicitly use a size:

    char name[15] = "smti prajapati";
    

    Note that I added an extra space for the null byte, it is mandatory if you want to handle name as a propper string. Also note that once the char array is given a size, it will not change, so when you store other strings in name keep that in mind.


    The other problem is gets, you should never use it, it's a very dangerous function, it does not check the bounds of the destination buffer and therefore can easily cause a buffer overflow and that can cause all kinds of trouble. Recent compilers don't even support it anymore as it was deprecated and later removed from the C standard.

    Even those which do still support it often issue warnings similar to:

    warning: the `gets' function is dangerous and should not be used.

    Here are two examples of the output of two modern compilers when you use it.

    Summarizing and concluding, use fgets instead:

    fgets(name, sizeof name, stdin);