Help with variables and constants.

This is a discussion on Help with variables and constants. within the C Programming forums, part of the General Programming Boards category; Hello Board, I hope my questions isn't too silly. But I'm reading a book learn C in 21 days, it's ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Help with variables and constants.

    Hello Board,

    I hope my questions isn't too silly. But I'm reading a book learn C in 21 days, it's a bit dated but a friend recommended it. I'm on day three where it focuses on variables. Although a part of it has got me very confused.

    At one point, after describing the types of variables, it goes right in to initializing a variable right on the same line for instance:

    int count = 0;

    or

    double percent = 0.01;

    Easy enough I thought. Then I read on and it starts on constants, and specifically literal constants. It then says a literal constant can be expressed as:

    int count = 20;

    or

    float tax_rate = 0.28;

    The book goes on to explain that a constant is a value that cannot be changed during program execution.

    But my question is this, what is the difference between those two examples? Defining a variable and defining a literal constant look EXACTLY the same!!! How does the compiler know the difference? How does the programmer know the difference!?!

    The only tiny glimmer of difference I seem to be able to segment the two with is the fact that a variable would be defined outside the main() function, and a literal constant be defined inside any function. Whats stopping me from re-assigning a value to say tax_rate once I set the value?!

    Whats the deal?

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This
    Code:
    int count = 20;
    is a variable. This:
    Code:
    const int count = 20;
    is a constant.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    Quote Originally Posted by tabstop View Post
    This
    Code:
    int count = 20;
    is a variable. This:
    Code:
    const int count = 20;
    is a constant.
    Thanks tabstop, the book must be wrong, or the editor was hitting the bottle because there is no const in front of any of their literal constant examples.

    This puts me at ease.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More questions about constants
    By R.Stiltskin in forum C++ Programming
    Replies: 31
    Last Post: 01-03-2008, 02:05 PM
  2. Initalizing global variables to constants
    By maxhavoc in forum C Programming
    Replies: 6
    Last Post: 06-21-2006, 11:25 AM
  3. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 08:01 AM
  4. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 07:29 PM
  5. string variables? string constants?
    By andrew22289 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2002, 12:18 AM

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