473,796 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add 4 20 digit numbers in C

Hi,

Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and
pass the result back to a calling program in COBOL? We were able to
add up to 15 digit numbers without any problems, but we started facing
issues once we go above 15 digits.

Thanks,
Venkat

Nov 8 '07 #1
14 8663
On Wed, 07 Nov 2007 20:43:47 -0800, thehobbit <ve**********@g mail.com>
wrote in comp.lang.c:
Hi,

Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and
pass the result back to a calling program in COBOL? We were able to
add up to 15 digit numbers without any problems, but we started facing
issues once we go above 15 digits.
ANSI C, or ISO C, or just plain standard C, does not define linkage to
COBOL or any other language. So there is no way to do this in "ANSI
C".

On the other hand, you may have an issue with numeric precision in the
C implementation you are using, regardless of how you are getting the
values into the C program.

But you haven't provided nearly enough information. What types are
these numbers? Integers, floating point, what? What data type are
they defined as in C?

And you haven't told us what "problems" you think you are having.

How many digits does the C data type you are using have?

If this is an integer type, you can look at the type_MAX macro in
<limits.hto tell you what the range of values the type can handle
is.

If you are using one of the floating point types, look at type_DIG in
<float.h>.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Nov 8 '07 #2

Thank you for the details jack... actually am a nincompoop in C...
more of a COBOL programmer... actually what we are trying to do is add
4 20 digit integers and return the sum back to the COBOL program. This
is because COBOL has a limitation of 18 digits. When we added 15 4
digit integer numbers we got the expected sum, however the moment we
started adding 16 4 digit integers then the sum was all wrong... the
code we kind of wrote is as follows; Please let us know if you have
any suggestions...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Function to add 4 integers
*/

void ccadd(char *inp1,char *inp2,char *inp3,char *inp4,char *out)
{
long long int_inp1,int_in p2,int_inp3,int _inp4,int_out;
int_inp1 = atoi(inp1);
int_inp2 = atoi(inp2);
int_inp3 = atoi(inp3);
int_inp4 = atoi(inp4);

int_out = int_inp1 + int_inp2 + int_inp3 + int_inp4;
sprintf(out,"%l f",int_out);
}
On Nov 7, 9:20 pm, Jack Klein <jackkl...@spam cop.netwrote:
On Wed, 07 Nov 2007 20:43:47 -0800, thehobbit <venkat.na...@g mail.com>
wrote in comp.lang.c:
Hi,
Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and
pass the result back to a calling program in COBOL? We were able to
add up to 15 digit numbers without any problems, but we started facing
issues once we go above 15 digits.

ANSI C, or ISO C, or just plain standard C, does not define linkage to
COBOL or any other language. So there is no way to do this in "ANSI
C".

On the other hand, you may have an issue with numeric precision in the
C implementation you are using, regardless of how you are getting the
values into the C program.

But you haven't provided nearly enough information. What types are
these numbers? Integers, floating point, what? What data type are
they defined as in C?

And you haven't told us what "problems" you think you are having.

How many digits does the C data type you are using have?

If this is an integer type, you can look at the type_MAX macro in
<limits.hto tell you what the range of values the type can handle
is.

If you are using one of the floating point types, look at type_DIG in
<float.h>.

--
Jack Klein
Home:http://JK-Technology.Com
FAQs for
comp.lang.chttp ://c-faq.com/
comp.lang.c++http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

Nov 8 '07 #3
thehobbit wrote:

In future, please don't top post.
Thank you for the details jack... actually am a nincompoop in C...
more of a COBOL programmer... actually what we are trying to do is add
4 20 digit integers and return the sum back to the COBOL program. This
is because COBOL has a limitation of 18 digits. When we added 15 4
digit integer numbers we got the expected sum, however the moment we
started adding 16 4 digit integers then the sum was all wrong... the
code we kind of wrote is as follows; Please let us know if you have
any suggestions...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Function to add 4 integers
*/

