473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

program output

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

May 17 '06 #1
7 1386
jjf

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 '06 #2
Thanx . can u review my code and do some necessary corection...
jj*@bcs.org.uk 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 ...


May 17 '06 #3
sonu wrote:
jj*@bcs.org.uk 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.

May 17 '06 #4
On 2006-05-17, sonu <sa****************@gmail.com> 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>
May 17 '06 #5

sonu wrote:
jj*@bcs.org.uk 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".

May 17 '06 #6
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)
May 17 '06 #7

"Richard Heathfield" <in*****@invalid.invalid> 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

May 17 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
2
by: atreide_son | last post by:
hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
1
by: sewid | last post by:
Hi there! I've got a problem with no solution, I hope you might help me. I am writing a small tool with many buttons. Every button starts a thread and this thread starts something else, in the...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
13
by: Liang Chen | last post by:
Hope you all had a nice weekend. I have a question that I hope someone can help me out. I want to run a Python program that uses Tkinter for the user interface (GUI). The program allows me to type...
10
by: len | last post by:
I have created the following program to read a text file which happens to be a cobol filed definition. The program then outputs to a file what is essentially a file which is a list definition...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.