-1

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
  • `char name[1]` fits only a single character, not a full null-terminated string. – Some programmer dude Jul 17 '21 at 17:09
  • 3
    Also never ***ever*** use `gets`. [It's so dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) it have even been removed from the C language. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Jul 17 '21 at 17:10

4 Answers4

2

You’ve declared the name array to have a single element; it’s not large enough to hold the contents of the string "smti prajapati". You’d need to declare name to be at least 15 elements wide - 14 characters in the string plus the zero terminator.

You can omit the size in the declaration and write

char name[] = "smti prajapati";

and the array size will be taken from the size of the initializer.

Note that the size of name is fixed after it is defined, and will not able to store strings longer than the initializer.

Do not use gets; if you enter a string that’s too long for the array, it will write those extra characters to whatever follows the array. This is known as a buffer overflow, and can lead to anything from corrupted data, a runtime error, or branching to a random location in your program. Buffer overflows are a common malware exploit. Use fgets instead, as it allows you to limit how many characters get written to the target.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

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);
anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

When you use arrays in C you are using a fixed memory space. If I want to save "Hello World!" in memory, I have to reserve an array of length 13. In C it looks as:

char aString[13] = "Hello World!"

It may go as high as 20, but no lower than 13. Why? Strings have characters that we care and the last one is the null character (its value is 0). So, you need to reserve thirteen characters.

In your code you are reserving only 1 byte (1 character). At least, you have to reserve 15 bytes.

Try this code:

#include<stdio.h>

int main()
{
    char name[15]="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;
}

I compiled the code without problems. Also, you are able to not specify the length and It is going to work right. The best solution is dynamic memory. Avoid gets(). It has security issues. Instead, use fgets().

  • `"Hello World!"` properly needs an array of size 13, not 12, if it is to be handled as a string. And `puts(name);` is *undefined behaviour* because there is no NUL terminator, and `gets()` is obsolete as any decent compiler will tell you. – Weather Vane Jul 17 '21 at 17:45
0

You have a number of misunderstandings. Let's look at them in order.

in the c language we know, the string is known as a null-terminated character** array

In C, a string is a null-terminated array of char. It is very common to refer to a string using a character pointer, char *. It may have been a typo when you said "character**", but type char ** is a pointer-to-pointer, which is something else.

when we declare char name[5]; it means the string store at least 4 characters value and null value.

Correct.

char name[1]="smti prajapati";

This is incorrect. Your compiler correctly warned you: warning: initializer-string for array of chars is too long

when we have entered a name that contains more than 5 characters it is accepted and prints all characters.

Right. Sometimes, when you break the rules, you get away with it.

In this case, the compiler does not throw the warning.

Right. C does not always detect or complain about buffer overflow. In general, it is your responsibility to make sure your arrays are allocated large enough for the strings you try to store in them.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103