Thread: error: syntax error before printf

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    7

    Question error: syntax error before printf

    Hi. I have just learnt C programming and I don;t know what's wrong with the printf("Your BMI is %0.2f.\n", BMI);below. Is there anyone can check it for me please?



    Code:
    /* Demonstates variables and constants*/
    #include <stdio.h>
    
    
    /*Declare the need variable*/
    float BMI;
    int height_in_meters, weight_in_killograms;
    
    
    int main()
    {
    
    
      /* Input data from user*/
    
    
      printf("How tall are you in meters:");
      scanf("d%", &height_in_meters);
      printf("Please enter your weight in killograms:");
      scanf("d%", &weight_in_killograms);
      
      /* Perform conversions*/
      
      BMI = (weight_in_killograms)/(height_in_meters)^2
      
      /* Display result on the screen*/
      
      printf("Your BMI is %0.2f.\n", BMI); 
      
      return 0;
      
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    There is nothing wrong with that line. However, there are several other problems in the code that your compiler should be warning you about. If you haven't already, you should compile with maximum warnings (see your compiler documentation for how to do this).

    This is what I got when I compiled your code:

    Code:
    /*
    main.c||In function 'main':|
    main.c|17|warning: spurious trailing '%' in format|
    main.c|17|warning: too many arguments for format|
    main.c|19|warning: spurious trailing '%' in format|
    main.c|19|warning: too many arguments for format|
    main.c|27|error: expected ';' before 'printf'|
    ||=== Build finished: 1 errors, 4 warnings ===|
    */
    See if you can use these warnings to figure out where your problems are.

    Also note that there is no exponent operator in 'C' - so '^' does not do what you think it does (it is the bitwise XOR operator). For smaller powers, you'd be better off just doing repeated multiplication.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also deviding integers will result in integer part only... And how do you plan to store people height in meters as integer?

    In your world only leave people of 1 and 2 meter height?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jul 2014
    Posts
    7
    I have already correct some of the mistakes here, but I don't know why it still doesn't work.

    Code:
    /* Demonstates variables and constants*/
    #include <stdio.h>
    
    
    /*Declare the need variable*/
    float height_in_meters, weight_in_killograms, BMI;
    
    
    int main()
    {
    
    
      /* Input data from user*/
    
    
      printf("How tall are you in meters:");
      scanf("d%", &height_in_meters);
      printf("Please enter your weight in killograms:");
      scanf("d%", &weight_in_killograms);
      
      /* Perform conversions*/
      
      BMI = height_in_meters*height_in_meters/weight_in_killograms;
      
      /* Display result on the screen*/
      
      printf("Your BMI is %0.2f.\n", BMI); 
      
      return 0;
      
    }
    When I try to run the program, there are some of the mistakes like below:

    How tall are you in meters:6.1
    Please enter your weight in killograms:Your BMI is -1.#J.
    Press any key to continue . . .

  5. #5
    Registered User
    Join Date
    Jul 2014
    Posts
    7
    All right. I am sorry. I have already corrected the mistake here. Thanks for your help.

    [code]
    /* Demonstates variables and constants*/
    #include <stdio.h>


    /*Declare the need variable*/
    float height_in_meters, weight_in_killograms, BMI;


    int main()
    {


    /* Input data from user*/


    printf("How tall are you in meters:");
    scanf("%f", &height_in_meters);
    printf("Please enter your weight in killograms:");
    scanf("%f", &weight_in_killograms);

    /* Perform conversions*/

    BMI = weight_in_killograms/(height_in_meters*height_in_meters);

    /* Display result on the screen*/

    printf("Your BMI is %0.2f.\n", BMI);

    return 0;
    [\code]

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should also stop using global variables, this is very bad practice and since everything is in main() you don't even need the global.

    Jim

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Are you dyslectic?

    "%d" and "d%" are NOT the same in C.

    Tim S.
    "...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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax error befor printf.
    By alienxx in forum C Programming
    Replies: 3
    Last Post: 11-20-2012, 07:13 AM
  2. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. error C2061: syntax error : identifier
    By maninboots in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2009, 05:40 AM
  5. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM

Tags for this Thread