Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Type in C++: Signed, Unsigned, Short, and Long (Type Modifiers)

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Data type in C++

Type Length Range


unsigned char 8 bits 0 to 255
char 8 bits -128 to 127
enum 16 bits -32,768 to 32,767
unsigned int 16 bits 0 to 65,535
short int 16 bits -32,768 to 32,767
int 16 bits -32,768 to 32,767
unsigned long 32 bits 0 to 4,294,967,295
long 32 bits -2,147,483,648 to 2,147,483,647
float 32 bits 3.4 * (10**-38) to 3.4 * (10**+38)
double 64 bits 1.7 * (10**-308) to 1.7 * (10**+308)
long double 80 bits 3.4 * (10**-4932) to 1.1 * (10**+4932)

signed, unsigned, short, and long (type modifiers)


A type modifier alters the meaning of the base data type to yield a new type. Each of these type
modifiers can be applied to the base type int. The modifiers signed and unsigned can also be
applied to the base type char.
In addition, long can be applied to double. When the base type is omitted from a declaration,
int is assumed.
Examples:
long x; /* int is implied */
unsigned char ch;
signed int i; /* signed is default */
unsigned long int l; /* int OK, not needed */

char (keyword) Character data type


Variables of type char are 1 byte in length. They can be signed (default) or unsigned.
int (keyword) Integer data type
Variables of type int are one word in length.They can be signed (default) or unsigned.
float, double and long double (keywords) Real number data types
Type Length Range
float 32 bits 3.4 * (10**-38) to 3.4 *
(10**+38)
double 64 bits 1.7 * (10**-308) to 1.7 *
(10**+308)
long double 80 bits 3.4 * (10**-4932) to 1.1 *
(10**+4932)

Use of double or float requires linking in the floating-point math package.

printf Commands
printf Format Specifiers

In ...printf format strings, format specifiers have the following form:


% [flags] [width] [.prec] [F|N|h|l|L] type_char
Each format specifier begins with the percent character (%).
After the % come the following, in this order:

Component What It Controls or Specifies


[flags] (Optional) Flag character(s)
Output justification, numeric signs, decimal
points, trailing zeros, octal and hex
prefixes
[width] (Optional) Width specifier
Minimum number of characters to print,
padding
with blanks or zeros
[.prec] (Optional) Precision specifier
Maximum number of characters to print; for
integers, minimum number of digits to print
[F|N|h|l|L] (Optional) Input size modifier
Override default size of next input argument
N = near pointer h = short int
F = far pointer l = long int
L = long double
type_char (Required) Conversion type character

printf Precision Specifiers


printf Flag Characters
The ...printf flag characters specify output justification, numeric signs, decimal points,
trailing zeros, and octal and hex prefixes.
The flag characters can appear in any order and combination.

What It Means
Flag
- Left-justifies the result, pads on the right
with blanks.
If not given, right-justifies result, pads on
left with zeros
or blanks.

+ Signed conversion results always begin with a


plus (+) or
minus (-) sign.

blank If value is non-negative, the output begins


with a blank
instead of a plus; negative values still begin
with a minus.

# Specifies that arg is to be converted using an


"alternate form"

Plus (+) takes precedence over blank ( ) if both are given.


The ...printf precision specifiers set the maximum number of characters (or minimum number of
integer digits) to print.
A printf precision specification always begins with a period (.) to separate it from any
preceding width specifier.

Then, like [width], precision is specified in one of two ways:


 directly, through a decimal digit string
 indirectly, through an asterisk (*)

If you use an "*" for the precision specifier, the next argument in the call (treated as an int)
specifies the precision.
If you use asterisks for the width or the precision, or for both, the width argument must
immediately follow the specifiers, followed by the precision argument, then the argument for the
data to be converted.

How Output Precision Is Affected


[.prec]

(none) Precision set to default:


= 1 for d, i, o, u, x, X types
= 6 for e, E, f types
= All significant digits for g, G types
= Print to first null character for s types
= No effect on c types
.0 For d, i, o, u, x types, precision set to
default;
for e, E, f types, no decimal point is
printed.
.n n characters or n decimal places are printed.
If the output value has more than n
characters, the
output might be truncated or rounded. (Whether
this
happens depends on the type character.)
* The argument list supplies the precision
specifier,
which must precede the actual argument being
formatted.

No numeric characters will be output for a field (i.e., the field will be
blank) if the following conditions are all met:
 you specify an explicit precision of 0
 the format specifier for the field is one of the integer formats (d, i, o, u, or x)
 the value to be printed is 0
How [.prec] affects conversion
Type
Char Effect of [.prec] (.n) on Conversion
d Specifies that at least n digits are
i printed.
o If input argument has less than n
u digits, output value is left-padded
x with zeros.
X If input argument has more than n
digits, the output value is not
truncated.
e Specifies that n characters are
E printed after the decimal point, and
f the last digit printed is rounded.

g Specifies that at most n significant


G digits are printed.

c Has no effect on the output.

s Specifies that no more than n


characters are printed.

printf Conversion-Type Characters


The information in this table is based on the assumption that no flag characters, width
specifiers, precision specifiers, or input-size modifiers were included in the format specifier.
NOTE: Certain conventions accompany some of these format specifiers.
Type
Char Expected Input Format of output
Numerics
d Integer Signed decimal integer
i Integer Signed decimal integer
o Integer Unsigned octal integer
u Integer Unsigned decimal integer
x Integer Unsigned hexadecimal int (with a, b, c, d, e, f)
X Integer Unsigned hexadecimal int (with A, B, C, D, E, F)
f Floating point Signed value of the form [-]dddd.dddd.
e Floating point Signed value of the form [-]d.dddd or e[+/-]ddd
g Floating point Signed value in either e or f form, based on
given value and precision. Trailing zeros and
the decimal point are printed if necessary.
E Floating point Same as e; with E for exponent.
G Floating point Same as g; with E for exponent if e format used
Characters
c Character Single character
s String pointer Prints characters until a null-terminator is
pressed or precision is reached
% None Prints the % character
Pointers
n Pointer to int Stores (in the location pointed to by the input
argument) a count of the chars written so far.

p Pointer Prints the input argument as a pointer; format


depends on which memory model was used. It will be
either XXXX:YYYY or YYYY (offset only).
Infinite floating-point numbers are printed as +INF and -INF. An IEEE Not-A-Number is printed as
+NAN or -NAN.

You might also like