0% found this document useful (0 votes)
4 views7 pages

Data Types

Datw types

Uploaded by

Mekoya Demissie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Data Types

Datw types

Uploaded by

Mekoya Demissie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C++ reserved words.

C++ reserves these words for specific purposes in program construction. None of the
words in this list may be used as an identifier; thus, you may not use any of these words to name a
variable.

Data types

Integer Types

C++ supports several other integer types. The type short int, which may be written as just
short,
Represents integers that may occupy fewer bytes of memory than the int type. If the short
type occupies
less memory, it necessarily must represent a smaller range of integer values than the int
type. The C++
standard does not require the short type to be smaller than the int type; in fact, they may
represent the
same set of integer values. The long int type, which may be written as just long, may
occupy more
storage than the int type and thus be able to represent a larger range of values. Again, the
standard does
not require the long type to be bigger then the int type. Finally, the long long int type, or
just
long long, may be larger than a long. The C++ standard guarantees the following relative
ranges of
values hold:

short int <= int <=long int <=long long int

By default, an integer variable is assumed to be signed (i.e., have a signed representation so that
it can assume positive as well as negative values). However, an integer can be defined to be
unsigned by using the keyword unsigned in its definition. The keyword signed is also
allowed but is redundant.
unsigned short age = 20;
unsigned int salary = 65000;
unsigned long price = 4500000;

Floating-point Types
Many computational tasks require numbers that have fractional parts. For example, the
formula from mathematics
to compute the area of a circle given the circle’s radius, involves the value p, which is
approximately
3.14159. C++ supports such non-integer numbers, and they are called floating-point
numbers. The name
comes from the fact that during mathematical calculations the decimal point can move or
“float” to various
positions within the number to maintain the proper number of significant digits. The types
float and
double represent different types of floating-point numbers. The type double is used more
often, since it
stands for “double-precision floating-point,” and it can represent a wider range of values
with more digits of
precision. The float type represents single-precision floating-point values that are less
precise. Table 3.3
provides some information about floating-point values as commonly implemented on 32-bit
computer systems.
Floating point numbers can be both positive and negative.
Any literal numeric value with a decimal point in a C++ program automatically has the type
double. To make a literal floating-point value a float, you must append an f or F to the
number, as in 3.14f. Floating-point numbers are an approximation of mathematical real
numbers.

Floating-point numbers are an approximation of mathematical real numbers. As in the case


of the
int data type, the range of floating-point numbers is limited, since each value requires a
fixed amount of
memory. In some ways, though, int are very different from doubles. Any integer within the
range of
the int data type can be represented exactly. This is not true for the floating-point types.
Consider the
real number p. Since p contains an infinite number of digits, a floating-point number with
finite precision
can only approximate its value
#include <iostream>
using namespace std;
int main() {
double pi = 3.14159;
cout << "Pi = " << pi << endl;
cout << "or " << 3.14 << " for short" << endl;
}

Constants
C++ supports named constants. Constants are declared like variables with the addition of
the const keyword:
const double PI = 3.14159;
Once declared and initialized, a constant can be used like a variable in all but one way—a
constant may not be reassigned. It is illegal for a constant to appear on the left side of the
assignment operator (=) outside its declaration statement. A subsequent statement like
PI = 2.5;
would cause the compiler to issue an error message: error C3892: ’PI’ : you cannot assign to
a variable that is const

example:
#include <iostream>
using namespace std;
int main() {
const double avogadros_number = 6.022e23, c = 2.998e8;
cout << "Avogadro's number = " << avogadros_number << endl;
cout << "Speed of light = " << c << endl;
}

#include<iostream.h>
void main ()
{
const double pi = 3.14;
cout<<”Defined constant PI = “<<PI<<endl;
cout<<”Memory constant pi = “<<pi<<endl;
cout<<”Literal constant Pi = “<<3.14<<endl;
}

Characters
The char data type is used to represent single characters: letters of the alphabet (both
upper and lower case), digits, punctuation, and control characters (like newline and tab
characters).
In C++ source code, characters are enclosed by single quotes ('), as in
char ch = 'A';
Standard (double) quotes (") are reserved for strings, which are composed of characters, but
strings and
chars are very different. C++ strings are covered in Section D.7. The following statement
would produce
a compiler error message:
ch = "A";
since a string cannot be assigned to a character variable

#include <iostream>
using namespace std;
int main() {
char ch1, ch2;
ch1 = 65;
ch2 = 'A';
cout << ch1 << ", " << ch2 << ", " << 'A' << endl;
}

The program displays


A, A, A
The first A is printed because the statement ch1 = 65; assigns the ASCII code for A to ch1.
The second A is printed because the statement ch2 = 'A'; assigns the literal character A to
ch2. The third A is printed because the literal character 'A' is sent directly to the output
stream

Some characters are non-printable characters. The ASCII chart lists several common non-
printable
characters:
• '\n'—the newline character
• '\r'—the carriage return character
• '\b'—the backspace character
• '\a'—the “alert” character (causes a “beep” sound on many systems)
• '\t'—the tab character
• '\f'—the formfeed character

Expressions
An expression is any computation which yields a value.When discussing expressions, we often
use the term evaluation. For example, we say that an expression evaluates to a certain value.
Usually the final value is the only reason for evaluating the expression
C++ provides operators for composing arithmetic, relational, logical, bitwise, and conditional
expressions. It also provides operators which produce useful side effects, such as assignment,
increment, and decrement. We will look at each category of operators in turn. We will also
discuss the precedence rules which govern the order of operator evaluation in a multi-operator
expression.

C++ provides five basic arithmetic operators

When both operands of the division operator (/) are integers then the division
is performed as an integer division and not the normal division we are used to.
Integer division always results in an integer outcome (i.e., the result is always
rounded down). For example:

9 / 2 // gives 4, not 4.5!


-9 / 2 // gives -5, not -4!

To obtain a real division when both operands are integers, you should cast one of the operands to
be real:

int cost = 100;


int volume = 80;
double unitPrice = cost / (double) volume; // gives 1.25

Relational Operators

C++ provides six relational operators for comparing numeric quantities

Logical Operators
C++ provides three logical operators for combining logical expression
Increment/Decrement Operators

The auto increment (++) and auto decrement (--) operators provide a
convenient way of, respectively, adding and subtracting 1 from a numeric variable

The examples assume the following variable definition:


int k = 5;

Both operators can be used in prefix and postfix form. The difference is significant. When used
in prefix form, the operator is first applied and the outcome is then used in the expression. When
used in the postfix form, the expression is evaluated first and then the operator applied

Assignment Operator
The assignment operator is used for storing a value at some memory location
(typically denoted by a variable). Its left operand should be an lvalue, and its right
operand may be an arbitrary expression. The latter is evaluated and the outcome is
stored in the location denoted by the lvalue

The sizeof Operator


C++ provides a useful operator, sizeof, for calculating the size of any data item
or type. It takes a single operand which may be a type name (e.g., int) or an
expression (e.g., 100) and returns the size of the specified entity in bytes

You might also like