-3

I ve been coding in C++, completly new in C. Why doesnt it work? I want to end program by typing exit

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
  char command[4];

  do{
          printf( "  -> " ) ;
          scanf("%c", &command);
  }while(&command != "exit");

  return 0;
}
Pablo
  • 13,271
  • 4
  • 39
  • 59
R. Mis
  • 9
  • 2
  • 2
    You really should save C-style strings for later. They're really complex and you should get yourself much more comfortable with the language before you tackle them. – David Schwartz Jan 30 '18 at 19:36
  • first time I have seen it this way round. Normally a c->c++ programmer getting stuck and we say use std::string. Here a c++->c std::string-using programmer is stuck using c strings – pm100 Jan 30 '18 at 19:51
  • Short answer: in C, none of the comparison or relational operators are defined to work with array types (and strings are stored as arrays of `char`). You have to use the `strcmp` or `strncmp` library functions to compare strings. – John Bode Jan 30 '18 at 21:40

3 Answers3

3

Because in C you have to use strcmp for string comparison.

In C a string is a sequence of characters that ends with the '\0'-terminating byte, whose value is 0.

The string "exit" looks like this in memory:

+-----+-----+-----+-----+------+
| 'e' | 'x' | 'i' | 't' | '\0' |
+-----+-----+-----+-----+------+

where 'e' == 101, 'x' == 120, etc.

The values of the characters are determined by the codes of the ASCII Table.

&command != "exit"

is just comparing pointers.

while(strcmp(command, "exit") != 0);

would be correct. strcmp returns 0 when both strings are equal, a non-zero value otherwise. See

man strcmp

#include <string.h>

int strcmp(const char *s1, const char *s2);

DESCRIPTION

The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

But you've made another error:

scanf("%c", &command);

Here you are reading 1 character only, this command is not a string.

scanf("%s", command);

would be correct.

The next error would be

char command[4];

This can hold strings with a maximal length of 3 characters, so "exit" doesn't fit in the buffer.

Make it

char command[1024];

Then you can store a string with max. length of 1023 bytes.

In general, of want to save a string of length n, you need a char array of at least n+1 dimension.

Pablo
  • 13,271
  • 4
  • 39
  • 59
0

You use strcmp, obviously:

while (strcmp(c, "exit"))

What your code does is compare the address of the input buffer with the address of the static string "exit", which of course will never match. You must compare the characters at the pointers.

The orher problem is you have a four byte buffer for a five byte string, the terminator character needs to fit. C is extremely tricky this way, you'll need to allocate a "big enough" buffer for whatever people might type in or the program will immediately crash. Use 1024 or something reasonably big for test programs.

Now I say "obviously" because when writing C code you should have a C standard library reference open at all times to be sure you're using the correct functions and arguments, plus to know what tools you have available.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

Multiple issues with the code

  1. You need five chars not four to include the ending null char \0.
  2. You should use %s for inputting string. %c is for characters
  3. You are comparing memory locations (or pointers) in the while loop. You need strcmp for comparing strings.
balki
  • 26,394
  • 30
  • 105
  • 151