To make a C++ program you must declare names or identifiers for things that can be constants, variables, functions or data types. C++ has no "define" keyword: instead, a statement beginning with the name of a type. There is no "function" keyword: instead, a function is indicated by the parentheses following the function name. In parentheses you can enumerate formal parameters.
Primitive Types:
To understand C++ code you must grasp idea of data type. You can not declare anything without using a data type. However you can define your own types using keyword: typedef. C++ has very few basic data types enumerated below:
Type Name | Description | Size | printf() placeholder |
char | character | 1 byte | %c |
int | default integer | 4 bytes | %d |
short int | short integer | 2 bytes | %hd |
long int | long integer | 8 bytes | %li |
unsigned int | unsigned integer | 8 bytes | %u |
float | single precision floating point | 4 bytes | %f |
double | double precision floating point | 8 bytes | %lf |
To detect the real size of the data type you can use function: sizeof()
#include <stdio.h>
int main() {
short a;
long b;
double d;
printf("size of short = %d bytes\n", sizeof(a));
printf("size of long = %d bytes\n", sizeof(b));
printf("size of long double= %d bytes\n", sizeof(d));
return 0;
}
Note: You can use following modifiers for integer type:
Derived types:
You can create new data types from basic types using a derived data types. There are different methods of derivation. One method is to group multiple elements into data collection or structure. These are called improperly user defined data types.
I will present derived types in future articles.
Next, you will learn about variables and rules for naming a variable. A variable is like a labeled container or storage area in computer memory used to hold data for a short period of time. Variable names are symbolic representation of a memory location. Content of this location can be changed.
//declare variable ch with initial value 'a'
char ch = 'a';
//alter variable ch content using operator '='
ch = 'l';
Notes:
You can define variables that are immutable. These area actually called constants.
const double PI = 3.14; //protected
PI = 2.9; //Error: PI is a constant
Now is time to understand something more. C++ source code can contain numbers and characters after the symbol "=", for example: 3.14. These elements of the language are also constants, but are different kind of constants named Literals. The literals can be changed only if you change the source code.
Literal examples:
Escape sequence:
Some characters are not printable. For representing these characters in string literals you can use escape notation. Use symbol "\" follow by a letter. This will be replaced by the compiler with the proper code point.
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Return |
\t | Horizontal tab |
\v | Vertical tab |
\\ | Backslash |
\’ | Single quotation mark |
\" | Double quotation mark |
\? | Question mark |
\0 | Null character |
"First line \nSecond line."
Read next: Control Flow