o/p problem

Discussion in 'C Programming' started by fidlee, Dec 23, 2005.

  1. fidlee

    fidlee Guest

    what should be the ouptut of
    printf("%d",'++');

    the ascii value of + is 43... the output comes as 11051 in turboC... (i
    was surprised it doesnt throw an error on compiling... ) someone
    explain me as to how this output comes...
     
    fidlee, Dec 23, 2005
    #1
    1. Advertising

  2. On Fri, 23 Dec 2005 04:33:33 -0800, fidlee wrote:

    > what should be the ouptut of
    > printf("%d",'++');
    >
    > the ascii value of + is 43... the output comes as 11051 in turboC...
    > (i was surprised it doesnt throw an error on compiling... ) someone
    > explain me as to how this output comes...


    6.4.4.4 "Character constants" #10:
    The value of an integer character constant containing more than one
    character (e.g., 'ab'), or containing a character or escape sequence that
    does not map to a single-byte execution character, is
    implementation-defined.

    In your case, 43 * 256 + 43.

    --
    Pieter Droogendijk <pieter at binky dot org dot uk>
    PGP/1E92DBBC [ Make way for the Emperor's Finest. ] binky.org.uk
     
    Pieter Droogendijk, Dec 23, 2005
    #2
    1. Advertising

  3. fidlee wrote:
    > what should be the ouptut of
    > printf("%d",'++');
    >
    > the ascii value of + is 43... the output comes as 11051 in turboC... (i
    > was surprised it doesnt throw an error on compiling... ) someone
    > explain me as to how this output comes...


    Character constants can contain multiple characters, the integer value
    of which is implementation-defined. You will need to consult the
    documentation that came with your compiler for the details but you
    might observe the fact that 43*256+43=11051.

    Robert Gamble
     
    Robert Gamble, Dec 23, 2005
    #3
  4. fidlee

    fidlee Guest

    @ Robert

    so u mean to say that this statement will behave differently on the
    different compilers??
    i also tried out
    printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
    (windows) i dont think they are compiler dependednt...

    Can someone please tell me as to why the compiler behaves in the
    following way?? (43*256+43=11051)
     
    fidlee, Dec 23, 2005
    #4
  5. fidlee

    Stefan Krah Guest

    * fidlee <> wrote:
    > what should be the ouptut of
    > printf("%d",'++');
    >
    > the ascii value of + is 43... the output comes as 11051 in turboC... (i
    > was surprised it doesnt throw an error on compiling... ) someone
    > explain me as to how this output comes...


    gcc gives this warning:

    multichar.c:5:15: warning: multi-character character constant

    Now you should be equipped for a Google search. ;)


    Stefan Krah

    --
    Break original Enigma messages: http://www.bytereef.org/m4_project.html
     
    Stefan Krah, Dec 23, 2005
    #5
  6. fidlee

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    fidlee wrote:
    > @ Robert
    >
    > so u mean to say that this statement will behave differently on the
    > different compilers??
    > i also tried out
    > printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
    > (windows) i dont think they are compiler dependednt...


    Certainly, the results will be compiler dependant. It's just that both
    of your compilers give the same results :)

    Consider that the result depends on
    - - the width (sizeof()) of a character constant (hint: it isn't
    necessarily == 1)
    - - the 'endianness' of a character constant (hint: it isn't necessarily
    little-endian)
    - - the characterset used to express characters in (hint: it isn't always
    ASCII)

    Think of what the differences might be between your results and the
    results of the same program when compiled and run on a system with
    - - 32bit integer values
    - - big-endian integers
    - - EBCDIC-US characterset

    > Can someone please tell me as to why the compiler behaves in the
    > following way?? (43*256+43=11051)


    Because, on /your/ system, character constants are stored as 16bit
    integer values, in the ASCII characterset. Since the ASCII for '+' is
    binary 43, then the first '+' (being shifted left into the high-order 8
    bits of your 16bit integer) becomes 43*256 (or 43 << 8). The second '+'
    (not moving from the low-order position) becomes 43. And the combination
    of the two (a 16bit integer, remember?) becomes (45*256)+43



    - --

    Lew Pitcher, IT Specialist, Enterprise Data Systems
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFDq/8+agVFX4UWr64RAnAEAJ95BsHcGCYBqkvNRyHHzggeNFTPZwCeJvP6
    ymeBG+DMNGdtX6rSOY+HKVI=
    =Ra/t
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Dec 23, 2005
    #6
  7. fidlee

    fidlee Guest

    true that it does give the multi character constant warning... but in
    ends up giving the same o/p right ?? :)
     
    fidlee, Dec 23, 2005
    #7
  8. On Fri, 23 Dec 2005 05:19:31 -0800, fidlee wrote:

    > so u mean to say that this statement will behave differently on the
    > different compilers??


    Not quite. Different compilers, architectures, operating systems,
    characters sets, etcetera.

    > i also tried out
    > printf("%d",'+a'); and it gives 11105 on g++ (linux)


    g++ implies a C++ compiler, mind. You probably mean gcc.

    > and turboC (windows) i dont think they are compiler dependednt...


    It might do the same on a whole lot of compilers for Windows and Linux.
    Try it on a few more systems, especially if they don't use ASCII.

    Or better yet, try it on a mainframe.

    > Can someone please tell me as to why the compiler behaves in the
    > following way?? (43*256+43=11051)


    Not I. Perhaps your compiler documentation or operating system manual can
    help you there.

    --
    Pieter Droogendijk <pieter at binky dot org dot uk>
    PGP/1E92DBBC [ Make way for the Emperor's Finest. ] binky.org.uk
     
    Pieter Droogendijk, Dec 23, 2005
    #8
  9. fidlee

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Lew Pitcher wrote:
    > fidlee wrote:
    >
    >>>@ Robert
    >>>
    >>>so u mean to say that this statement will behave differently on the
    >>>different compilers??
    >>>i also tried out
    >>>printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
    >>>(windows) i dont think they are compiler dependednt...

    >
    >
    > Certainly, the results will be compiler dependant. It's just that both
    > of your compilers give the same results :)
    >
    > Consider that the result depends on
    > - the width (sizeof()) of a character constant (hint: it isn't
    > necessarily == 1)
    > - the 'endianness' of a character constant (hint: it isn't necessarily
    > little-endian)
    > - the characterset used to express characters in (hint: it isn't always
    > ASCII)
    >
    > Think of what the differences might be between your results and the
    > results of the same program when compiled and run on a system with
    > - 32bit integer values
    > - big-endian integers
    > - EBCDIC-US characterset


    For example, here's a test on /my/ system

    source code:

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    printf("The character constant '++' == %d\n",'++');
    return EXIT_SUCCESS;
    }


    execution results:

    The character constant '++' == 20046


    Of course, this was compiled and executed on an IBM OS/390 operating
    system using the OS/390 C V2 R10 compiler (5647A01). OS/390 uses EBCDIC
    variant charactersets, and the source code was written using the
    EBCDIC-US variant.

    [snip]

    - --

    Lew Pitcher, IT Specialist, Enterprise Data Systems
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFDrAJ2agVFX4UWr64RAiFlAJ9Lqz5umtvPU9xWbKkFRyX85PXWZwCgiCYF
    GAh416UYHPyruVU5PoEusow=
    =RCUr
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Dec 23, 2005
    #9
  10. fidlee

    fidlee Guest

    @Pieter
    thanx for the help offered...
    but i still do think that there must be some logic as to y the c
    compiler behaves in this fashion...

    if someone knows d logic, just let me know ...
     
    fidlee, Dec 23, 2005
    #10
  11. [Original context restored]

    Please read the document located at http://cfaj.freeshell.org/google/
    before posting again to this group.

    > fidlee wrote:
    > > Robert Gamble wrote:
    > > > fidlee wrote:
    > > > what should be the ouptut of
    > > > printf("%d",'++');
    > > >
    > > > the ascii value of + is 43... the output comes as 11051 in turboC... (i
    > > > was surprised it doesnt throw an error on compiling... ) someone
    > > > explain me as to how this output comes...

    > >
    > > Character constants can contain multiple characters, the integer value
    > > of which is implementation-defined. You will need to consult the
    > > documentation that came with your compiler for the details but you
    > > might observe the fact that 43*256+43=11051.
    > >

    > @ Robert
    >
    > so u mean to say that this statement will behave differently on the
    > different compilers??


    I mean what I said which was that the value of such character constants
    is implementation-defined, it may very well behave differently on
    different compilers and you can be sure that not all compilers will
    treat them the same way.

    > i also tried out
    > printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
    > (windows) i dont think they are compiler dependednt...


    First off, g++ is a c++ compiler, C and C++ are different languages,
    the latter of which is off-topic here. Secondly, just because two or
    more compilers behave in the same manner does not mean that the results
    are portable or even Standard conforming, thinking otherwise would be a
    serious error.

    > Can someone please tell me as to why the compiler behaves in the
    > following way?? (43*256+43=11051)


    As I and others have already told you, the Standard declares this to be
    implementation-defined, therefore any reasons for why this behavior is
    defined in a specific way on an a certain implementation would not be
    found in the Standard but in the documentation that came with your
    compiler. If your documentation does not provide an explanation to
    your liking, ask the implementors for a rationale, not us.

    Robert Gamble
     
    Robert Gamble, Dec 23, 2005
    #11
  12. fidlee

    Stefan Krah Guest

    * fidlee <> wrote:
    > true that it does give the multi character constant warning... but in
    > ends up giving the same o/p right ?? :)


    My point (which you didn't quote) was that your question could possibly
    be answered by googling for "multi-character character constant".


    Stefan Krah

    --
    How to quote using Google Groups: http://cfaj.freeshell.org/google/
    Break original Enigma messages: http://www.bytereef.org/m4_project.html
     
    Stefan Krah, Dec 23, 2005
    #12
  13. fidlee

    fidlee Guest

    @Lew Pitcher
    thanx for your answer... but the reason y i am getting confused is
    this:
    char a='i';
    printf("%d",a);

    the following code gives the output as 1

    whereas
    printf("%d",'i')

    gives the output as 2

    arent both character constants here??

    why does the compiler react differently??

    this may be a simple question for you guys but i have been tryin hard
    to understand...
    so please dont lose your temper :)
     
    fidlee, Dec 23, 2005
    #13
  14. fidlee

    Chuck F. Guest

    fidlee wrote:
    > @Pieter
    > thanx for the help offered...
    > but i still do think that there must be some logic as to y the c
    > compiler behaves in this fashion...
    >
    > if someone knows d logic, just let me know ...


    I is spelled capitalized. I know of no c compiler called y, thus
    cannot explain its aberrant and unspecified behaviour. I also
    never heard of d logic. Is this something like so-called female logic?

    Believe it or not, there is a reason that Usenet protocols have
    evolved over the past thirty plus years, and that these include
    adequate context and spelling.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
     
    Chuck F., Dec 23, 2005
    #14
  15. fidlee

    fidlee Guest

    @Chuck F

    I am really sorry for all the spelling mistakes. I will make sure that
    there are no more of them from now on.
    By the way it seems that you are the guardian of the 'Usenet messaging
    standards'
    Because I have seen similar comments from you on many other posts.

    Have you ever posted a message that has proved helpful to someone ? :))
     
    fidlee, Dec 23, 2005
    #15
  16. fidlee

    Zara Guest

    On 23 Dec 2005 04:33:33 -0800, "fidlee" <> wrote:

    >what should be the ouptut of
    >printf("%d",'++');
    >
    >the ascii value of + is 43... the output comes as 11051 in turboC... (i
    >was surprised it doesnt throw an error on compiling... ) someone
    >explain me as to how this output comes...



    It is implementation dependent, although it is legal.

    In your case, b ut not guaranteed to be portable, you have got an int
    composed of two bytes with value '+' (43), thta is: 257*43=11051

    Don't count on it for other compilers/platforms

    Regards,

    Zara
     
    Zara, Dec 23, 2005
    #16
  17. fidlee

    Flash Gordon Guest

    fidlee wrote:
    > @Chuck F
    >
    > I am really sorry for all the spelling mistakes. I will make sure that
    > there are no more of them from now on.


    In general we don't mind spelling mistakes or poor grammar since we
    recognise that posting in a foreign language can be difficult. However,
    since most languages have similar capitalisation rules that in general
    is not a good reason for not using any capitalisation. Also,
    abbreviating words to single characters makes it hard to read posts, and
    why should many people have to work to read a post when the author for
    negligible extra effort can write it out in full?

    > By the way it seems that you are the guardian of the 'Usenet messaging
    > standards'


    He is not, but *many* of us agree with Chuck. Since you are using Google
    Groups read http://cfaj.freeshell.org/google/

    > Because I have seen similar comments from you on many other posts.


    That is because you have been making it hard for people to understand
    your posts and the instructions are posted here regularly, so you should
    have seen them before you first posted.

    > Have you ever posted a message that has proved helpful to someone ? :))


    Chuck has posted many helpful posts, and that includes the posts telling
    people how to post properly. :)
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 23, 2005
    #17
  18. fidlee

    Thad Smith Guest

    fidlee wrote:

    > @Lew Pitcher
    > thanx for your answer... but the reason y i am getting confused is
    > this:
    > char a='i';
    > printf("%d",a);
    >
    > the following code gives the output as 1
    >
    > whereas
    > printf("%d",'i')
    >
    > gives the output as 2
    >
    > arent both character constants here??


    Yes, 'i' is a character constant in both cases, but it has type int. In
    the first case you are assigning it to a type char, possibly signed
    (implementation-defined), then converting the type char to int. In the
    second case, you are not converting to type char. If your char type is
    signed and (char)'i' is negative (depends on the character set and
    number of bits in a byte), you can get different values printed.

    In your situation, I suspect that there are other reasons that are not
    visible in the snippets you posted.

    --
    Thad
     
    Thad Smith, Dec 23, 2005
    #18
  19. "fidlee" <> writes:
    > @Lew Pitcher
    > thanx for your answer... but the reason y i am getting confused is
    > this:
    > char a='i';
    > printf("%d",a);
    >
    > the following code gives the output as 1


    What do you mean by "following"?

    > whereas
    > printf("%d",'i')
    >
    > gives the output as 2
    >
    > arent both character constants here??
    >
    > why does the compiler react differently??


    Are you sure about that? Please show us a small, complete, compilable
    program that demonstrates this. On my system, the following program:

    #include <stdio.h>
    int main(void)
    {
    char a='i';
    printf("%d\n", a);
    printf("%d\n", 'i');
    return 0;
    }

    produces the following output:

    105
    105

    If your system uses ASCII or an ASCII-like character set (as it almost
    certainly does), the output of both printf statements should be 105.
    Regardless of the character set, the output should the the same for
    both statements.

    And please read <http://cfaj.freeshell.org/google/> and follow its
    advice.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 23, 2005
    #19
  20. fidlee

    Joe Wright Guest

    fidlee wrote:
    > @ Robert
    >
    > so u mean to say that this statement will behave differently on the
    > different compilers??
    > i also tried out
    > printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
    > (windows) i dont think they are compiler dependednt...
    >
    > Can someone please tell me as to why the compiler behaves in the
    > following way?? (43*256+43=11051)
    >

    Naturally..
    '+'
    00000000 00000000 00000000 00101011
    43
    '++'
    00000000 00000000 00101011 00101011
    11051
    '+++'
    00000000 00101011 00101011 00101011
    2829099
    '++++'
    00101011 00101011 00101011 00101011
    724249387

    ...don't you think?
    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Dec 23, 2005
    #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. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,030
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,218
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    2,000
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    421
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    581
    Bullschmidt
    May 11, 2004
Loading...

Share This Page