Fundamental Data Types
Basic Types
Signed Types
Signed integer data types are defined with the long, int, and short keywords (no byte keyword). However, unlike in Java, the size of each integer data type is not defined by the standard - the standard only defines the minimum size.
To specify a size, which is a good practice in Embedded Programming, it is recommended to use integer data types with length modifiers such as int8_t, int16_t, int32_t or int64_t. When using such integer data types, the size is not platform specific anymore and portability is improved.
Unsigned Types
Unlike Java, integer values can also be unsigned (only positive) in C/C++. Unsigned integer data types can be defined by prefixing the integer data types with a u, such as in uint8_t.
When using unsigned integer data types, the programmer must be careful when performing conversion to or from signed data types. Conversion operations will always perform without errors and since the available ranges differ between signed and unsigned data types, it is the responsibility of the programmer to guarantee that no error happens.
Floating Point Types
Floating point data types are defined with the float and double keyword. float and double correspond to single and double precision floating-point types, as defined in the IEEE-754 binary32 format and IEEE-754 binary64 format specifications. Their sizes are thus defined by the language, unlike integer data types.
Boolean Type
Boolean type is defined with the bool keyword, which may hold the true or false literal values. Note that the size of a boolean variable is also platform specific and not necessary 1 byte.
Character Types
Character data types are usually defined with the char keyword. In C++, character data types can also be signed and can also be suffixed with a length modifier, such as char8_t or char16_t used for representing UTF-8 or UTF-16 characters.
Example
#include <iostream>
#include <cstdint>
int main() {
// Signed data types
short s = 0;
int i = 1;
long l = 2;
int8_t i8 = 3; // this variable ranges from -2^7 to +2^7 - 1
int16_t i16 = 4; // this variable ranges from -2^15 to +2^15 - 1
std::cout << "Number of bytes for i : " << sizeof(i) << std::endl;
std::cout << "Number of bytes for i8 : " << sizeof(i8) << std::endl;
std::cout << "Number of bytes for i16 : " << sizeof(i16) << std::endl;
// Unsigned data types
unsigned short us = 0;
unsigned int ui = 1;
unsigned long ul = 2;
uint8_t ui8 = 5; // this variable ranges from 0 to 255
uint16_t ui16 = 6; // this variable ranges from 0 to 2^16 - 1
// Floating data types
float f = 1.2;
double d = 3.4;
// Boolean data type
bool b = true;
// Character data type
char c = 'a';
return 0;
}
Strings
In C, a string is nothing else than a character array (i.e. char[]),
terminated by the 0 value. In C++, the standard library includes a
std::string type, with similar functionalities as the String class of Java.
An important difference as compared to Java is that instances of the string
class in C++ are not immutable and can thus be modified. Also, since
overloading operators is possible in C++, the == operator has been defined
for the string class, hence the in-depth comparison of two string instances is
possible by using the == operator. Finally, in C++ string objects can only
be concatenated with other string objects - no auto-boxing and implicit
conversions like in Java.
example
#include <iostream>
#include <string>
int main() {
std::string s1 = "Hello";
std::string s2 = "world";
// Concatenation
std::string s = s1 + " " + s2;
std::cout << s << std::endl;
// Not possible
// std::string x = "10";
// int y = 20;
// std::string z = x + y;
// Length
std::cout << "String length : " << s.length() << std::endl;
// The size method is an alias for length
std::cout << "String length : " << s.size() << std::endl;
// Get the first character
std::cout << "The first character is : " << s[0] << std::endl;
// Change the first character
s[0] = 'h';
std::cout << s << std::endl;
// The backslash character turns special characters into string characters
std::cout << "\"Hello world\"" << std::endl;
return 0;
}
The null pointer
In C, the null pointer is often represented with the NULL value. In C++, the
null pointer literal is std::nullptr. The type of this null pointer
literal is std::nullptr_t. It is not itself a pointer type, but it may be
implicitly converted to any pointer type.
example
#include <cstddef>
int main() {
std::nullptr_t n1 = nullptr;
return 0;
}