-1
puts("Type your name");
gets(name);
if(name=="exit"){
    exit(0);
}

My logical conditional in if is wrong, how I can repair it?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157

2 Answers2

3

Since name is a string why don't you use strcmp function?

if (strcmp(name, "exit") == 0)
{
  exit(0);
}
Soumik Das
  • 276
  • 2
  • 14
1

You can't compare strings that way, use strcmp, or preferably, strncmp

Kninnug
  • 7,992
  • 1
  • 30
  • 42
  • 1
    Why do you recommend `strncmp`? That'd give a false positive for any string starting "exit". – simonc Jun 02 '13 at 18:04
  • Hmm, that's right, but it is slightly safer. – Kninnug Jun 02 '13 at 18:05
  • 4
    @Kninnug No. None of the strings are being written, so `strncmp()` is not any safer. No risk for buffer overflow for properly NUL-terminated strings at all. –  Jun 02 '13 at 18:07