#define CONSTANT - how to change it's value?

This is a discussion on #define CONSTANT - how to change it's value? within the C Programming forums, part of the General Programming Boards category; Hi, what does this error mean and how to fix it? '=' : left operand must be l-value Code: #include ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    #define CONSTANT - how to change it's value?

    Hi,

    what does this error mean and how to fix it?
    '=' : left operand must be l-value

    Code:
    #include <stdio.h>
    #define TAX 0.10
    
    void main() {
    	float bal, t;
    	bal = (float)72.10;
    	TAX = 0.20;
    	t = bal * (float)TAX;
    	printf("The tax on %.2f is %.2f\n", bal, t);
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You can't change a #define like that. If you want to change the value throughout your program just make it a variable ( local or global ) and then change it. When you have a #define the whole idea is that it is constant for the life of the program. You cannot modify that value.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Thanks for explanation. Yes, it is a constant and illegal to change it.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ahh, and about your code, don't use void main(), the main() function only returns integer.

    Another way to declare constants is:
    Code:
    const int blah;
    const type name;

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Another way to declare constants is:
    Not in C. Using the const keyword on a variable merely makes it read-only where if you innocently try to modify the value, you get an error. A true constant can be used as the size for an array:
    Code:
    #include <stddef.h>
    
    #define CONSTANT 10
    const size_t READONLY = 10;
    
    int main ( void )
    {
      int array1[CONSTANT]; /* Legal */
      int array2[READONLY]; /* Bzzt! Error */
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 03:07 PM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  5. Error check problem again
    By rtransfi in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 03:55 PM

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