Basic data types
C/C++ statically typed languages. Before we can store certain values in the computer memory, we need to declare their data types.
- C
- C++
src/c/basics/02-data-types.c
#include <stdio.h>
int main()
{
unsigned int u_num = 8;
printf("Unsigned integer: %u\n", u_num);
int num = -9;
printf("Integer: %d\n", num);
long int large_num = 3988764563425;
printf("Large integer: %ld\n", large_num);
float num_f = 3.14159;
printf("Float: %f\n", num_f);
double num_d = 3.141592653589793;
printf("Double precision float: %16.15f\n", num_d);
char c = 'a'; // char data type must be wrapped in single quotes
// double quote in c/c++ denotes string literal which is an
// array of char with null terminator (\0)
printf("Char: %c\n", c);
printf("ASCII value of char: %d\n", c);
printf("char corresponding to ascii value 98: %c\n", 98);
char message[] = "Hello"; // string literal
// same as {'H', 'e', 'l', 'l', 'o', '\0'}
printf("String: %s\n", message);
return 0;
}
src/cpp/basics/02-data-types.cpp
#include <iostream>
#include <iomanip> // setprecision
using namespace std;
int main()
{
unsigned int u_num = 8;
cout << "Unsigned integer: " << u_num << endl;
int num = -9;
cout << "Integer: " << num << endl;
long int large_num = 3988764563425;
cout << "Large integer: " << large_num << endl;
float num_f = 3.14159;
cout << "Float: " << num_f << endl;
double num_d = 3.141592653589793;
cout << "Double precision float: " << setprecision(16) << num_d << endl;
char c = 'a';
cout << "Char: " << c << endl;
cout << "ASCII value of char: " << int(c) << endl;
cout << "char corresponding to ascii value 98: " << char(98) << endl;
char message[] = "Hello";
cout << "String: " << message << endl;
return 0;
}
info
A decimal integer cannot start with 0
, in that case it is considered octal
base. For example, 076
denotes 63
in decimal notation.
Good to know
For numeric data types that span multiple bytes, the order of arrangement of bytes is important. Depending on the processor architecture, there are two formats: big endian and little endian.
Big endian: the most significant bits occupy the lower address. This representation is used in powerpc processor.
Little endian: the least significant bits occupy the lower address. This format is used in all x86 compatible processors.