program output

Discussion in 'C Programming' started by sonu, May 17, 2006.

  1. sonu

    sonu Guest

    Hi all,
    from my code i want the out put should be 502 but its giving 7 pls any
    one try to correct.
    (Actualy i want to write a similar function as atol() which takes const
    char * and gives long but i need my function should take const wchar_t
    * and returns long.)

    #include<stdlib.h>
    int main()
    {
    long fun(const wchar_t *);
    const wchar_t *a=L"a500";
    const wchar_t *b=L"2";
    long c;

    c=fun(a)+fun(b);
    printf("%d\n",c);
    }

    long fun(const wchar_t *a)
    {
    const char *b;
    b=(const char *)a;
    return atol(b);
    }


    Tkanks
    Sonu
     
    sonu, May 17, 2006
    #1
    1. Advertising

  2. sonu

    Guest

    sonu wrote:
    >
    > (Actualy i want to write a similar function as atol() which takes const
    > char * and gives long but i need my function should take const wchar_t
    > * and returns long.)


    Then why not use wcstol() since that's what it's for. It should be
    documented in your local C book, man pages, WWW ...
     
    , May 17, 2006
    #2
    1. Advertising

  3. sonu

    sonu Guest

    Thanx . can u review my code and do some necessary corection...
    wrote:
    > sonu wrote:
    > >
    > > (Actualy i want to write a similar function as atol() which takes const
    > > char * and gives long but i need my function should take const wchar_t
    > > * and returns long.)

    >
    > Then why not use wcstol() since that's what it's for. It should be
    > documented in your local C book, man pages, WWW ...
     
    sonu, May 17, 2006
    #3
  4. sonu

    santosh Guest

    sonu wrote:
    > wrote:
    > > sonu wrote:
    > > >
    > > > (Actualy i want to write a similar function as atol() which takes const
    > > > char * and gives long but i need my function should take const wchar_t
    > > > * and returns long.)

    > >
    > > Then why not use wcstol() since that's what it's for. It should be
    > > documented in your local C book, man pages, WWW ...

    > Thanx . can u review my code and do some necessary corection...


    Please don't top-post.
     
    santosh, May 17, 2006
    #4
  5. On 2006-05-17, sonu <> wrote:
    > Hi all,
    > from my code i want the out put should be 502 but its giving 7 pls any
    > one try to correct.

    Type properly. It took me three tries to figure out what you meant by "pls",
    "out put", lowercase i, "any one", lack of any capitals, etc. You have a
    shift key for a reason.

    > (Actualy i want to write a similar function as atol() which takes const
    > char * and gives long but i need my function should take const wchar_t
    > * and returns long.)
    >

    Still with the grammar... people can't read what you are saying if you
    don't take the time to type it out.

    > #include<stdlib.h>
    > int main()
    > {
    > long fun(const wchar_t *);
    > const wchar_t *a=L"a500";
    > const wchar_t *b=L"2";
    > long c;
    >
    > c=fun(a)+fun(b);
    > printf("%d\n",c);
    > }
    >
    > long fun(const wchar_t *a)
    > {
    > const char *b;
    > b=(const char *)a;
    > return atol(b);
    > }
    >
    >
    > Tkanks
    > Sonu
    >

    You didn't return anything from main. You'd better do that.

    A char is CHAR_BITS wides. This is usually 8 or 9. A wchar is substantially
    larger; that's why it has its own type. When you cast a wchar * to a char *,
    you end up with a pointer to the first CHAR_BITS bits of your char.

    Of course that wasn't going to work.


    --

    Andrew Poelstra <http://www.wpsoftware.net/blog>
     
    Andrew Poelstra, May 17, 2006
    #5
  6. sonu

    Vladimir Oka Guest

    sonu wrote:
    > wrote:
    > > sonu wrote:
    > > >
    > > > (Actualy i want to write a similar function as atol() which takes const
    > > > char * and gives long but i need my function should take const wchar_t
    > > > * and returns long.)

    > >
    > > Then why not use wcstol() since that's what it's for. It should be
    > > documented in your local C book, man pages, WWW ...

    >
    > Thanx . can u review my code and do some necessary corection...


    Don't top post. Read
    <http://www.clc-wiki.net/wiki/Introduction_to_comp.lang.c>.

    Feeling generous today:

    #include <stdio.h>
    #include <wchar.h>

    int main(void)
    {
    const wchar_t *a=L"500";
    const wchar_t *b=L"2";

    long c;

    c = wcstol(a, NULL, 10) + wcstol(b, NULL, 10);

    printf("%ld\n", c);

    return 0;
    }

    I'll leave it to you, and your textbooks, to explain all the changes to
    your original code. Don't skip this last step, and look for changes
    carefully.

    PS
    Your original code could not have output 7. It actually output 2, as
    you mistyped your first string as "a500".
     
    Vladimir Oka, May 17, 2006
    #6
  7. Andrew Poelstra said:

    > A char is CHAR_BITS wides.


    A char is CHAR_BIT bits wide.

    > This is usually 8 or 9.


    It's always 8 or more. 8 is common, 16 and 32 are reasonably common, but 9
    is relatively rare, I think. Not unheard of, of course.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 17, 2006
    #7
  8. sonu

    Malcolm Guest

    "Richard Heathfield" <> wrote in message
    >> This is usually 8 or 9.

    >
    > It's always 8 or more. 8 is common, 16 and 32 are reasonably common, but 9
    > is relatively rare, I think. Not unheard of, of course.
    >

    The Nintendo 64 has 8 and a half bits per byte.
    It looks like a regular 8-bit machine, but there is a special assembly
    instruction you can use to get access to the 9th bit.
    --
    www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm, May 17, 2006
    #8
    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. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    595
    John Bentley
    Sep 10, 2003
  2. chuck amadi
    Replies:
    1
    Views:
    491
    Larry Bates
    Jun 23, 2004
  3. Puneet
    Replies:
    16
    Views:
    1,138
    Daniel Vallstrom
    Mar 20, 2005
  4. Paul
    Replies:
    1
    Views:
    430
  5. jmr
    Replies:
    2
    Views:
    337
Loading...

Share This Page