473,406 Members | 2,208 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,406 software developers and data experts.

C functions and data types

I guess I don't fully understand the way functions in C work.

For instance, I'll use this program as an example:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. float celsius(int);
  4.  
  5. main()
  6. {
  7.  
  8.     int a;
  9.  
  10.     for(a  = 0; a <= 300; ++a)
  11.         celsius(a);
  12.  
  13.     return 0;
  14. }
  15.  
  16. float celsius(int fahr)
  17. {
  18.     int a, b;
  19.  
  20.     a = (5.0 / 9.0) * (fahr - 32.0);
  21.  
  22.     b = printf("%d\n", a);
  23.  
  24.     return b;
  25. }
  26.  
So now, what I don't understand is, the data types. We declare the parameter fahr to be of the int data type. But now, what if I were to (say for instance, in the main function) put a variable as an argument for the celsius function, I don't understand how it works when say for instance, I were to declare this variable as another data type than what fahr is.

Also, I don't understand the return statement, what if I return a variable of type int, but the function is of another data type? hm.

Also, the data types are switched up in this program for a reason. :P
Mar 17 '08 #1
6 1475
gpraghuram
1,275 Expert 1GB
I am not getting your question fully.
With whatever i have understood i have given my explanation
1)I think you are asking what will happen if you passa float variable to a function accepting int or vice versa
==>Then the variable will be automatically converted.
If you pass 1.1 to a function which accepts int then it will be truncated and accepted as 1

Same is the case when returning values

If your question is something else then post again

Raghuram
Mar 17 '08 #2
Thanks for the reply friend.

Well, I don't understand functions fully. If I use the return statement to return a variable as type int to a function that is for instance type float, what happens? It is converted? Why do we even declare a function to be any data type? Do we declare them as a data type so that it knows how to store the value the return statement gives it?

Also, I don't understand parameters for functions. If I put a value for this function as an argument for this function, it will be stored in fahr as int correct? Well what happens when I put a variable as the argument? And of a different data type?
Mar 17 '08 #3
gpraghuram
1,275 Expert 1GB
Thanks for the reply friend.

Well, I don't understand functions fully. If I use the return statement to return a variable as type int to a function that is for instance type float, what happens? It is converted? Why do we even declare a function to be any data type? Do we declare them as a data type so that it knows how to store the value the return statement gives it?

Also, I don't understand parameters for functions. If I put a value for this function as an argument for this function, it will be stored in fahr as int correct? Well what happens when I put a variable as the argument? And of a different data type?

you are specifying data type so that the compiler can allocate that many number of bytes to it.
i cant get uyour second para fully

Raghuram
Mar 17 '08 #4
Well, what I was talking about in the second paragraph is...

For instance in this program, we have fahr (line 16) declared as int. But say for instance, in line 11, if that variable a was a variable of type float. What happens? I really don't understand entering variables as the argument.

I picture it like this, the variable a's value is now stored in fahr, and converted to the type of fahr (if its a different data type, in this program, fahr and the variable a are both of type int) and then that value is computed. etc. is that right?
Mar 17 '08 #5
gpraghuram
1,275 Expert 1GB
Well, what I was talking about in the second paragraph is...

For instance in this program, we have fahr (line 16) declared as int. But say for instance, in line 11, if that variable a was a variable of type float. What happens? I really don't understand entering variables as the argument.

I picture it like this, the variable a's value is now stored in fahr, and converted to the type of fahr (if its a different data type, in this program, fahr and the variable a are both of type int) and then that value is computed. etc. is that right?
Yes.
read my previous post for more info..

1)I think you are asking what will happen if you passa float variable to a function accepting int or vice versa
==>Then the variable will be automatically converted.
If you pass 1.1 to a function which accepts int then it will be truncated and accepted as 1

Same is the case when returning values



Raghuram
Mar 17 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
Data types in C and C++ are broken into two families: integer and floating point.

Integers are of different sizes (short, int, long, char, etc.) and floating point has different sizes (float, double, long double, etc.).

With integers, any integer can be converted to any other integer and it is your responsibility to be certain the result is OK. That is, converting a larger integer to a smaller one may cause a data loss.

With floating point, when you convert from a large one (double) to a smaller one (float), the number of signifcant figures chnages and you will get a warning about truncation an possible loss of data. This warning does not occur going th other way.

Converting an integer to floating point results in a floating point variable with a zero decimal portion. However, due to the number of significant digits in your integer, you could see rounding and loss of accuracy.

Converting a floating point to an integer always produces a warning about truncation and possible loss of data since integers have no decimal portion.

When you code, try to stick with one family and do not mix integers and floating point. And when you do use floating point, unless there is a reason that you can write down on paper, use double.

Finance and programs dealing with money should use integers. Keep the amounts in pennies. Using floating point for money (because of the nifty decimal point) will cause you trouble when the variables are rounded- and there is no way to turn that off.
Mar 17 '08 #7

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

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
7
by: Aguilar, James | last post by:
I've heard that virtual functions are relatively ineffecient, especially virtual functions that are small but get called very frequently. Could someone describe for me the process by which the...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? ...
8
by: York | last post by:
Hi, R language has very high-level IO functions, its read.table can read a total .csv file and recogonize the types of each column. write.table can do the reverse. R's MySQL interface has...
4
by: No One | last post by:
Here is my problem: I have a certain set of well-defined manipulations that I have to apply to different types of data. In all cases the manipulations are exactly the same, and are to be...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
5
by: Bern McCarty | last post by:
I have a DLL written in C++ (it's really C code that was adjusted to compile OK as C++) that I compile successfully into IL with the /CLR switch of Visual C 7.1. I use the resultant library...
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: 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...
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...
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...

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.