Lower Language: Instructions that are directly tied to one machine.
Middle level: C/ C++
High level: Matlab
Basic data types:
int: - 2,147,483,648 till 2,147,483,647
unsigned int: 0 till 4,294,967,295
short: - 32,768 till 32767
unsigned short: 0 till 65535
char: -128 till 127
unsigned char: 0 till 255
Write a program to test the biggest number that a short can represent.
For unsigned version:
#include<stdio.h>
2
3 int main(){
4 unsigned short temp = 1;
5 while(1){
6 unsigned short tempnew = (temp << 1) + 1;
7 if(tempnew == temp ){
8 break;
9 }
10 temp = tempnew;
11 }
12 printf("the number is %d",temp);
13 }
For signed version:
#include<stdio.h>
2
3 int main(){
4 short temp = 1;
5 while(1){
6 short tempnew = (temp << 1) + 1;
7 if(tempnew < temp ){
8 break;
9 }
10 temp = tempnew;
11 }
12 printf("the number is %d",temp);
13 }
~
About Declaration:
C++ requires forward declaration.
Any label (variable name, function name, class name, etc.) must be declared before using.
This is because C++ executes base on the sequence of written code. C++ program starts from the main function, and flows top to bottom of each functions.
What needs to happen earlier needs to be written before what needs to happen later. Therefore, you need to declare variables before use them.
About text string:
In C++/C, string is implemented by char array. And the char array is special in C/ C++, because there is an ending charter for each array and occupies a 8bit(the last position of char) of the char array. Therefore, if the array is declared as 'char temp[8]', it can only take 7 char and the last one is '0'.
About type casting:
/*usage of type casting*/
int a = 10;
int b = 100;
float ans = a/b;
// output of ans is 0;
float ans1 = (float) a/b;
// output of ans1 is 0.1;
float ans2 = (float) a/ (float)b;
//output of ans2 is 0.1.
float ans3 = a / (float)b;
//output of ans3 is 0.1
/*However, be careful, the output of*/
float ans4 = (float)(a/b);
// output of ans4 is 0.
About the format of printf:
%d | decimal output |
%o | octal output |
%d | hex output (hexadecimal) |
%u | unsigned version decimal |
%f | decimal float or double |
%e | exponent style float or double |
%c | char |
%s | string |
%g | output shorter one of f or g |