about string and character

Discussion in 'C Programming' started by dattts@gmail.com, Nov 24, 2007.

  1. Guest

    what is the difference between a single character and a string
    consisting only one character
    , Nov 24, 2007
    #1
    1. Advertising

  2. santosh Guest

    In article
    <>,
    <> wrote on Saturday 24 Nov 2007 2:57
    pm:

    > what is the difference between a single character and a string
    > consisting only one character


    Try this program:

    #include <stdio.h>

    int main(void) {
    char c0 = '0';
    char c1[] = "0";

    printf("Size of c0 is %u\nSize of c1 is %u\n", sizeof c0, sizeof c1);
    return 0;
    }

    What is the output and why are the sizes for 'c0' and 'c1' different?
    What does your C textbook say?

    And try this one too:

    #include <stdio.h>

    int main(void) {
    printf("Size of 'a' is %u\nSize of \"a\" is %u\n",
    sizeof 'a', sizeof "a");
    return 0;
    }

    What is the output and why are they different for 'a' and "a"?
    What does you textbook say about this?
    santosh, Nov 24, 2007
    #2
    1. Advertising

  3. santosh Guest

    In article <fi8ska$rev$>, santosh
    <> wrote on Saturday 24 Nov 2007 3:28 pm:

    > In article
    > <>,
    > <> wrote on Saturday 24 Nov 2007 2:57
    > pm:
    >
    >> what is the difference between a single character and a string
    >> consisting only one character

    >
    > Try this program:
    >
    > #include <stdio.h>
    >
    > int main(void) {
    > char c0 = '0';
    > char c1[] = "0";
    >
    > printf("Size of c0 is %u\nSize of c1 is %u\n", sizeof c0, sizeof
    > c1); return 0;
    > }
    >
    > What is the output and why are the sizes for 'c0' and 'c1' different?
    > What does your C textbook say?
    >
    > And try this one too:
    >
    > #include <stdio.h>
    >
    > int main(void) {
    > printf("Size of 'a' is %u\nSize of \"a\" is %u\n",
    > sizeof 'a', sizeof "a");
    > return 0;
    > }
    >
    > What is the output and why are they different for 'a' and "a"?
    > What does you textbook say about this?


    Note that this second example could confuse you if you happen to compile
    on systems where the type int is two bytes.

    It's still worthwhile to consult your prescribed textbook and do a bit
    of thinking on your own rather than ask for instant answers in
    newsgroups.
    santosh, Nov 24, 2007
    #3
  4. Chris Dollin Guest

    wrote:

    > what is the difference between a single character and a string
    > consisting only one character


    One's a single character, and the other is a sequence containing
    a single character, implemented as a pointer to a sequence of
    characters the second of which is a terminating nul character and
    the first is the character in the string.

    It's like the difference between an apple and a paper bag containing
    an apple: the bag is not an apple, even though you can get an apple
    out of it, or replace the apple with an orange. (Since the string
    bag can contain only one fruit character, you can't mix apples and
    oranges [1].)

    [1] In this bag. In bigger bags you can.

    --
    Perhaps Caffeine Now? Hedgehog
    "A facility for quotation covers the absence of original thought." /Gaudy Night/
    Chris Dollin, Nov 24, 2007
    #4
  5. Guest

    On Nov 24, 5:58 pm, santosh <> wrote:
    > In article
    > <>,
    > <> wrote on Saturday 24 Nov 2007 2:57
    > pm:
    >
    > > what is the difference between a single character and a string
    > > consisting only one character

    >
    > Try this program:
    >
    > #include <stdio.h>
    >
    > int main(void) {
    > char c0 = '0';
    > char c1[] = "0";


    This string "0" consists of two characters. The only string that
    consists of a single char is "".
    , Nov 24, 2007
    #5
  6. In article <>,
    <> wrote:
    >On Nov 24, 5:58 pm, santosh <> wrote:
    >> In article
    >> <>,
    >> <> wrote on Saturday 24 Nov 2007 2:57
    >> pm:
    >>
    >> > what is the difference between a single character and a string
    >> > consisting only one character

    >>
    >> Try this program:
    >>
    >> #include <stdio.h>
    >>
    >> int main(void) {
    >> char c0 = '0';
    >> char c1[] = "0";

    >
    >This string "0" consists of two characters. The only string that
    >consists of a single char is "".


    strlen() and common sense say otherwise. You are confusing how big
    something is with how much space it takes to store it. They are rarely
    the same thing, and the later is usually greater than the former.
    Kenny McCormack, Nov 24, 2007
    #6
  7. CBFalconer Guest

    "" wrote:
    >
    > what is the difference between a single character and a string
    > consisting only one character


    The character contains exactly one char. The string contains two
    chars, the char followed by '\0'. It the char is 'c', then you can
    define the two cases with:

    char c = 'c'; /* single char */
    char *s = "c"; /* a string, non modifiable */
    char m[] = "c"; /* a string, modifiable */

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 24, 2007
    #7
  8. Chris Dollin Guest

    wrote:

    > This string "0" consists of two characters.


    The nul terminator isn't one of the characters "in" a
    string.

    --
    Misplaced Hedgehog
    Otherface: Jena RDF/Owl toolkit http://jena.sourceforge.net/
    Chris Dollin, Nov 24, 2007
    #8
  9. Joe Wright Guest

    wrote:
    > what is the difference between a single character and a string
    > consisting only one character


    You are posting too early in your knowledge quest. Read your book.

    char ch = 'A';
    char *ar = "A";

    ch is the name of an object of type char which holds the value 'A'.

    ar is the name of an object of type char* which holds the address of an
    array of two char objects, 'A' and '\0'.

    These two are exquisitely different things. Both are explained in your
    book. Please read your C book.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Nov 24, 2007
    #9
  10. On Sat, 24 Nov 2007 17:31:43 +0000, Chris Dollin wrote:
    > wrote:
    >
    >> This string "0" consists of two characters.

    >
    > The nul terminator isn't one of the characters "in" a string.


    From a common sense perspective:
    char a[] = "0";
    and
    char a[] = { '0', '\0' };
    do exactly the same thing, and common sense says that '\0' is "in" a in
    the second case, so it must also be "in" a in the first.

    From a standards perspective:
    A string is defined as "a contiguous sequence of characters terminated by
    and including the first null character".
    Harald van Dij殺俎k, Nov 24, 2007
    #10
  11. CBFalconer Guest

    Chris Dollin wrote:
    > wrote:
    >
    >> This string "0" consists of two characters.

    >
    > The nul terminator isn't one of the X-Mozilla-Status: 0009ng.


    But it IS one of the characters in the char array holding the
    string.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 24, 2007
    #11
  12. On Sat, 24 Nov 2007 01:27:26 -0800 (PST), ""
    <> wrote:

    >what is the difference between a single character and a string
    >consisting only one character


    Since a string must be terminated with a '\0', the only possible
    1-character string is "". If instead you meant a string whose length
    is 1, then such a string obviously contains two characters.

    A single character can contain any one of at least 256 possible
    values.


    Remove del for email
    Barry Schwarz, Nov 25, 2007
    #12
  13. Chris Dollin Guest

    CBFalconer wrote:

    > Chris Dollin wrote:
    >> wrote:
    >>
    >>> This string "0" consists of two characters.

    >>
    >> The nul terminator isn't one of the X-Mozilla-Status: 0009ng.


    Urm -- what happened there?

    > But it IS one of the characters in the char array holding the
    > string.


    Oh, yes.
    Elsethread $)CHarald van D )& k <> (and
    what happened /there/? Is my newsgarbler doing something
    odd to names & content?) remarks

    | From a standards perspective:
    | A string is defined as "a contiguous sequence of characters
    | terminated by and including the first null character".

    which would make my position unsupported by the Standard's
    terminology.

    --
    One-Hand Hedgehog
    Notmuchhere: http://www.electrichedgehog.net/
    Chris Dollin, Nov 25, 2007
    #13
  14. santosh Guest

    In article <WMa2j.152625$>, Chris
    Dollin <> wrote on Sunday 25 Nov 2007 2:26 pm:

    > CBFalconer wrote:
    >
    >> Chris Dollin wrote:
    >>> wrote:
    >>>
    >>>> This string "0" consists of two characters.
    >>>
    >>> The nul terminator isn't one of the X-Mozilla-Status: 0009ng.

    >
    > Urm -- what happened there?
    >
    >> But it IS one of the characters in the char array holding the
    >> string.

    >
    > Oh, yes.
    > Elsethread $)CHarald van D )& k <> (and
    > what happened /there/? Is my newsgarbler doing something
    > odd to names & content?) remarks


    No. His name appears mangled on my newsserver/newsreader too. I suspect
    that he is using an encoding not supported by some software in the
    Usenet hierarchy.

    It was fine when Harald started posting to this group, but for a few
    months now, his name has not been displayed properly.

    > | From a standards perspective:
    > | A string is defined as "a contiguous sequence of characters
    > | terminated by and including the first null character".
    >
    > which would make my position unsupported by the Standard's
    > terminology.


    Yes. So my first post contains errors. Hopefully the OP would have read
    far enough into the thread to note the corrections.
    santosh, Nov 25, 2007
    #14
  15. santosh Guest

    Re: [OT] Re: about string and character

    In article <fibrpm$mco$1.ov.home.nl>, Harald van D?k
    <> wrote on Sunday 25 Nov 2007 6:32 pm:

    > On Sun, 25 Nov 2007 12:32:13 +0000, Harald van D?k wrote:
    >> On Sun, 25 Nov 2007 14:47:18 +0530, santosh wrote:
    >>> It was fine when Harald started posting to this group, but for a few
    >>> months now, his name has not been displayed properly.

    >>
    >> Apparently KNode encoded my name in utf-8, but Pan encodes it in
    >> iso-2022-kr. I'll check if there's an option to change that.

    >
    > Does it display better like this?


    Yes. It is now displayed correctly.
    santosh, Nov 25, 2007
    #15
  16. James Kuyper Guest

    Re: [OT] Re: about string and character

    Harald van Dk wrote:
    > On Sun, 25 Nov 2007 12:32:13 +0000, Harald van Dk wrote:
    >> On Sun, 25 Nov 2007 14:47:18 +0530, santosh wrote:
    >>> It was fine when Harald started posting to this group, but for a few
    >>> months now, his name has not been displayed properly.

    >> Apparently KNode encoded my name in utf-8, but Pan encodes it in
    >> iso-2022-kr. I'll check if there's an option to change that.

    >
    > Does it display better like this?


    Yes.
    James Kuyper, Nov 25, 2007
    #16
  17. On Sun, 25 Nov 2007 14:47:18 +0530, santosh wrote:
    > In article <WMa2j.152625$>, Chris
    > Dollin <> wrote on Sunday 25 Nov 2007 2:26 pm:
    >> Oh, yes.
    >> Elsethread $)CHarald van D )& k <> (and what
    >> happened /there/? Is my newsgarbler doing something odd to names &
    >> content?) remarks

    >
    > No. His name appears mangled on my newsserver/newsreader too. I suspect
    > that he is using an encoding not supported by some software in the
    > Usenet hierarchy.
    >
    > It was fine when Harald started posting to this group, but for a few
    > months now, his name has not been displayed properly.


    Apparently KNode encoded my name in utf-8, but Pan encodes it in iso-2022-
    kr. I'll check if there's an option to change that.
    Harald van Dij殺俎k, Nov 25, 2007
    #17
  18. Re: [OT] Re: about string and character

    Harald van D?k said:

    > On Sun, 25 Nov 2007 12:32:13 +0000, Harald van D?k wrote:
    >> On Sun, 25 Nov 2007 14:47:18 +0530, santosh wrote:
    >>> It was fine when Harald started posting to this group, but for a few
    >>> months now, his name has not been displayed properly.

    >>
    >> Apparently KNode encoded my name in utf-8, but Pan encodes it in
    >> iso-2022-kr. I'll check if there's an option to change that.

    >
    > Does it display better like this?


    Yes. But you're still going to have problems with the i-j ligature (or
    diphthong, or whatever the proper name is).

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 25, 2007
    #18
  19. [OT] Re: about string and character

    On Sun, 25 Nov 2007 12:32:13 +0000, Harald van Dk wrote:
    > On Sun, 25 Nov 2007 14:47:18 +0530, santosh wrote:
    >> It was fine when Harald started posting to this group, but for a few
    >> months now, his name has not been displayed properly.

    >
    > Apparently KNode encoded my name in utf-8, but Pan encodes it in
    > iso-2022-kr. I'll check if there's an option to change that.


    Does it display better like this?
    Harald van Dk, Nov 25, 2007
    #19
  20. santosh Guest

    Re: [OT] Re: about string and character

    In article <fibo1c$sk3$>, santosh
    <> wrote on Sunday 25 Nov 2007 5:28 pm:

    > In article <fibrpm$mco$1.ov.home.nl>, Harald van D?k
    > <> wrote on Sunday 25 Nov 2007 6:32 pm:
    >
    >> On Sun, 25 Nov 2007 12:32:13 +0000, Harald van D?k wrote:
    >>> On Sun, 25 Nov 2007 14:47:18 +0530, santosh wrote:
    >>>> It was fine when Harald started posting to this group, but for a
    >>>> few months now, his name has not been displayed properly.
    >>>
    >>> Apparently KNode encoded my name in utf-8, but Pan encodes it in
    >>> iso-2022-kr. I'll check if there's an option to change that.

    >>
    >> Does it display better like this?

    >
    > Yes. It is now displayed correctly.


    Oops. It was correct when I received and replied to it, but not when I
    read it back, as you can see.
    santosh, Nov 25, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Velvet
    Replies:
    9
    Views:
    14,813
    Joerg Jooss
    Jan 19, 2006
  2. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    734
    those who know me have no need of my name
    May 28, 2006
  3. herman
    Replies:
    5
    Views:
    7,599
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 30, 2007
  4. PerlFAQ Server
    Replies:
    0
    Views:
    391
    PerlFAQ Server
    Jan 25, 2011
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    729
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page