1

I'm totally new to programming.

I just want to type in Dave, then it should show "have a nice day"

So I write it in this way:

 {
  char str[100];
  printf("\nwhat is your name?\n");
  scanf("%s",str);
  if (str="Dave")
     {
        printf("\nhave a nice day\n");
      }
  }

Eventually, it shows nothing. What's wrong with it?

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 3
    First you assign and you don't compare (= instead of ==), second you can't compare a string with == operator (for pointers you compare an address, not the content) – Adriano Repetti Mar 07 '13 at 08:26
  • 1
    @ShashankKadne no, he doesn't – SomeWittyUsername Mar 07 '13 at 08:26
  • @Dugarry I would strongly advise you to learn how arrays an pointers work, before attempting string handling, since string handling in C relies heavily on arrays and pointers. (One of many reasons why C may not be ideal for beginner programmers. Personally I would recommend Java as first programming language) – Lundin Mar 07 '13 at 08:29
  • @Lundin not sure about that. Maybe it's better to have a bit of a hard time at first and learn C - otherwise he may give up and forget about C. By the way Java, classes, packages, maven, Spring and co. are not that easy to learn either... – Déjà vu Mar 07 '13 at 08:32
  • How come you guys can answer it so quickly lol – Dugarry Chan Mar 07 '13 at 08:38
  • 1
    `I'm totally new to programming. I just want to...` Save us and yourself some grief, get a good book on C and learn the language from it. If a C program compiles, that by itself does not guarantee it'll work. If you do something without understanding what you're doing, you pay for it. Ever heard of C letting you shoot yourself in the foot? That's a nice way of putting it. Surely, `=` is present in mathematics and the concepts of comparison and assignment exist in most programming languages, but they are no mathematics and you need to relearn how to do certain things in them and in their way. – Alexey Frunze Mar 07 '13 at 08:40
  • @AlexeyFrunze , do you have any suggestion that which book i should start study for or any other web resource ? – Dugarry Chan Mar 11 '13 at 12:36
  • [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Alexey Frunze Mar 11 '13 at 13:08

3 Answers3

7

Your code is assigning str to point to the string literal "Dave". I guess you meant to compare (using ==) instead? This would also have failed, comparing the addresses of two char arrays. In C, you should use strcmp to compare strings

if (strcmp(str, "Dave") == 0)
simonc
  • 41,632
  • 12
  • 85
  • 103
2

string comparison requires a library function. Replace

if (str="Dave")

with

if(strcmp(str, "Dave") == 0)

and in the beginning, include

#include<string.h>
uba
  • 2,012
  • 18
  • 21
1

String comparison in c could be done by strcmp() from #include <string.h>

if (strcmp(str,"Dave")==0)
     {
        printf("\nhave a nice day\n");
      }
MOHAMED
  • 41,599
  • 58
  • 163
  • 268