471,863 Members | 979 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,863 software developers and data experts.

char comparison error

196 100+
Hi, im just trying to compare a character(char) to a set list of characters (e.g. "a", "b", etc).

first i read in the value- (i have tried this two ways)
Expand|Select|Wrap|Line Numbers
  1. scanf("%c", &var_name);
  2. //also tried this and yes i know the difference
  3. scanf("%s", &var_name);
When i printf this value -
Expand|Select|Wrap|Line Numbers
  1. printf("%c this is var_name", var_name);
It outputs what it is supposed to,

Now for the actual error (of which there are a couple)

When i compare var_name to another character
Expand|Select|Wrap|Line Numbers
  1. char var_name;
  2. if (var_name != "z") {
  3. // code goes here
  4. }
I get the following error cannot compare a const char to a char.

Then i read somewhere about adding an * to fix this problem (i thought this was a pointer by the way) -

Expand|Select|Wrap|Line Numbers
  1. char *var_name;
  2. if (var_name != "z") {
  3. // code goes here
  4. }
And believe it or not it actually worked???, anyhow now its stopped working completely (e.g even though var_name = "z" it wont pick it up)

So my question to anyone who will help how do i compare a char to a character and have it work.

Any help in this matter is greatly appreciated, you have my thanks.
Apr 6 '08 #1
2 1548
671 Expert 512MB
On the code that involves replacing %c with %s (at the beginning):
Interesting. I thought you would have removed the ampersand from var_name, because you are passing in an array, right?

But on to your actual problem. Just as a note, when something doesn't work, don't guess the code. You'll typically make things worse (as you did here).

First, I'll note the problems in your two code snippets involving character comparisons. In the first snippet, the issue is using "z" instead of 'z'. Yes, the quote type matters. "z" creates a C string. 'z' creates a character.

The error message is a bit cryptic if you are a beginner in C. So, var_name is a char. That's simple. Now look at "z". First, it's a constant. It's a constant as much as if you had typed the number 5 in place of it. Second, a C string can be pointed to with a char*. You have a constant string literal, so you have a const char*. And you try to compare that to a char. Hence the compilation error. You actually wanted the char 'z'. So use the single quotes.


But your second snippet is even worse. So you added a * to var_name, and now you have a character pointer. This gives you an immediate problem. Suddenly, you no longer have a character. So what you have to do is allocate memory for a character, and then point var_name to it. I'm betting you omitted this portion in your code.

Of course, the var_name != "z" comparison will pass. You are comparing two char pointers. It's always going to return false though. "z" occupies a certain place in memory. var_name isn't going to point to it. That is what comparing two pointers does, by the way. It compares if the pointer values are equal. So when you have

Expand|Select|Wrap|Line Numbers
  1. char *cstr1, *cstr2;
  2. ... cstr1 == cstr2 ...
That comparison doesn't check if the string contents match. You need to explicitly go through the string contents and check if each individual character matches up with one another. That's why there is a function in the library strcmp and strncmp to do so for you.

But you never wanted a string comparison anyway. Change "z" to 'z' and you're set to go.
Apr 6 '08 #2
196 100+
Thanks for the help, it is greatly appreciated
Apr 6 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

18 posts views Thread by muser | last post: by
12 posts views Thread by Francis Bell | last post: by
5 posts views Thread by Marcus | last post: by
3 posts views Thread by Vishal Ladha | last post: by
74 posts views Thread by aruna.mysore | last post: by
9 posts views Thread by subramanian100in | last post: by
4 posts views Thread by HED | last post: by
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.