0
#include<stdio.h>
#include<string.h>

int main()
{
    char strg1[];
    char strg2[] = "football"



    printf("Enter first string: ");
    gets(strg1);

    

    if(strcmp(strg1, strg2)==0)
    {
        printf("\nEqual");
    }

    else
    {
        printf("\nNot Equal");
    }

   
    return 0;
}

I'm getting a string from the user as input and I want to compare it with a ready-made string I have, whether it's equal or not. When I try with strcmp, they are not equal. When I try with strncmp, for example, user footballABC wrote my string football is still equal because with strncmp it is 8 characters. I've limited it. Is there a solution?

elo melo
  • 1
  • 3
  • 5
    Don't use `gets()` ever. – Jonathan Leffler Dec 19 '22 at 06:25
  • 6
    You've not allocated any space for `strg1`. Nothing good is going to happen. (I'm surprised it compiled at all. A modern compiler would surely issue warnings about it.) – Jonathan Leffler Dec 19 '22 at 06:25
  • 2
    [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Retired Ninja Dec 19 '22 at 06:36
  • `char strg1[];` is not valid C. This is your true problem - your compiler is incorrectly configured. [What compiler options are recommended for beginners learning C?](https://software.codidact.com/posts/282565) – Lundin Dec 19 '22 at 07:47
  • And `scanf("%s", strg1);` is equally bad as `gets(strg1);`. No difference at all. – Zakk Dec 19 '22 at 08:39

1 Answers1

2

You've done following mistakes in your program:

  1. Not allocated space for char strg1[];(It will throw error while compiling).

error: definition of variable with array type needs an explicit size or an initializer char strg1[];

  1. Used gets() for reading string which can lead to Buffer Overflow. while using gets() you should have been warned by compiler.

warning: this program uses gets(), which is unsafe.
Full article why gets() in not safe.

Corrected program:

#include<stdio.h>
#include<string.h>

int main()
{
    // char strg1[]; // Error!
    char strg1[100]; // Allocate space to Array.
    char strg2[] = "football";

    printf("Enter first string: ");
    scanf("%s",strg1); // Use Scanf() instead of gets().

    if(strcmp(strg1, strg2)==0)
    {
        printf("\nEqual");
    }

    else
    {
        printf("\nNot Equal");
    }

    return 0;
}
  • 1
    Using `scanf` to read strings without providing a length limit is no better than using `gets`. – Gerhardh Dec 19 '22 at 08:31
  • Besides `scanf("%s"...` being just as bad as `gets`, it's also not doing the same as `gets`. The best alternative to `gets` is `fgets`. But remember that `fgets` differs from `gets` by saving the newline character in the destination buffer. – Support Ukraine Dec 19 '22 at 08:50
  • 1
    And in case you still want to use `scanf` make sure to do it like: `scanf("%99s",strg1);` as the `strg1` has size 100 – Support Ukraine Dec 19 '22 at 08:52
  • @Gerhardh Yes your are correct. For the sake of removing warning you can use `scanf()` – Sandeep Sahani Dec 19 '22 at 09:47
  • Well, we should not aim at silencing warnings but on fixing the problems that are warned about. – Gerhardh Dec 19 '22 at 09:52