Thread: C question

  1. #1
    Unregistered
    Guest

    C question

    i read the notes
    it said character terminating null(\0) character
    is it means if my statment is
    scanf ("%s",$string);<---- here i input apple
    printf ("%s",string);

    then the output will become apple0 ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf ("%s",$string);<---- here i input apple
    printf ("%s",string);

    Your scanf call is wrong, unless you're using it as pesudocode, just as an example. Anyway...

    The 'NULL' terminator, '\0' is not displayed. It is simply used to tell the program when it's reached the end of the string. Think of it as punctuation at the end of a sentence. Punctuation signals you the reader, letting you know that the sentence has ended. You don't actually think of people saying the punctuation:

    "Hello there period" "My name is Hot Stuff Eddie Gilbert period"

    If you've ever seen reference to someone reading a telegram, they actually say the word "stop" whenever a sentence is ended. This is how telegrams denote the end of a sentence. This is similar to how the NULL works.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    When the books say strings in c/c++ are terminated by a null byte all that means is that there is some sort of sentinel value at the end of the string so that the compiler knows where the string will end. This sentinel value is a null byte.... '\0'

    the \ means escape character
    '\0' is normally 0 but then again it might not be so we always use the terminology '\0' to ensure that the correct representation of a null byte is placed after your string.

    in memory your string will look like this....

    'a' 'p' 'p' 'l' 'e' '\0'

    but when its printed the null is not sent to the screen.So on the screen we get apple.

    You need to sort out the syntax of your scanf() because there is no $ to identify strings in c/c++.

    You also need to set aside some memory for your input. An array will be fine for this. for the string above you will need

    char myarray[6]; // thats 5 for the chars and 1 for the null byte.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    oops... beaten to it!lol
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Unregistered
    Guest
    Originally posted by Stoned_Coder
    oops... beaten to it!lol
    thanks~
    scanf is use & , not $

  6. #6
    Unregistered
    Guest
    so,
    scanf is use & , but not $, right??

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    scanf() takes pointers (i.e. addresses). & among other things is the address of operator so normally you would prefix your variable name with & to say you are passing the address of a variable. But an array name acts like a pointer to an array so there is no need to prefix an array name with &.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM