Thread: Constants

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    113

    Constants

    I am trying to fully understand details about constants.

    1. What is a multibyte character and what is a wide character?

    What is the difference between 'A' and L'A'

    2. The book I have says:

    By default, the compiler fits a numeric constant into the smallest compatible data type that will hold it. Therefore, assuming 16-bit integers, 10 is int by default, but 103,000 is a long int.

    What should I understand from that. When does the compiler fit the numeric constant, won't I always define the data type?

    3. When should I use suffixes F,L and U?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by GokhanK View Post
    I am trying to fully understand details about constants.

    1. What is a multibyte character and what is a wide character?
    Multibyte characters are usually a single byte... ASCII with extensions for accent marks.
    Wide characters, also called Unicode are usually 2 bytes, allowing up to 65.535 different characters.

    What is the difference between 'A' and L'A'
    'A' is a single byte character.
    L'A' is a unicode character.

    (The same appies to strings, btw)

    2. The book I have says:
    By default, the compiler fits a numeric constant into the smallest compatible data type that will hold it. Therefore, assuming 16-bit integers, 10 is int by default, but 103,000 is a long int.

    What should I understand from that. When does the compiler fit the numeric constant, won't I always define the data type?
    Basically you should always define types, most compilers will error off if you don't.
    What you should take from this is that smaller numbers fit into larger variables, but not the other way around.

    Code:
     int x;
     char y;
    
     y = 100;
     x = y;       // no problem
    
     x = 1000;
     y =  x;      // will cause an overflow.

    3. When should I use suffixes F,L and U?
    [/quote]

    I use them as very convenient typecasts for literal numbers double x = 1000f; etc.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Code:
    float x;
    x=123.12
    Code:
    float x;
    x=123.12f
    What is the difference between them? I have defined x as a float, then why should I specify it as a float again?

    And can you give me an example of a wide character?

    Edit: I saw later

    Code:
    double x;
    x=123.12f
    Does it mean that:

    At this step store it as a float but x can overflow float later (or could overflow float before)
    Last edited by GokhanK; 02-13-2011 at 07:08 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by GokhanK View Post
    Code:
    float x;
    x=123.12
    Code:
    float x;
    x=123.12f
    What is the difference between them? I have defined x as a float, then why should I specify it as a float again?
    In your example, none... anything with decimal points *has* to be a float or a double since integers are whole numbers only. But consider...
    Code:
    float x;
    int y;
    y = 100;
    x = y+ 25f;
    And can you give me an example of a wide character?
    Whew... easier asked than answered... you need to open some unicode files in a hex editor and see what's going on... if it's english language, every other byte will be a 0.

    Edit: I saw later

    Code:
    double x;
    x=123.12f
    Does it mean that:

    At this step store it as a float but x can overflow float later (or could overflow float before)
    No it doesn't. Overflows are BAD, we spend an inordinate amount of time avoiding them in C.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Code:
    #include <stdio.h>
    
    main()
    {
    float x;
    int y;
    y = 100;
    x = y+ 25f;
    return 0;
    }
    It doesn't work in CodeBlocks

    It says: Invalid suffix f on integer constant

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by GokhanK View Post
    Code:
    #include <stdio.h>
    
    main()
    {
    float x;
    int y;
    y = 100;
    x = y+ 25f;
    return 0;
    }
    It doesn't work in CodeBlocks

    It says: Invalid suffix f on integer constant
    It was only an example... to demonstrate the concept.
    Cut and paste coding is just as BAD as overflows.

    Also... the correct form of main is int main (void) or int main (int argc, char *argv[])... and it always returns an integer value, usually 0.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Quote Originally Posted by CommonTater View Post
    It was only an example... to demonstrate the concept.
    Cut and paste coding is just as BAD as overflows.

    Also... the correct form of main is int main (void) or int main (int argc, char *argv[])... and it always returns an integer value, usually 0.
    I still wonder the difference between;

    Code:
    float a;
    a=100
    and

    Code:
    float a;
    a=100f
    Do I need to place f to specify the variable a is a float, not integer. Isn't it enough to define it as float? If I don't put f will it store it differently from a float?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by GokhanK
    Do I need to place f to specify the variable a is a float, not integer. Isn't it enough to define it as float? If I don't put f will it store it differently from a float?
    The f suffix is used to denote that the constant (as in literal) is a float, e.g., 100.0 is a double constant whereas 100.0f is a float constant. It does not denote that the variable is a 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

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Code:
    #include <stdio.h>
    
    main()
    {
    float x;
    int y;
    y = 100;
    x = y+ 25.0f;
    return 0;
    }
    If I write it like this it works. I think f cannot be used as 25f or 100f, they have to be like 25.0f or 100.00f.

    Then I don't know why to use these suffixes still.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    #define MY_FLOATING_CONST      (1.0f)
    If you just use 1.0 the type will be double, sure you can use cast ((float)1.0) also.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to store a global constants file?
    By dxfoo in forum C++ Programming
    Replies: 9
    Last Post: 04-11-2010, 09:12 AM
  2. LNK2001 and constants
    By g4j31a5 in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2010, 09:23 PM
  3. Replies: 7
    Last Post: 11-12-2008, 11:00 AM
  4. Constants
    By shuo in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2007, 11:51 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