Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Ivor Horton's Beginning Visual C++ 2010
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2010 by Ivor Horton; ISBN: 9780470500880
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2010 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 6th, 2012, 07:29 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default long and unsigned

I don't get the significance of the letter "L" at the end of the value for a long as in
Code:
long BigNumber = 1000000L
. If it's declared as a long then the value you put in will be a long, won't it? Leaving the "L" off doesn't seem to stop programs compiling. Also the compiler doesn't produce outputs with the letter "L" appended.

Same thing with "U" for unsigned.

Bob
  #2 (permalink)  
Old December 1st, 2012, 05:00 AM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Posts: 66
Thanks: 0
Thanked 4 Times in 4 Posts
Default

britbeef,

I offer my apology. It may be a little late for this reply to help you because of the time lag between your question and my reply. Since I work and go to school, I tend to put my independent study of C++ on the back burner when I am not between semesters.

Your question made me put my thinking cap on because the answer deals with both fundamental concepts and their application. I will address long only. The same principle applies to unsigned.

To begin with, C++ is a strongly typed language, and you don't want the compiler to assume what you mean when you type code. Long is a keyword which performs two functions. The type of data which is acceptable for the variable is defined by the keyword long. The keyword also allocates memory to store the variable's value. This determines the range of values which will fit in the variable. Too large a number will not fit because it is out of the range. The suffix L tells the compiler the same thing as the keyword long.

Neglecting a discussion of variable names and their values would fail to explain the significance of the seemingly insignificant redundancy of using both long and L. A variable's name is a container of sorts. It's value is the contents of the container. The equal (=) sign in math says that both sides are equal in value. It is normally read from left to right. In C++ this is not the case. In your example, long BigNumber = 1000000L, is a right to left operation. The value (1000000L) is placed into the variable (BigNumber). This concept is essential to the understanding of the C++ language and many other programming languages.

There are times when a value is transferred to another variable or is included in a computation. Some operations like bitwise shift, type conversions in assignments, and defining synonyms for data types must be in a form which the compiler recognizes by type.

To simplify the explanation, let's say you have some code like this:
Code:
long BigNumber = 1000000L

lots of code

answer = BigNumber
What happens when the last line is executed? The value of BigNumber (1000000L) is placed in answer. The suffix L tells the compiler that the value is a long integer. (Also read the section in the book where calculations of mixed types (adding a float to an int, for example) are discussed.

If you acquire the habit of using the seemingly redundant syntax, you will be much less likely to encounter logical errors (where the program compiles and runs, but produces the wrong results) in your programs.

regards,
drpepper
  #3 (permalink)  
Old May 31st, 2013, 06:00 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Looking back through this forum I see that I failed to acknowledge your comprehensive and very helpful reply to my query last year. Please accept my thanks for your help and my apologies for my tardiness.

regards

Bob
  #4 (permalink)  
Old June 1st, 2013, 12:58 AM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Posts: 66
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Bob,

apology accepted ... I really appreciate your diligence in offering thanks now. ...You are quite welcome!

regards,
drpepper
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unsigned int is non-CLS compliant. anilvhsh BOOK: Professional C#, 2nd and 3rd Editions 1 October 7th, 2010 04:39 AM
Long Long int to bin walid C# 0 January 23rd, 2007 12:47 PM
Unsigned integers AndrewH Java Basics 1 September 26th, 2006 02:46 PM
string to "long long" without using atoll sarraju C++ Programming 2 August 4th, 2004 07:19 AM
Modifying long text in a long field sajsal Classic ASP Databases 1 February 20th, 2004 12:36 PM



All times are GMT -4. The time now is 10:22 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.