I have this program and the variables part says
double CelFah (double);
I want to know why do I have to put the word double in parenthesis again , I've never seen this ?? How can I avoid that, is it necessary?
I have this program and the variables part says
double CelFah (double);
I want to know why do I have to put the word double in parenthesis again , I've never seen this ?? How can I avoid that, is it necessary?
It's just a function prototype. It is declaring the existence of a function named CelFah that takes one double and returns a double. You've probably come across it in an odd position such as inside a function definition where you usually see variable declarations. But function declarations are allowed to be there too. They are declared kind of like variables. If you had functions f and g that take an int and return a double and you also have x and y as double variables, you could declare them like this:
Declarations are best moved to headers for real projects and are best gathered at the top of the program (under the includes and possibly some type declarations) for most programs.Code:double x, f(int), g(int), y;
Code:#include <stdio.h> int main() { double CelFah(double), cel; // should just declare CelFah above main printf("Celsius: "); while (scanf("%lf", &cel) == 1) { printf("Fahr : %10.2f\n", CelFah(cel)); printf("Celsius: "); } return 0; } double CelFah(double cel) { return cel * 9.0 / 5.0 + 32.0; }
The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell
Further, there is no benefit at all to removing variable names from function prototypes.
double CelFah(double cel)
is better than
double CelFah(double)
double CelciusToFahrenheit(double celciusTemperature) would be better still.
It tells you a hell of a lot more than double CelFah(double) ever could.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Thank you so much for the help both of you!! Really appreciate it !
Hey sorry to bother but can you help me understand something. So I have this code the same about converting temperatures .
printf("%8.2f\t %-10.2f\n", number1,CelToFah(number1),CelToKel(number1));
-Question :Where exactly are the 8.2 and -10.2 coming from?
now another one
scanf_s("%12lf %12lf %12lf", &number1,number2,&number3);
-question:Where is the %12lf coming from?
I am sorry this is the first time in a programming class!
search for "man printf"
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Although applicable to C++, it looks like these questions are really intended to be concerning C programming, so I have moved this topic to the C programming forum.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Are they random numbers ?
I think they are what tell you how many decimals points will show right?
You need to RTM on printf and scanf because they're somewhat complicated.
In your specific examples:
In the printf, the 8 and 10 are field widths. The field in which the number is printed will be padded (with spaces or zeroes) to make it this wide. If there is no negative sign then it's padded on the left (right-justified); otherwise it's padded on the right (left-justified). A leading 0 causes the padding character to be 0 instead of space (not allowed for right padding! ).
The .2 (for the f conversion specifier) gives the number of digits to print after the decimal point.
In the scanf, the 12's are again field widths, but in this case they give the maximum number of characters to read when scanning in the number (after skipping leading spaces). So if there is a 14 digit number only the first 12 would be scanned into the corresponding variable and the next 2 digits would go to the next variable. If the numbers being read are separated by spaces then the 12s are probably not needed.
The lf (lowercase L and f) means "long float", i.e., double. In scanf it's necessary to use f for float and lf for double. In printf they end up being equivalent since float values are passed as doubles (to variadic functions, i.e., ones that have ... in their argument list like printf) so f is interpretted as lf (and therefore people usually just say f with printf and doubles ... but beware scanf!)
The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell