If Statement

This is a discussion on If Statement within the C Programming forums, part of the General Programming Boards category; Whats wrong with my code? Code: #include <stdio.h> main() { char x[10]; printf("Enter [True/False] : "); scanf("%s", x); if(x=="true") printf("Great..\n"); ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    If Statement

    Whats wrong with my code?

    Code:
    #include <stdio.h>
    
    main()
    {
    char x[10];
    printf("Enter [True/False] : ");
    scanf("%s", x);
    if(x=="true")
    printf("Great..\n");
    else
    printf("What's wrong?\n");
    }
    result:
    Code:
    root@invecta:~# gcc -o result if.c
    root@invecta:~# ./result
    Enter [True/False] : true
    What's wrong?
    root@invecta:~#

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    C does not know how to do this...
    Code:
    if(x=="true")
    Look into the workings of strcmp()

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    In the context of your "if" statement, the string literal "true" is interpreted as an address where that string exists in memory. Also, "x" is interpreted as the address of the array on the stack. These two addresses are what's effectively being compared in your "if" test. Please see that these two addresses are NEVER going to be equal to each other and so such a test will always take the false branch. Now, if you were doing this in C++ and the variable "x" was a std::string, then your code would work as you think it should since the equality operator (==) is overloaded to compare such objects against a string literal. As mentioned, you need to use the strcmp function (in the string.h header) to compare the two strings and test the returned value from the function call - a 0 is returned if the two strings are equal.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    if (x=="true")
    It's not that simple with strings. Use this instead.

    Code:
    #include <string.h>
    /* ... */
         if (strcmp(x, "true") == 0)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bizarre print statement glitch
    By spongefreddie in forum C Programming
    Replies: 4
    Last Post: 09-23-2010, 10:39 AM
  2. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  3. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  4. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21