Doubt regarding "

This is a discussion on Doubt regarding "

Thread: Doubt regarding "\0"

"
within the C Programming forums, part of the General Programming Boards category; For the below code why it is getting a white space when trying to print c1 and nothing is printed ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Doubt regarding "\0"

    For the below code why it is getting a white space when trying to print c1 and nothing is printed for c2?

    Code:
    int main(void)
    {
           char c1='\0';
           char c2[]="\0";
           printf("h%cello\n",c1);
           printf("h%sello\n",c2);
           getch();
           return 0;
    }
    o/p: h ello(white space is there as it is '\0')
    hello (white space is not there why???)

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by karthik537 View Post
    For the below code why it is getting a white space when trying to print c1 and nothing is printed for c2?

    Code:
    int main(void)
    {
           char c1='\0';
           char c2[]="\0";
           printf("h%cello\n",c1);
           printf("h%sello\n",c2);
           getch();
           return 0;
    }
    o/p: h ello(white space is there as it is '\0')
    hello (white space is not there why???)
    Because '\0' is a character like any other character. When you print this character you see an empty space. For a string, the situation is different -- a string that begins with '\0' is a string of zero length, and thus nothing at all is displayed.

    For c1, you are literally seeing what a '\0' "looks like"
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Thanku brewbuck

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    1
    Yeah, the null character's interesting; it's tripped me up in the past when I've encountered 'em in the *nix world.

    That being said, it's not necessary for our latest assignment; if you're going for the homework early, go you! If not, carry on; it's interesting.

    -- KB1JWQ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Doubt abt Storage!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 04-21-2006, 05:30 AM
  4. A doubt on how to make a program
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 08-20-2004, 11:16 AM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 03:41 AM

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