'Why is my if block not getting executed in my C program?

Why is the if block not executed in the code below?

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("----------------------------------< Welcome >----------------------------------\n");
    printf("-------------------------------------- to -------------------------------------\n");
    printf("-------------------- Temperature Convertor and Vice-Versa ---------------------\n");
    printf("-------------------( Press enter button at prompt to exit )--------------------\n");
    int i = 0;
    
    const char inp[4];
    printf("Do you want to Convert From Fahrenheit to Celsius:\n");
    scanf("%s",inp);
    printf("%s",inp);
    if(inp == "Yes" || inp == "yes" || inp == "y")
    {
        const char *temp1;
        printf("Enter the Temperature in Fahrenheit:\n");
        scanf("%s",temp1);
    }
    return 0;
}

Output of the Code:

----------------------------------< Welcome >----------------------------------
-------------------------------------- to -------------------------------------
-------------------- Temperature Convertor and Vice-Versa ---------------------
-------------------( Press enter button at prompt to exit )--------------------
Do you want to Convert From Fahrenheit to Celsius:

yes
yes
--------------------------------
Process exited after 19.01 seconds with return value 0
Press any key to continue . . .


Solution 1:[1]

Other than the strcmp problem which has been addressed in an other answer there are three problems in this piece of code:

const char* temp1;
printf("Enter the Temperature in Fahrenheit:\n");
scanf("%s",temp1);
  • you don't want const because the value you want to get from the user is not ... constant
  • temp1 is an uninitialized pointer that points nowhere
  • you don't need a string here but you want the user enter a number

So you want actually something like this:

float tempFahrenheit;
printf("Enter the Temperature in Fahrenheit:\n");
scanf("%f", &tempFahrenheit);

float tempCelsius = ...   // I let you write the rest of the code yourself

If you want get a string from the user and convert that to a number you might want something like this:

float tempFahrenheit;
char inputstring[20];   // max length of string is 20
printf("Enter the Temperature in Fahrenheit:\n");
scanf("%s", inputstring);
sscanf(temp, "%f", &tempFahrenheit); 
float tempCelsius = ...   // I let you write the rest of the code yourself

Solution 2:[2]

Yes the if block is skipped, because the condition is never true. In C, you can't just do inp == "Yes". You have to do strcmp and compare the strings.

if (strcmp(inp, "Yes") == 0 || strcmp(inp, "yes") == 0 || strcmp(inp, "y") == 0)

Like so, but in order to use strcmp(), you have to include string.h

#include <string.h>

Apart from that, there is some other problem in your code.

        const char* temp1; // this should be int or float value, not const char.
        printf("Enter the Temperature in Fahrenheit:\n");
        scanf("%s",temp1);

And scanf("%s", temp1); will not work because temp1 is undefined.

Solution 3:[3]

if(inp == "Yes" || inp == "yes" || inp == "y")

The reason why this doesn't work is because of pointers.

The compiler creates a string for "Yes", "yes" and "y" where each has it's own address in memory.

inp is another pointer from your program, so what the if does is simply comparing the pointers, which are obviously never the same. It does NOT compare the content the pointers are pointing to, as the C compiler wouldn't even know what it exactly points to, and how to compare it. So you have to use strcmp().

Solution 4:[4]

This is not correct compare string method. if(inp == "Yes" || inp == "yes" || inp == "y")

You need to use strcmp() and include <string.h>.

This is correct way to compare string:

if(strcmp( inp,"Yes" ) == 0 || strcmp( inp,"yes" ) || strcmp( inp,"y") == 0 )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 thuyein
Solution 3 Devolus
Solution 4 Itati