Darlyn
Darlyn

Reputation: 4938

scan arithmetic operands c and compare them with string

I am trying to scanf arithmetical operands into variable. I want to put "+" into variable. I tried everything I have found but nothing has worked so far. The best thing I came with is:

char plus = "+";
char* c;
scanf("%c", &c);
if (strcmp(plus, c) == 0) {
    printf("you have + in variable");

But this does not work. It seems like "+" does not get into variable plus nor does it get scanned into variable using scanf. Is there any trick for this?

Upvotes: 1

Views: 698

Answers (1)

Jack
Jack

Reputation: 133609

There are multiple errors in there:

  • you declare a char plus and you initialize it with a char* (and not a char).
  • scanf with %c expects a char* but you are providing a char**
  • you are comparing a char with a char* in strcmp

If you are dealing with single character operators there's no need to do things more complex than they are:

char plus = '+';
char c;
scanf("%c",&c);

if(plus == c)
  printf("you have + in variable");

Upvotes: 4

Related Questions