HomePractices Page 13 - Basic Data Types and Calculations
Functional Notation for Initial Values - Practices
This article looks at some of the basic data types that are built into C++. If you're learning how to use C++, you will want to keep reading, since you'll be using these data types in all of your programs. It is taken from chapter two of the book Beginning ANSI C++: The Complete Language, by Ivor Horton (Apress, 2004; ISBN: 1590592271).
An alternative notation for specifying the initial value for a variable when you declare it is called functional notation. The term stems from the fact that you put the initial value between parentheses after the variable name, so it looks like a function call, as you’ll discover later on.
An alternative notation for specifying the initial value for a variable when you declare it is called . The term stems from the fact that you put the initial value between parentheses after the variable name, so it looks like a function call, as you’ll discover later on.
Let’s look at an example. Instead of writing a declaration as
int unlucky = 13;
you have the option to write the statement as
int unlucky(13);
Both statements achieve exactly the same result: they declare the variableunluckyas typeintand give it an initial value of 13.
You can initialize other types of variables using functional notation. For instance, you could declare and initialize a variable to store a character with this statement:
char letter('A');
However, functional notation for initializing variables is primarily used for the initialization of variables of a data type that you’ve defined. In this case, it really does involve calling a function. The initialization of variables of the fundamental types in C++ normally uses the approach you have taken up to now. You’ll have to wait until Chapter 11 to find out about creating your own types and how those kinds of variables get initialized!
Summary
In this chapter, I covered the basics of computation in C++. You learned about most of the fundamental types of data that are provided for in the language. The essentials of what I’ve discussed up to now are as follows:
Numeric and character constants are called literals.
You can define integer literals as decimal, hexadecimal, or octal values.
A floating-point literal must contain a decimal point, or an exponent, or both.
Named objects in C++, such as variables, can have names that consist of a sequence of letters and digits, the first of which is a letter, and where an underscore is considered to be a letter. Upper- and lowercase letters are distinguished.
Names that begin with an underscore followed by a capital letter, and names that contain two successive underscores, are reserved for use within the standard library, so you shouldn’t use them for names of your own variables.
All literals and variables in C++ are of a given type.
The basic types that can store integers areshort,int, andlong. These store signed integers by default, but you can also use the type modifierunsignedpreceding any of these type names to produce a type that occupies the same number of bytes but only stores unsigned integers.
A variable of typecharcan store a single character and occupies 1 byte. The typecharmay besignedorunsignedby default, depending on your compiler. You can also use variables of the typessigned charandunsigned charto store integers.
The typewchar_tcan store a wide character and occupies either 2 or 4 bytes, depending on your compiler.
The floating-point data types arefloat,double, andlong double.
The name and type of a variable appear in a declaration statement ending with a semicolon. A declaration for a variable that results in memory being allocated is also a definition of the variable.
Variables may be given initial values when they’re declared, and it’s good programming practice to do so.
You can protect the value of a “variable” of a basic type by using the modifierconst. The compiler will check for any attempts within the program source file to modify a variable declared asconst.
An lvalue is an object or expression that can appear on the left side of an assignment. Non-constvariables are examples of lvalues.
Although I discussed quite a few basic types in this chapter, don’t be misled into thinking that’s all there are. There are some other basic types, as well as more complex types based on the basic set, as you’ll see, and eventually you’ll be creating original types of your own.