Tag: literals

Primitive Built-in Types

Types

C++ defines a set of primitive built-in types that are either arithmetic or void. Arithmetic types can be further classified into character, integral and floating-point. Table below shows all the types, sizes and their uses.

Type (Min. ) Size in bits Remarks
char 8 Character type. Used to store basic character set. Is the same size as a single machine byte
wchar_t, char16_t, char32_t 16, 16, 32 More character types. Used for extended character set. Used for Unicode characters and/or internationalization.
bool NA(typically size of int) Boolean type.
short, int, long, long long 16, 16, 32, 64 Integer types.
float, double, long double 6 significant digits (typically 32 bits), 10 (64b), 10 (96b) floating point types. float is single precision, double is double precision and long double is extended precision floating point.

Types char, short, int, long, long long can be signed or unsigned. By default these types are signed. Note other types cannot be qualified with signedness. Making a variable unsigned helps gain one extra bit, doubling the value that can be stored in the variable.

The sizeof operator reports the size of a variable/type in bytes for that system.

Typically, the signed values are stored in two’s complement notation. Floating point types are represented using IEEE representation (see: MSDN and Wiki).

void is used when a function does not want to return a value.

Limits of the types

The limits of the types can be found using the limits library. A sample code (adapted from here) is shown below to get int limits.

// numeric_limits example
#include <iostream> 
#include <limits>      // std::numeric_limits
int main () {
std::cout << "Minimum value for int: " << std::numeric_limits::min() << '\n';
std::cout << "Maximum value for int: " << std::numeric_limits::max() << '\n';
std::cout << "int is signed: " << std::numeric_limits::is_signed << '\n';
std::cout << "Non-sign bits in int: " << std::numeric_limits::digits << '\n';
std::cout << "int has infinity: " << std::numeric_limits::has_infinity << '\n';
return 0;
}

Is signed or unsigned better?
Unsigned is better. See here for a detailed answer. Reasons:

  • Lack of signed integer support at the op code level
  • Shift right is not the same as divide by two for signed integers
  • Unsigned integers can often save you in a comparison

What happens when you mix types?
Required to read type conversions. Will update soon.

Literals

A literal is a value such as a number, character or a string. The value of the literal cannot be changed. Integer literals can be written in decimal (e.g., 15), octal (e.g., 017), hexadecimal (e.g., 0xFF) or binary (e.g., 0b1111).

What is the type of a literal?
Literals take the smallest type. A decimal literal can be one of int, long, or long long in which the value fits first. Similarly, octal or hexadecimal literals have the smallest type of int, unsigned int, long, unsigned long, long long or unsigned long long.

Are decimal literals signed type?
Technically, no. The minus sign is an operator on the literal.

Can you specify type of literal?

Suffix (Min. ) Type
u or U long
l or L unsigned
ll or LL long long
f or F float
l or L double

What about character literals?
Character literals are specified by enclosing a single quote (e.g., ‘x’). String literals are specified by using double quotes (e.g., “” for empty string, “hello!”). String literals are C-type strings terminating with a '\0' character.

Is this a valid string literal – “abc ” ” def”?
Yes. String literals separated by white space characters (space, tabs or newline) will be concatenated.

What’s a quote doing in a integer literal – 3.141’592’653? Is it valid?
It is a valid literal. The quote here is only for readability used as separators (e.g., 3.141,592,653).

How do we specify a Unicode string literal?

Prefix Type
u8 char
u char16_t
U char32_t
L wchar_t

Are there any other literals?
Boolean (true or false). Pointer (nullptr).

FAQ

Remember to read the faq !
One question from this faq:
How can I tell if an integer is a power of two without looping?

bool isPowerOf2(int i) {
    return i > 0 && (i & (i - 1)) == 0;
}

This follows the logic of K&R’s fastest way to compute the number of 1’s in a bit-representation of an integer.

unsigned countOnes (int x) {
    unsigned count = 0;
    while (x > 0) {
        x &= x-1;
        count++;
    }
    return count;
}