1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Rep Power

    const or variables

    Came upon constants and variables today in my book im learning from.

    Its confusing me a little. And was just wondering if someone could explain it to me.

    First, am I right that variables can be changed and constants can't be while the program is running?

    So if I'm using a variable I can ask the user for input as to what that variable is equal to, and I can't do that with a constant? The constant is always set to what I tell it to be?

    But what's the point of using constants? I can set a predefined value to my variable by typing:

    int myNumber = 5;

    wouldnt that make is so every time I typed myNumber in my code 5 would be there.

    I guess I dont understand why I would use a const instead of a variable..

    Also how would I make it so that my variable is to be defined by the user..

    Say I want int myNumber = (what the user of my program inputs)

    Thanks for any help,
    Last edited by Bohh; September 3rd, 2003 at 04:15 PM.
  2. #2
  3. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Rep Power
    Yes, your right, constant's can not be changed at runtime. Variables can.

    I think a good purpose for constants is so you don't bury 'magic numbers' in your code, like instead of having a number hardcoded in your program, you define a constant and use it, so if later on you want to change the #, you would just change it once on top and it gets changed in your own code.

    #define MAX 100
    int main()
         int LIST[MAX]; 
         int x; 
         for(x=0; x<MAX; x++)
         return 0; 
    Useless example, but if you want to change the MAX value from 100 to let's say, 500, you just change it once, instead of twice, in a real world program you might use MAX a lot more than twice.. so that's where it would really help.

    To accept input from the user, (This is in C):
    int x; 
    printf("Enter a number: "); 
    scanf("%d", &x); 
    printf("You entered %d\n", x);
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Woodland Hills, Los Angeles County, California, USA
    Rep Power
    There's a bit of a difference between using a #define and a const keyword. With #define, the C preprocessor replaces all instances of the #define string with the constant, then the compiler takes over. So, if you had code like this:
    #define FOO 100
    int array[FOO];
    for (i = 0; i < FOO; i++) ...
    array[FOO - 1] = 0;
    The preprocessor will search and replace all occurrences of FOO with 100 and then pass it on to the compiler. There is no actual variable called FOO for the compiler to deal with.

    When you use a const keyword, say like this:
    const int FOO = 100;
    There is an actual variable called FOO somewhere (compiler might choose to optimize it away though). More importantly, its type is declared as int. So, the compiler knows what the type of variable is ahead of time, which it cannot tell with the #define.

    To answer your second question, in C you can use scanf as recommended above. In C++, you can use scanf too, but you can also use cin
    #include <iostream>
    using namespace std;
    int main(void) {
        int intVar;
        cout << "Enter a value: ";
        cin >> intVar;
        cout << "You entered " << intVar << endl;
        return 0;
    Last edited by Scorpions4ever; September 3rd, 2003 at 05:18 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo