Abhishek Pandey
Abhishek Pandey

Reputation: 13558

If Else falls with variable in C

I'm a C beginner, I don't understand why this code is not working?

void main(){
    char userInput = "a";

    if(userInput == "a"){
        printf("a");
    } else printf("b");

    getch();
}

returning "b"

but this one is working

void main(){
    char userInput;

    if("a" == "a"){
        printf("a");
    } else printf("b");

    getch();
}

returning "a"

What is the difference?

Upvotes: 1

Views: 144

Answers (6)

Luis Colorado
Luis Colorado

Reputation: 12668

"a" is a string literal, and not a character literal, which should be quoted with single quotes, as in 'a'.

Upvotes: 0

danglingpointer
danglingpointer

Reputation: 4920

Simplified answer: In C anything that goes inside double quotes is string, I.e sequence of chars terminated with null. Single quote is for single character.

Example :

char c = ‘a’ ;

char *str = “string”;

Upvotes: 0

Lundin
Lundin

Reputation: 213842

Ignoring invalid syntax char userInput = "a" (I take it you meant char*), then the first case is obvious and a common FAQ: you aren't comparing the contents of the strings, but their addresses. See How do I properly compare strings?

The same problem happens when you do "a" == "a", you compare the address of the strings. These read-only "..." constants in C are formally called string literals.

There's a common optimization technique used by compilers, known as "string pooling". String pooling means that upon encountering the same string literal several times in the program, the compiler allocates only one of them.

So in your case "a" and "a" happened to be the very same memory address, because only one of the string literals was actually allocated. You can try to print the actual addresses where the string literals are allocated, and see that they are identical:

#include <stdio.h>

int main (void) 
{
  printf("%p %p", "a", "a");  

}

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

A literal represented like "a", is a string literal. The essential type is an array of chars, like char [size]. It is not a compatible type with char, so the assignment

char userInput = "a";

is not valid.

You want to use a character constant, you write something like

char userInput = 'a';

That said, if("a" == "a") seems to work in your case, but it does not do what you think it does. It does not compare the content, rather, it compares the base address (address of the first element) of the strings, and in your case, they seem to be the same. Your compiler, happens to use the same memory location for the identical string literals (optimization) - so you see a truthy result, but this is not guaranteed by the standard. A(ny) compiler is free to choose it's own memory allocation strategy, with or without optimization process.

Using gcc, if the -fno-merge-constants is used as compilation option, this condition is expected to evaluate to falsy.

Upvotes: 5

Guillem Castro
Guillem Castro

Reputation: 281

With char userInput = "a"; you're initializing userInput with a char* and not with the character 'a'. Same with the comparison, you're comparing a char with a char*.

For both the initialization and the comparison you have to use single quotes around the character,

void main(){
    char userInput = 'a';

    if(userInput == 'a'){
        printf("a");
    } else printf("b");

    getch();
}

Upvotes: 2

Peheje
Peheje

Reputation: 14204

Quote chars with ' So

char userInput = 'a';

if(userInput == 'a'){

Upvotes: 0

Related Questions