Square 1: really basic C++ question

Discussion in 'Software' started by Brookston, May 18, 2004.

  1. Brookston

    Brookston Private E-2

    Would you mind terribly answering a couple of really, really basic C++ Language questions?

    I am a beginning C++ programming student. In order to get off on the right foot, I'd like to really understand whether ...and why...or why not the digit and decimal here in parentheses ( 0. ) can be considered a valid constant. I was under the distinct impression that decimal constants should not begin with zero. I thought that the compiler would automatically interpret a zero as an octal number. I tried writing a test program to check output, and when I added an octal number to the above digit and decimal ( 0.), I seemed to obtain an octal result, but when I added a decimal number to it, I seemed to obtain a decimal result. So does this make it a valid constant?

    Also...would the following be a valid constant? It seems to me that it qualifies as an expression, not a constant: 14.0F-3

    Sorry if this is painfully basic (yes, I realize it's C++ and not basic at all:)
     
  2. Maxwell

    Maxwell Folgers

    Only an integer with a leading zero is recognised as an octal constant. If you have a decimal point then you have a floatin gpoint number.

    Not sure about 14.0F-3 but 14.0E-3 is also floating point numbers both representing 0.014, i.e., using exponential notation for floating numbers.

    Further information here: http://www.ssec.wisc.edu/~dglo/c_class/constants.html
     
  3. Brookston

    Brookston Private E-2

    Thanks for responding. That's helpful. About the other...I understand about scientific notation using the letter "e." That's what leads me to believe that something like 14.0F-3 (fourteen point zero followed by the letter "F" followed by a minus three) is really a floating point (it would be, even without the "F") expression or calculation rather than being a constant. It was a trick exercise question to aid learning, I think... My evaluation is reasonable, don't you think?
     
  4. Maxwell

    Maxwell Folgers

    14.0F-3 is an expression and is 14.0 minus 3. The F indicating that the value preceding it is a floating point number. However, you would need to verify that the compiler recognises this compliance to ANSI C. In good coding practice where there is potentially an ambiguity like this to introduce code constructs to avoid such an expression, e.g., the introduction of brackets, etc..
     
  5. Brookston

    Brookston Private E-2

    Thanks, Maxell. I really appreciate your help. I have a greater sense of security going on from here. Now that you mention it, when I was writing my little test program, I think I did have to put that in parentheses before it would compile and run. Again, thank you for your help.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds