Thread: Help fixing a compile time error

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    25

    Help fixing a compile time error

    I had to define a simple macro for class in the professor's code

    Heres the code -
    Code:
    #include <stdio.h> //this is to call printf() function to put output
    #include <time.h>  //this is to call clock() function that get the time
     
    
    //define a MACRO COMPUTE_AVERAGE here
     #define COMPUTE_AVERAGE(x, y) (((x)+(y))/2));
     
    //define a function compute_average here
     int computer_average(int num1,int num2);
     
     int computer_average(num1, num2)
     {
         int avg = 0;
         avg = ((num1 + num2)/2);
         
         return (avg);
     } 
     
    void main()
    {
       
       
       int i=1;
       int j=5;
       double ave=0.0;
     
       int num1, num2;
     
       int start_time, end_time;
       int time_task1, time_task2;
     
       //reading in two integers
       printf("Enter an integer:\n");
       scanf("%d", &num1);
       printf("Enter another integer:\n");
       scanf("%d", &num2);
     
       //print the results computed by MACRO and function
       printf("The average computed by your MACRO is: %10.2f\n", COMPUTE_AVERAGE(num1, num2) );
       printf("The average computed by your function is: %10.2f\n", compute_average(num1, num2));
     
       //get the starting time for the MACRO
       start_time = clock();
       printf("\nstart time1 %d\n", start_time);
     
       while (i < 10000000)
          ave = COMPUTE_AVERAGE(i++, j++); // macro call
     
       //get the ending time for the MACRO
       end_time = clock();
       printf("end time1 %d\n", end_time);
     
       time_task1 = end_time - start_time;
     
       printf("The time elapsed for the Macro is: %d\n\n", time_task1);
     
       i = 1;
       j = 5;
     
       //get the starting time for the function
       start_time = clock();
     
       printf("start time %d\n", start_time);
     
       while (i < 10000000)
          ave = compute_average(i++, j++); // regular function call
     
       //get the ending time for the function
       end_time = clock();
       printf("end time %d\n", end_time);
     
       time_task2 = end_time - start_time;
     
       printf("The time elapsed for the regular function is: %d\n\n", time_task2);
     
       return;
    }
    I get a compile time error on line 47 and 55 that reads " Syntax error before ')' token" I've tried multiple things to fix this but I have had no luck, any help would be appreciated.

    Line 47 is
    Code:
    printf("The average computed by your MACRO is: %10.2f\n", COMPUTE_AVERAGE(num1, num2) );
    and line 55 is
    Code:
    ave = COMPUTE_AVERAGE(i++, j++); // macro call

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Lots of little issues here, but following is what you asked about:

    Code:
    #define COMPUTE_AVERAGE(x, y) (((x)+(y))/2));
    Take the bracket and semi-colon away.

    As for the other things that I alluded to:

    1)
    Code:
    void main()
    
    ...
    
    return;
    Why bother returning, if you declare main as void? The truth is, main usually returns an int. You can read more about this subject here and here. In other words, try something like:
    Code:
    int main(void)
    
    ...
    
    return 0;
    2)
    Code:
    int computer_average(int num1,int num2);
     
    int computer_average(num1, num2)
    
    ...
    
    printf("The average computed by your function is: %10.2f\n", compute_average(num1, num2));
    You see that you declare one function, but call another. By the way, since your compute(r)_average function only returns an int, but the call to printf expects a double, you should be getting a compiler warning. Try to fix this mismatch. Changing your function to return a double would be one way of doing this. Have a look at the function macro too - it also returns an int, yet that printf statement where it is called also is looking for a double. Perhaps you could cast the return value from COMPUTE_AVERAGE to a double or something.

    3)
    Code:
    int computer_average(num1, num2)
    Just because your function prototype has the types for 'num1' & 'num2' does not mean that you can omit the types in the function definition.
    Last edited by kermit; 09-05-2009 at 09:36 PM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, main returns an int. Second, the usage of macros is error-prone. Consider this bit of code:

    Code:
    #define TWICE(N) ((N) + (N))
    
    int main( void )
    {
    	double 
    		x = 0, 
    		y = TWICE( ++x );
    	printf( "X (should be 1): %g\n", x );
    	printf( "Y (should be 2): %g\n", y );
    	return 0;
    }
    So the problem is that the macro expands to the expression ((++x) + (++x)). The general rule is to never pass expressions with side-effects to macros, but my opinion is that they should *never* be used in place of a function - only for defines and other special-purpose uses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM