Home > Mobile >  How on earth to use char and if statements?
How on earth to use char and if statements?

Time:11-23

I'm a rookie programmer trying to run a simple code on VS code.

#include<stdio.h>

int main()
{
char* a;

printf("Enter a char");
scanf("%s",&a);

if (a = "yes")
{
    printf("Number is 30");
}
else if (a = "no")
{
    printf("Number is 50");
}
else{
    printf("oops");
}

return 0;
}

I guess looking at the code you guys can figure out what I'm trying to do, if the user enters "yes", a specific sentence need to be displayed and similarly for "no". The problem here is whatever I write in the input, it will always print the first statement, "Number is 30". I've tried running similar codes but ended up with the same output.

If possible, please explain me how to use char,strings,arrays with if-else statements.

CodePudding user response:

There are several misunderstandings in the posted code.

First there is a misunderstanding of char versus string. A char is for instance a single letter, a single special character like ., ;, etc. (see note1) while a string is a serie of chars. So

'y' is a char
"yes" is a string

You print "Enter a char" but from the code it's obvious that you really want "Enter a string".

This leads to the next problem. To input a string using scanf you need to pass a "pointer to char". Your code pass "a pointer to pointer to char" due to the &. Further the passed pointer must point to some memory. So you need:

char a[10];   // Make it an array of char so that it can hold a string

printf("Enter a string, max 9 characters");
scanf("%9s", a);  // No & before a and width specifier used to avoid buffer overflow

Now this part

if (a = "yes")

is not the way to compare two strings in C. For that you need the function strcmp - like:

if (strcmp(a, "yes") == 0)

Putting it together it's like:

int main()
{
    char a[10];
    printf("Enter a string, max 9 characters");
    scanf("%9s", a);
 
    if (strcmp(a, "yes") == 0){
        printf("Number is 30");
    }
    else if (strcmp(a, "no") == 0)
    {
        printf("Number is 50");
    }
    else
    {
        printf("oops");
    }

    return 0;
}

That said, I don't understand why you print stuff like: "Number is 30" but that's kind of irrelevant here.

note1: The type char is actually an integer type, i.e. a number, but the common use is to map these numbers to characters using ASCII encoding.

CodePudding user response:

There are different ways to initialize a variable to access C string.

char *char_ptr = "Hello";

This initializes char_ptr to point to the first character of the read-only string "Look Here".A C string initialized through a character pointer cannot be modified. When a C string is initialized this way, trying to modify any character pointed to by char_ptr is undefined behaviour. An undefined behaviour means that when a compiler encounters anything that triggers undefined behaviour, it is allowed to do anything it seems appropriate.

A more convenient way to define strings that can be modified is to use:

char str[];

This way you can modify any character in the C string

p.s you also need to use strcmp() for the if statement

CodePudding user response:

You can take string input in C using

scanf(“%s”, str);

And to compare the string you need to use:

strcmp(str1, "yes");
  • Related