Thread: Question about c for engineers

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    18

    Question about c for engineers

    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?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    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:
    Code:
    double x, f(int), g(int), y;
    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:
    #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;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Registered User
    Join Date
    Mar 2018
    Posts
    18
    Thank you so much for the help both of you!! Really appreciate it !

  5. #5
    Registered User
    Join Date
    Mar 2018
    Posts
    18
    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!

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2018
    Posts
    18
    Are they random numbers ?

  9. #9
    Registered User
    Join Date
    Mar 2018
    Posts
    18
    I think they are what tell you how many decimals points will show right?

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    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!)
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework c for engineers
    By hellomiami in forum C++ Programming
    Replies: 10
    Last Post: 03-02-2018, 09:33 PM
  2. Question with professional Software Engineers
    By nick753 in forum General Discussions
    Replies: 23
    Last Post: 02-19-2010, 11:49 PM
  3. Question for experienced sofware engineers
    By RATyson in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2007, 12:39 PM
  4. Are programmers engineers?
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 04-01-2003, 01:55 AM

Tags for this Thread