C: Input, Variables, and Data Types - Data Type Modifiers
(Page 4 of 4 )
In addition to Data Types, there are also data type modifiers, which are used to...you guessed it genius, modify data types.
The Short and Long of It
Short and Long are used to modify the amount of memory a data type uses. It can either be less (short) or more (long). You use data modifiers by preceding the data type with the short or long keyword with the exception of an int, which does not need to follow the keywords. A short typically takes up 2 bytes of memory (less than an integer) and is often used for values ranging from -32768 to 32767.
Longs on the other hand are either the same size as an integer (4 bytes) or larger (8 bytes). Note that you can also use the unsigned specifier in addition to your data modifier to ensure that the values are positive and never negative. Again, you do not need to write int after this specifier. Here are some examples:
unsigned short myage;
short myage;
long yourage;
short int myage;
There are other data modifiers and specifiers, but these will do for now.
Scope
While this won't really make sense until future articles, it is important to note that variables have a lifetime (just like your goldfish you flushed down the toilet last night). There are two types of lifetimes for variables:
Local Variables
Local variables are declared at the beginning of the block they are going to be used in and can only be used in that block of code. They essentially die after that block, becoming useless to the rest of the program.
Global Variables
Global Variables are declared outside of any block and can be used from anywhere in your program, essentially never dying. Some people consider them bad programming practice, as (for example) they are harder to track down should a bug occur, but they definitely serve their purpose.
Well that is it for this article. We will cover more of the data types and modifiers as time progresses. In the next article however, we will discuss Operators and Assignments, so stick around. Or well...come back often, as you may have to wait a few days and I don't want you just sitting there, messing up my web page.
Till then...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |