program output

S

sonu

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
 
J

jjf

sonu said:
(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 ...
 
A

Andrew Poelstra

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.
 
V

Vladimir Oka

sonu said:
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".
 
R

Richard Heathfield

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.
 
M

Malcolm

Richard Heathfield said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top