Thread: Beginner practicing

  1. #1
    Registered User
    Join Date
    Apr 2022
    Posts
    4

    Beginner practicing

    This is an example of an error that I get on most code that I just don't understand.

    Here's the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    float convert (float f);
    
    
    int main()
    {
        float temp_f;
        float temp_c;
    
    
        printf("What is the temperature in fahrenheit: ");
        scanf("%.2f", &float temp_f);
    
    
        printf("The temperature in fahrenheit is %.2f", temp_f);
        return 0;
    }
    The error is: error: expected expression before 'float'.

    As mentioned above I get this alot and originally I didn't have the & in the scanf function, which I thought was the problem, but it gave me the same error even after adding it. Any ideas? (I am using Code::Blocks ver. 20.03)

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    You just need to remove the word 'float' from the scanf call.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2022
    Posts
    4
    Quote Originally Posted by john.c View Post
    You just need to remove the word 'float' from the scanf call.
    Thank you for the reply. I'm just curious as to why the compiler states "expected expression before float" because that to me says there's nothing wrong with having float itself there, but you're just missing something before the word float? Is that just something you have to get used to in working with these compilers?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Yes. Compiler error messages can be a little weird.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    It could be that you are trying to take the address of a key word and the compiler is looking for an expression that it can take the address of.

    Address-of operator: & | Microsoft Docs

    From the above link:

    Syntax

    address-of-expression:
    & cast-expression
    Last edited by G4143; 04-20-2022 at 07:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. practicing memory management
    By deathslice in forum C Programming
    Replies: 4
    Last Post: 02-26-2016, 08:29 PM
  2. Replies: 18
    Last Post: 08-04-2011, 09:47 PM
  3. Google practicing Voodoo in Europe
    By MK27 in forum General Discussions
    Replies: 18
    Last Post: 07-14-2010, 11:09 PM
  4. Still Practicing, Too Much To Learn
    By dolfaniss in forum C Programming
    Replies: 6
    Last Post: 05-21-2010, 01:27 PM
  5. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM

Tags for this Thread