473,412 Members | 1,944 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,412 software developers and data experts.

add 2 FLOAT variables

I am difficulty adding 2 variables which are type float.

help.


float a, b, total;

a = 1.5;
b = 1.5;
total = 0;
printf("total = %f", sum(a,b));

float sum(float a, float b)
{
return total= a + b;
}
Oct 28 '08 #1
6 6505
Ganon11
3,652 Expert 2GB
Your function isn't working properly because you don't have a good understanding of what a function does. Inside sum(), total doesn't exist. Also, you are returning a value, but you are not capturing the return value. Go to a basic tutorial on functions and read it.
Oct 28 '08 #2
//total is a global variable;

what about now?
Oct 28 '08 #3
#include <stdio.h>

float sum(float x, float y);

float a = 1.5;
float b = 1.5;
float total = 0;


main()
{
printf("total = %f", sum(a,b));
system("pause");
}

float sum(float x, float y)
{
return total = x + y;
}

this worked.

and YES, total was recognized in sum. ;-)
Oct 28 '08 #4
Banfa
9,065 Expert Mod 8TB
But it is very poor practice to store the output of a function in a global variable like that. The total global variable is unrequired in your simple example.

Global data in general is bad practice and should be avoided so having a and b as globals when they could be local to main is bad practice.
Oct 28 '08 #5
It was for demonstration/practice purposes only.
Oct 28 '08 #6
Banfa
9,065 Expert Mod 8TB
Get into the habit of using best practice even for unimportant code, then you will be less likely to introduce bugs when the code you are writing is important.
Oct 28 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Peter Scheurer | last post by:
I have a statement that works in compatibility mode 8 of SQL Server 2000 while it fails in 7: declare @P3 int exec sp_prepexec @P3 output, N'@P1 float', N'select custname from customer where...
2
by: Michael Klatt | last post by:
I've been looking through the FAQ and Googling previous threads on c.l.c++ but I haven't seen this exact situation addressed. I initialize variables of type float to a known invalid value (of type...
16
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why...
3
by: Piotrekk | last post by:
Hi I have important question. This is the way iam calculating machine epsilon float fEps = 1.0f, fStore = 2.0f; int i1 = 0; // Calculating epsilon for float while (fStore > 1.0f) {
15
by: k3n3dy | last post by:
Hello everybody, I would like to ask anybody to help me understand what should be done exactly with this Problem. I have provided what I have up to now, although it is not quite accurate. Create...
16
by: chandanlinster | last post by:
As far as I know floating point variables, that are declared as float follow IEEE format representation (which is 32-bit in size). But chapter1-page no 9 of the book "The C programming language"...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
3
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
7
by: alphasahoo | last post by:
Hi I am working on a program which writes the output a SQL select statements from number of source tables first to a load matrix and then writes to a load.dat file. But while writing to the...
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:
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...
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
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...
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.