Thread: Specifiying Constants L, UL, LL, f

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    12

    Specifiying Constants L, UL, LL, f

    I have looked this up in three books and Google has also failed me.

    To me a constant is a value that doesn't not change. It defined by "const", "#define", or a convention of all capital letters to remind the programmer not to change the value.

    What I am seeing is:
    float radius = 0.0f;
    long Big_Number = 1287600L;
    #define PI 3.141f
    circumference = 2.0f*PI*radius;

    Is it keeping the variable type constant? I read that the type can be promoted during implicit conversions.

    IF I guessed correctly, when should it be used?

    If I guessed incorrectly, I am completely lost and don't know when to use it.


    Thank You,
    Carl

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The variable type is specified in the declaration, e.g., radius is a float because you declared it to be a float. This is true whether or not the variable is declared const.

    The suffix f is used to denote that the floating point literal (constant) is of type float.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    I don't know if I am over thinking this or if I believe something that is wrong. I think I don't understand was is meant by "constant." The constants I keep finding references to are string constants.

    Is this correct? I have to declare the "variable_name" type and the "value" type seperately.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well I don't think there is any harm in thinking the way you do. But to be precise, there are literal constants, which refer to anonymous values. Examples include 5, 'H'; other things that Sesame Street are brought by; and "Hello world!" Even though these things have no name, they have a type. In larger expressions, you might apply a suffix to make sure that the literal is the correct type so that no implicit conversions mess with the result of the calculation (or whatever). Then there are constant variables, which refer to, and qualify, things that you've defined in your program. They usually store literal constants like variables do.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    I believe I clearly understand now. It seemed redundant so I wanted to be sure I had the right train of thought. The "2.0f" in the expression circumference = 2.0f*PI*radius; makes sense now to.

    Thanks to both of you!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constants
    By GokhanK in forum C Programming
    Replies: 9
    Last Post: 02-15-2011, 02:01 AM
  2. COnstants
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 09-01-2007, 04:59 AM
  3. Constants
    By shuo in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2007, 11:51 PM
  4. where are the constants?
    By Raven Arkadon in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2005, 02:07 PM
  5. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM