Thread: why doesn't this work?

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    18

    why doesn't this work?

    hi i don't know why but this code produces no output. I'm a beginner i don't get. why doesn't this work?

    Code:
    #include <stdio.h>
    
    int main()
    {
        char c[] = "0110";
        if (c == "0110")
            printf("%s", c);
    
        return 0;
    }

  2. #2
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by Mohsen Abasi View Post
    hi i don't know why but this code produces no output. I'm a beginner i don't get. why doesn't this work?

    Code:
    #include <stdio.h>
    
    int main()
    {
        char c[] = "0110";
        if (c == "0110")
            printf("%s", c);
    
        return 0;
    }
    Char c [] is an array of characters, initialize it's size: char c [3].
    Last edited by Terrance; 04-17-2017 at 04:21 AM.

  3. #3
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Or for the exact answer to your question, putting a string into char c [] puts it in read only format, so you can't output a value that is read only, it also has to be able to write the value (so initialize the size of the char array), see: string - What is the difference between char s[] and char *s in C? - Stack Overflow
    Last edited by Terrance; 04-17-2017 at 04:23 AM.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Use strcmp for comparing strings.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,688
    Quote Originally Posted by Satya View Post
    Use strcmp for comparing strings.
    This is the correct issue; ignore the first two posts by Terrance.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  2. Why doesn't it work?
    By fenikkusu in forum C Programming
    Replies: 4
    Last Post: 01-10-2009, 02:56 AM
  3. \b doesn't work when followed by \n
    By rllovera in forum C Programming
    Replies: 6
    Last Post: 03-11-2006, 12:28 PM
  4. why doesn't work
    By maro009 in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 04:33 AM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

Tags for this Thread