void ccadd(char *inp1,char *inp2,char *inp3,char *inp4,char *out)
Make the inputs const char*.
{
long long int_inp1,int_in p2,int_inp3,int _inp4,int_out;
int_inp1 = atoi(inp1);
Don't use atoi to convert a C string to long long, use strtoll, or if
you are 100% sure the C strings represent valid numbers, atoll.

--
Ian Collins.
Nov 8 '07 #4
On Nov 7, 10:22 pm, Ian Collins <ian-n...@hotmail.co mwrote:
thehobbit wrote:

In future, please don't top post.
Thank you for the details jack... actually am a nincompoop in C...
more of a COBOL programmer... actually what we are trying to do is add
4 20 digit integers and return the sum back to the COBOL program. This
is because COBOL has a limitation of 18 digits. When we added 15 4
digit integer numbers we got the expected sum, however the moment we
started adding 16 4 digit integers then the sum was all wrong... the
code we kind of wrote is as follows; Please let us know if you have
any suggestions...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Function to add 4 integers
*/
void ccadd(char *inp1,char *inp2,char *inp3,char *inp4,char *out)

Make the inputs const char*.
{
long long int_inp1,int_in p2,int_inp3,int _inp4,int_out;
int_inp1 = atoi(inp1);

Don't use atoi to convert a C string to long long, use strtoll, or if
you are 100% sure the C strings represent valid numbers, atoll.

--
Ian Collins.
thanks... i am sure that they represent valid numbers.. but not able
to use atoll function on my system (called HP3000)... gives a run time
error. any particular library needs to be included when using the
atoll function?

Nov 8 '07 #5
thehobbit said:
On Nov 7, 10:22 pm, Ian Collins <ian-n...@hotmail.co mwrote:
<snip>
>>
Don't use atoi to convert a C string to long long, use strtoll, or if
you are 100% sure the C strings represent valid numbers, atoll.

thanks... i am sure that they represent valid numbers.. but not able
to use atoll function on my system (called HP3000)... gives a run time
error. any particular library needs to be included when using the
atoll function?

Ian was working on the incorrect assumption that you have access to a C99
compiler. It is odd that he should assume this, since practically nobody
has access to a C99 compiler.

The problem you are having is that the numbers you wish to add are too big
to fit into any of the integer types you appear to have available on your
system, and too big to fit *exactly* into any of the floating point types.

It's at times like this that you need to reach for your friendly
neighbourhood "bignum" library, such as GMP (Gnu Maths Package, I think)
or Miracl. These libraries provide the functionality you need. (If you
don't like that answer, you could do what I did, which is to write your
own bignum library - and, if all you want to do is add two (or more) big
numbers together, it's pretty easy to do that yourself. Just think about
how you stop overflow. In fact, just think about how you don't even
*consider* overflow when adding numbers with pencil and paper. It's never
an issue, is it? Why not? Because you have more paper on the left, that's
why, and you allocate enough for your needs, which you can more or less
work out in your head (for any two numbers neither of which is more than D
digits long, D + 1 digits will suffice to store the answer, sometimes with
room to spare).

--
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
Nov 8 '07 #6
Richard Heathfield wrote:
thehobbit said:
>On Nov 7, 10:22 pm, Ian Collins <ian-n...@hotmail.co mwrote:

<snip>
>>Don't use atoi to convert a C string to long long, use strtoll, or if
you are 100% sure the C strings represent valid numbers, atoll.
thanks... i am sure that they represent valid numbers.. but not able
to use atoll function on my system (called HP3000)... gives a run time
error. any particular library needs to be included when using the
atoll function?


Ian was working on the incorrect assumption that you have access to a C99
compiler. It is odd that he should assume this, since practically nobody
has access to a C99 compiler.
It seamed a fair assumption as he was using long long. I don't know
about other platforms, but Solaris has had these function for at least a
decade. I'm sure most people here have access to systems with strtoll
and compilers with long long, fully C99 compliant or not.

--
Ian Collins.
Nov 8 '07 #7
Ian Collins said:
Richard Heathfield wrote:
<snip>
>Ian was working on the incorrect assumption that you have access to a
C99 compiler. It is odd that he should assume this, since practically
nobody has access to a C99 compiler.
It seamed a fair assumption as he was using long long.
Ian, I apologise. I am guilty of not reading the thread history carefully
enough. He does indeed use long long int in his own code, and what was
needed was indeed a C99 usage lesson rather than a bignum package.

<snip>

--
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
Nov 8 '07 #8
don't top post. I've rearranged things
On 8 Nov, 06:13, thehobbit <venkat.na...@g mail.comwrote:
On Nov 7, 9:20 pm, Jack Klein <jackkl...@spam cop.netwrote:
On Wed, 07 Nov 2007 20:43:47 -0800, thehobbit <venkat.na...@g mail.com>
Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and
pass the result back to a calling program in COBOL? We were able to
add up to 15 digit numbers without any problems, but we started facing
issues once we go above 15 digits.
ANSI C, or ISO C, or just plain standard C, does not define linkage to
COBOL or any other language. So there is no way to do this in "ANSI
C".
On the other hand, you may have an issue with numeric precision in the
C implementation you are using, regardless of how you are getting the
values into the C program.
But you haven't provided nearly enough information. What types are
these numbers? Integers, floating point, what? What data type are
they defined as in C?
And you haven't told us what "problems" you think you are having.
How many digits does the C data type you are using have?
If this is an integer type, you can look at the type_MAX macro in
<limits.hto tell you what the range of values the type can handle
is.
If you are using one of the floating point types, look at type_DIG in
<float.h>.
--
Jack Klein
Home:http://JK-Technology.Com
FAQs for
comp.lang.chttp ://c-faq.com/
comp.lang.c++http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
don't quote sigs (the bit after ("-- ")

Thank you for the details jack... actually am a nincompoop in C...
more of a COBOL programmer... actually what we are trying to do is add
4 20 digit integers and return the sum back to the COBOL program. This
is because COBOL has a limitation of 18 digits. When we added 15 4
digit integer numbers we got the expected sum,
? confusion

if you want to add 4 20-digit numbers why are you adding 4-digit
numbers?

however the moment we
started adding 16 4 digit integers then the sum was all wrong...
could you give an example of the numbers you used that gave the wrong
answer?

the code we kind of wrote is as follows;
this code doesn't add 16 numbers
Please let us know if you have
any suggestions...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Function to add 4 integers
*/

void ccadd(char *inp1,char *inp2,char *inp3,char *inp4,char *out)
this is more of a style point. Why not use an array? And since your
program doesn't change the input values make it const.

void ccadd (const char *inp[], int in_count, char *out)

{
long long int_inp1,int_in p2,int_inp3,int _inp4,int_out;
int_inp1 = atoi(inp1);
atoi() has poor error checking. And it doesn't return a long long.
Use a strtoX() function. And use one that returns a long long
or unsigned long long. Check for errors.

int_inp2 = atoi(inp2);
int_inp3 = atoi(inp3);
int_inp4 = atoi(inp4);

int_out = int_inp1 + int_inp2 + int_inp3 + int_inp4;
sprintf(out,"%l f",int_out);

}
try posting a complete program and its results
--
Nick Keighley

Nov 8 '07 #9
thehobbit <ve**********@g mail.comwrites:
On Nov 7, 10:22 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>thehobbit wrote:
/*
* Function to add 4 integers
*/
void ccadd(char *inp1,char *inp2,char *inp3,char *inp4,char *out)

Make the inputs const char*.
{
long long int_inp1,int_in p2,int_inp3,int _inp4,int_out;
int_inp1 = atoi(inp1);

Don't use atoi to convert a C string to long long, use strtoll, or if
you are 100% sure the C strings represent valid numbers, atoll.

--
Ian Collins.
Please don't keep quoting sigs.
thanks... i am sure that they represent valid numbers.. but not able
to use atoll function on my system (called HP3000)... gives a run time
error. any particular library needs to be included when using the
atoll function?
I have not heard of atoll but some versions of HP-UX do seem to be
missing strtoll. It would appear they have strtoimax[1] which is a
POSIX function to do the same job. I say "seem" and "appear to"
because I don't know -- I just did the web search you should have done
when you found the answer was a function that seems to be missing --
strtoll.

[1] http://www.opengroup.org/onlinepubs/...strtoimax.html

--
Ben.
Nov 8 '07 #10

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

Similar topics

3
4613
by: Matt Smolic | last post by:
Does anyone know where I can get some info on creating customer account numbers, part numbers and such. In other words what the logic is behind their creation. I am not looking for code, just how they go about it. I don't want to use something like a phone number or anything else that may change over time. Any info would be greatly appreciated. Thanks Matt
11
17820
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is greater number than second 20 digit number ,then it should return.
0
1193
by: ptek | last post by:
Hi, Is it possible to show "001" in a NumericUpDown control when its value is "1" ? That is, displaying a fixed number of digits, 0 padded. I've read its documentation but I haven't found anything... Thanks
5
1694
by: sesling | last post by:
I have a database field that stores 8 and 9 digit values. I need to calculate the sum value using the first 8 digits. ex.of stored numbers 123456781 234567892 45678903 987654321 calculation should use 12345678 23456789
2
6404
by: Zytan | last post by:
This page http://blog.stevex.net/index.php/string-formatting-in-csharp/ shows: {0:0,0} but for single digit numbers, it prefixes a 0! Is there a way that works with single digit numbers? thanks Zytan
2
3432
by: puneet vyas | last post by:
hi,can any one put light on this question? thanks puneet vyas
7
21602
by: harijay | last post by:
Hi I am a few months new into python. I have used regexps before in perl and java but am a little confused with this problem. I want to parse a number of strings and extract only those that contain a 4 digit number anywhere inside a string However the regexp p = re.compile(r'\d{4}')
3
4712
by: Qxx1 | last post by:
hello everyone i have to code a simple calculator program that performs addition,multiplication,division n substraction operations. The operations are to be performed on 200 digit numbers (i.e 2131237287293.....upto 200 digits) however multiplication and division can be neglected but i need to perform atleast addition and substraction. I am done with the calculator code, but the 200 digit thing is wat i am struck at. How to do it? wat is...
3
1769
by: shalskedar | last post by:
In my Database for 1 of the fields as "ID " i need to display the criteria as 10/2...followed by some characters till 90/2--- for ex-10/2001 11/2008 ---- ---- 90/2005
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.