Member Avatar for DaniWebUser_1

I've recently started learning C and I have a simple program that I can't get to work. (I'm pretty sure C++ and C are similar enough at the basic level not to cause any problems in this forum). I've tried it by substituting the scanf("%s", myName) captured string data with an integer instead and it works. I can't understand why it's not working with the string data. So, what it should be doing is simply printing out "Hi, MyName!" in the command line if the user enters "MyName" when prompted. It simply ends silently with nothing printed out otherwise. However, it's always ending with nothing printed out. Here's the code:

#include <stdio.h>

char myName[20];

int main(void)
{

    printf("Hi! Enter your name:");

    scanf("%s", myName);

    if(myName == "MyName")
    {
        printf("Hi, MyName!");
    }

    return 0;

}

I've also tried doing it a little differently by putting the variable in the printf() statement like this:

#include <stdio.h>

char myName[20];

int main(void)
{

    printf("Hi! Enter your name:");

    scanf("%s", myName);

    if(myName == "MyName")
    {
        printf("Hi, %s", myName);
    }

    return 0;

}

This still doesn't work for some reason. The only thing I can imagine might be going on is
a possible missing #include that links the program to library that's necessary for this
program. But, I'm sure I've seen basic examples like this already where you only need
#include <stdio.h>.
I also wouldn't even know what #include to use if this is the case. If someone can figure this out that would be awesome. Thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.