expected ) before ; token

This is a discussion on expected ) before ; token within the C Programming forums, part of the General Programming Boards category; i'm trying to get the program to run (M/m)^(1/5) and its telling me % gcc lab4.c lab4.c: In function ‚speeds_ratio‚: ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    expected ) before ; token

    i'm trying to get the program to run (M/m)^(1/5) and its telling me
    % gcc lab4.c
    lab4.c: In function ‚speeds_ratio‚:
    lab4.c:34:20: error: expected ‚)‚ before ‚;‚ token

    where in my code did I make a wrong turn? help please!!!


    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define one 1.0;
    #define five 5.0;
    
    
    
    double speeds_ratio(double M,double m);
    
    int main(void)
    {
    
    double Max;
    double min;
    
    
            printf("Input the max rpm.\n");
    scanf("%lf",&Max);
            printf("Input the min rpm.\n");
    scanf("%lf",&min);
    
    printf("The ratio between successive speeds of a six-speed gearbox with maximum speed %f rpm,and minimum speed %.1f rpm, is. %1f",Max,min,speeds_ratio(Max,min));
    
    return(0);
    }
    
    double 
    speeds_ratio(double M, double m)
    {
    
    double (speed);
    
            (speed) = pow(M/m,one/five);
    
     return(speed);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,306
    #define statements in C are nothing more than fancy text substitution. If you have a ; in your #define, it gets put in the text. So when the preprocessor runs, and substitutes one and five, you get:
    Code:
    (speed) = pow(M/m/1.0;/5.0;);
    See the extra semicolon? Drop the ; from the definition of one and five.

    Also, you're a bit parentheses-happy. Using too many just makes your code difficult to read. Only use them when needed to force the order of operation, or when it clarifies your intention or improves readability. I would do the following:
    Code:
    double
    speeds_ratio(double M, double m)
    {
        double speed;  // not needed on declaration
    
    
        speed = pow(M/m,one/five);  // not needed on left side of =.
    
    
        return speed;  // not needed for return, it's a keyword, not a function
    }

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,306
    Oh, and since you're using GCC to compile your code, you can use the -E option to see the result of your preprocessed code. You may get a lot of "junk" at the beginning that doesn't interest you, but:
    Code:
    $ gcc -E foo.c
    <snipped for brevity>
    double speeds_ratio(double M,double m);
    
    
    int main(void)
    {
    
    
        double Max;
        double min;
    
    
    
    
        printf("Input the max rpm.\n");
        scanf("%lf",&Max);
        printf("Input the min rpm.\n");
        scanf("%lf",&min);
    
    
        printf("The ratio between successive speeds of a six-speed gearbox with maximum speed %f rpm,and minimum speed %.1f rpm, is. %1f",Max,min,speeds_ratio(Max,min));
    
    
        return(0);
    }
    
    
        double
    speeds_ratio(double M, double m)
    {
    
    
        double (speed);
    
    
        (speed) = pow(M/m,1.0;/5.0;);
    
    
        return(speed);
    }
    See, there are those unnecessary semicolons in red.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected expression before ')' token
    By m_programmer in forum C Programming
    Replies: 6
    Last Post: 09-14-2012, 08:02 AM
  2. error: expected Ď)í before Ď*í token
    By evb@studioe.com in forum C Programming
    Replies: 4
    Last Post: 01-10-2012, 01:13 PM
  3. expected primary-expression before xxx token
    By lehe in forum C++ Programming
    Replies: 5
    Last Post: 07-06-2009, 02:54 PM
  4. Problem: expected `,' or `...' before '&' token
    By bigmac(rexdale) in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2008, 11:03 AM
  5. error: expected Ď;í before Ď:í token
    By kris.c in forum C Programming
    Replies: 5
    Last Post: 02-10-2008, 09:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21