Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
The code is like 5 lines, you get what I'm going for here. I'm pretty sure I did the scanf correctly, I don't know why the if won't work, I tried " and ' and & but its not working


#include <stdio.h>
#include <stdlib.h>

int main() {
 char name[20];
 printf("Enter your name: \n");
 scanf(" %s", name);
 if (name == 'Eric') {
    printf("Your name is Eric");
    return 0;
In C, you need to explicitly invoke a comparison function to get the logical comparison result for any type other than a primitive number type (ex. int, float). In the case of strings, the function you want is strcmp, defined in header <string.h>.
And why you need strcmp: the type of name in the comparison is char*, and "Eric" is also of type char*. You're checking whether the pointers point to the same thing, which will always be false in this case.
Yes as mentioned strcmp is the function that you are after:

int strcmp(const char *str1, const char *str2)

strcmp returns 0 when the strings are equal, a negative integer when str1 is less than str2, or a positive integer if str1 is greater than str2, according to the lexicographical order.

So it would be:

if(strcmp(name, "Eric") == 0) {
printf("Your name is Eric");
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum