Error in C code 
Author Message
 Error in C code

Hi, I'm currently learning C and I can't seem to debug this IF statment
code.

#include <stdio.h>
char name[100];
main() {
    printf("Name :> ");
    gets(name);
    if (name != "Hello")
        printf("Good");
    else
        printf("Bad");

Quote:
}

When I type Hello, it prints Good.  It should print Bad when I type Hello.
Why is it doing this?

Thank you in advance,

Tim.



Wed, 01 Oct 2003 22:28:53 GMT  
 Error in C code

Quote:

> Hi, I'm currently learning C and I can't seem to debug this IF statment
> code.

> #include <stdio.h>
> char name[100];
> main() {
>     printf("Name :> ");
>     gets(name);
>     if (name != "Hello")
>         printf("Good");
>     else
>         printf("Bad");
> }

> When I type Hello, it prints Good.  It should print Bad when I type Hello.
> Why is it doing this?

> Thank you in advance,

> Tim.

Both the 'name' and "Hello" in your if statement are treated as
pointers so you are comparing two pointers, which is pointless :)  The
C language does not have any syntax for comparing strings.  That
requires a multistep looping algorithm that is available in the strcmp
library function.

In C++ there are string classes that can treat == and != as calls to a
compare function so this convenient way of coding is available with
many C++ libraries.

--
Scott McPhillips [VC++ MVP]



Wed, 01 Oct 2003 23:53:03 GMT  
 Error in C code
thanks


Quote:

> > Hi, I'm currently learning C and I can't seem to debug this IF statment
> > code.

> > #include <stdio.h>
> > char name[100];
> > main() {
> >     printf("Name :> ");
> >     gets(name);
> >     if (name != "Hello")
> >         printf("Good");
> >     else
> >         printf("Bad");
> > }

> > When I type Hello, it prints Good.  It should print Bad when I type
Hello.
> > Why is it doing this?

> > Thank you in advance,

> > Tim.

> Both the 'name' and "Hello" in your if statement are treated as
> pointers so you are comparing two pointers, which is pointless :)  The
> C language does not have any syntax for comparing strings.  That
> requires a multistep looping algorithm that is available in the strcmp
> library function.

> In C++ there are string classes that can treat == and != as calls to a
> compare function so this convenient way of coding is available with
> many C++ libraries.

> --
> Scott McPhillips [VC++ MVP]



Thu, 02 Oct 2003 11:16:27 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. Include code in other Cs files

3. newbe/cs student, need help w/ code

4. Compile CS source code using ICodeCompiler

5. cs source code

6. Compiler error CS 1595 - Help

7. How can I translate an error code to a string (including WinInet codes)

8. Q: Error texts for Socket error codes ?

9. LoadLibrary error with error code 7e

10. How to show/call Form2.cs from Form1.cs ?

11. Reuse of cs files, namespace, arch advice pls

12. word - automatic numbering/bold/underline/italics

 

 
Powered by phpBB® Forum Software