Results 1 to 3 of 3

Thread: Why does this C prog not compile? (Learning C programming, newb stuff)

  1. #1
    Join Date
    Jun 2005
    Location
    Burlington, NC
    Beans
    394
    Distro
    Ubuntu 10.04 Lucid Lynx

    Why does this C prog not compile? (Learning C programming, newb stuff)

    I bought a book to learn C programming on Linux and am starting to work my way through it. I'm on the lesson for constants, and have run into an issue where the code in the book doesn't compile, and I'd like to know why. It makes sense to me, but that's not saying much. Anyway, here's the code:
    Code:
    /* Demonstrates variables and constants */
    #include <stdio.h>
    
    /* Define a constant to convert from pounds to grams */
    #define GRAMS_PER_POUND = 454
    
    /* Define a constant for the start of the next century */
    const int TARGET_YEAR = 2010;
    
    /* Declare the needed variables */
    int weight_in_grams,weight_in_pounds;
    int year_of_birth,age_in_2010;
    
    int main(void)
    {
            /* Input data from the user */
    
            printf("Enter your weight in pounds: ");
            scanf("%d", &weight_in_pounds);
            printf("Enter your year of birth: ");
            scanf("%d", &year_of_birth);
    
            /* Perform the conversions */
    
            weight_in_grams = weight_in_pounds * GRAMS_PER_POUND;
            age_in_2010 = TARGET_YEAR - year_of_birth;
    
            /* Display results on the screen */
    
            printf("\nYour weight in grams = %d.",weight_in_grams);
            printf("\nIn 2010 you will be %d years old.\n",age_in_2010);
    
            return 0;
    }
    And here's the error I get when compiling:
    listing2.4.c: In function ‘main’:
    listing2.4.c:25: error: expected expression before ‘=’ token
    (that's this line)
    Code:
    weight_in_grams = weight_in_pounds * GRAMS_PER_POUND;
    I've tried encapsulating the equation portion in ()'s, and changing the order of the equation. The only way I could get it to compile was by changing the constant name GRAMS_PER_POUND in the equation portion to the actual number. But this would make defining constants useless.... I'm assuming that it's just a change that has happened since the book was printed (2000), but it still seems to me that it should work.
    Can anyone offer a suggestion as to why it's not working?
    Thanks.

  2. #2
    Join Date
    Jun 2005
    Location
    Berkeley, CA
    Beans
    Hidden!
    Distro
    Hardy Heron (Ubuntu Development)

    Re: Why does this C prog not compile? (Learning C programming, newb stuff)

    #define GRAMS_PER_POUND = 454
    no = required here. actually you should not put an =, just do a
    #define GRAMS_PER_POUND 454

    AM
    AM

  3. #3
    Join Date
    Jun 2005
    Location
    Burlington, NC
    Beans
    394
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Why does this C prog not compile? (Learning C programming, newb stuff)

    Quote Originally Posted by amohanty View Post
    no = required here. actually you should not put an =, just do a
    #define GRAMS_PER_POUND 454

    AM
    It's always the little mistakes.... Like that . in bind configs....
    Thanks for the quick reply!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •