You are on page 1of 5

C++

Variables data type

 Preprocessor directives begins with # and donot require semicolon at the end:-
#include<iostream> , # define PI =3.14 .
 Format of declaring variable is variabletype identifier; the identifier must only contain
character,digits and_ , it should not begin with digits.
 Each binary digit is stored in one bit and 1 byte contains 8 bits. Int type has storage of 4 bytes.
Remember of 8bits 7 of them are used to store binary digits and one is used to assign sign.
 Unsigned , long, signed, etc are used before variable type for changing the range of the variable
type. const used before the variabletype makes it constant and its value cant be modified in
future.
 Scope of variable is limited to the {} they are declared within. If there is another function
declared within the {} of the variable then the variable can be used inside that function eg
int main()
{
int raj;
Int sum (int x,int y) {raj = x+y ;}
}
 The integers are assigned directly using = operator but character must be enclose within (‘ ‘)
and string within (“ “). When we typecast a char variable into int variable its ASCII value gets
stored in the int variable:- char b= ‘h’; int a = (int) b; infact char chr=97; is also a valid
declaration.
 Any type of variables can be typecasted into other type by simply enclosing it into the brackets
as done above.
 During cout we can use \n within “ ” to change the line or can use <<endl; outside the “ “

Operators

 Following are the operators in their preceding order:- ip tmarao as


1) ++ , -- increment
2) * pointer
3) typecasting
4) * / multiplicative
5) + - additive
6) >, <, != ,== relational and equalilty
7) & & ,|| and or
8) = , -=, += etc assignment
 The operator a+=b is equivalent to a= a+b;
 The operator ++a increments the value before its use but a++ increments it after (not at this
position)
 There is an modulo operator % which gives the numerical value of remainder 7%3==1.
 Sizeof(name or type of variable) function gives the size of variable type.

Control structures

 For certain conditions we need some statements to be executed we use if else:-


if(condition) { statement ;} the program continues if this condition is false. The condition must
be true for the evaluation of the statements inside the if structure. If condition is a Boolean
variable then if it contains value true (not zero) then the if condition is run.
 if we want this or that (out of two ) statement to be executed then use the following:
if (condition) {statement 1;} else {statement 2; } the program enters else if the condition is false.
 if we want to execute first statement or 2nd or 3rd or 4th etc on the basis of condition we use
concatenated form of if else :-
if (condition1) {statement1;}
else if (condition 2) {statement 2;}
else if (condition 3) {statement 3;}
else {statement4;}
 While loop is used to perform certain steps repeatedly if some condition is true during that
period :- while(condition){ statement;} . do while loop is another format of while loop which
runs atleast one time .ie it executes the false statement also atleast one time. Format:-
do{statements;} while (condition).
 For loop is more specifically used for countdown purpose loops :-
for( initialize ; condition ; Increment) {statements;} remember we can initialize more than one
varable and increment more than one variable by separating them with commas. And if we left
the initialize and increment blank for( ; condition ; ) then it becomes while loop . at initialize we
can also declare a variable as int i=0;
 Few jump statements are break; to break the loop wherever this statement is encountered,
continue; to leave the current iteration and continue for other iteration in the loop. Usually
these are used with if condition in the loop.
 Switch is similar to concatenated if- else loop
switch (variable){ case constant: statements ; break ; ………default: statements; }

Functions

 type functionname (declaration of parameters, , , ){statements; return value ;} here type is the
value type that the function is going to return like int , char, void etc. inside the () braces the
declaration of variable is done( which is local for function) and whose value is initialized by
passing value during call of the function eg int x,int y.
 function is called by name followed by braces containing the value to pass
functionname (variablename, variablename2,….) remember that the value that the variable
contains is passed to the function . and after function is evaluated the value it returns is
substituted at the place of call.
 We can define a function after main by declaring it before the main function . declaration is
done by type functionname (declaration of parameters, , , ); . note that it only doesnot contain
the statements block and the function is define in the same way afterwards.
 Recursive funtions call themselves within their execution and they always consist of if -else
statements.
 Passing argument to the function as pointer is as type fncname(int *a, char *b){statements}
And calling of function is done as fncname(&x, &raj).

Arrays

 Declaration of array is done by: type name[size]; size should always be an integer equal to no.
of variables we want to declare via array. We can initialize an array at same point of declaration
by using {} . int raj[] = {1,2,3,4}; is also valid as it assumes the size of array during compilation
from the integers defined inside the curly braces.
 To acess any variable of array use name[place of variable]; note that the counting of variables
start from 0,1,2,3,4,5….. ie in int raj[5]; array the first member is raj[0], second is raj[1], third is
raj[2] and fourth is raj[3].
 the name of array is actually an address of the first element of the array.
 Donot exceed the range of array eg raj[4]; above . this does not give compilation error but gives
run time error .
 To find the size of array use function sizeof (name of array);
 to declare multidimensional array use type name [row][column]; and to call any of its member
use name[row indices][column indices]; note that counting of indices starts from 0. Initializing
of multidimensional array is done as name[2][3]={{1,2,3},{4,5,6}};
 if we use int raj[5]; then the each element of array contains garbage value but if we declare an
array int raj[5]={23}; then all the elements except the first will get assigned to zero
 We can declare a nondefined size multivariable array by just leaving the first bracket ,others
must be clearly defined. Eg int raj[ ][5][6];
 We cannot assign one array to another eg int raj[5]= hello; where hello is itself a 5 array. To
initialize a array use for loop.
 We can pass an array to function by various methods
 char myword [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char myword [] = "Hello";
here the character array is initialized in two ways. The null character is used to initialize in first
case .whereas , it is present as default in the second case.
 cin and cout support null-terminated sequences as valid containers for sequences of characters
means that we can directly cout a character array terminated with a null character similarly cin
the character array terminated with a null character.
 Cin stops at a space and cout stops at null character.
 When we declare an array then it contains garbage value and as soon as we assign its first
member a value, others becomes zero.
 Passing array to the function can be performed as:- declaration of function
int funcname (int arr[]){statements;} , calling of function funcname(arrayname); or we can pass
an array using pointers int funcname (int *p){statements;} and calling the function as
funcname(arrayname); . these all types are passing by reference, passing an array by value is
somewhat difficult.
 When we cout << nameintarray; we get the address of the first variable but when we
cout<<namechararray; we get the entire content of the array. Also in the character array the
name of the array does not show the address of the first element of the array but it do point the
first element of the array.
 Returning an array from functions is done using pointers first a pointer function is declared, then
a static type array is made inside the function and its address is returned by the function
Int *pfunc(){static int raj[]={1,2,3}; return raj;} and calling of the function is done as
int *numbers= pfun();

Pointers

 Let a variable int raj; so by direct reffering to raj we say the value contained by raj, by saying
&raj we mean the address of variable raj and *raj means the other variable pointed by raj.
 So & = address of
* = pointed by (these are added before the variable name)
 The name of array also stores address and the name of pointer also stores address , the
difference is that the array address can’t be changed but we can change the address contained
by the pointers.
 The name of array is also a pointer if we write *name then the first element of array is pointed
and its value is displayed, *(name +2) points the third element of array. Which is equivalent to
name[2]; but name++ doesnot work because name of array is a fixed pointer.
 During initialization of pointers:- if we initialize pointer at the place of declaration then we write
as type *name = &variable; and if we initialize pointer at different place then write as
int *name; name=&variable;
 If we Initialize null to a pointer , then the pointer points at nothing and it contains 0 inspite of an
address.eg int *p = NULL;
 Adding 1 to the pointer shifts its address by the size of its type of declaration, for example it
shifts the int type pointer by 4 bytes ,while char type is shifted by 1 byte.
 We can compare the pointers using relational operators on the basis of the address contained
by it.
 To declare arrays of pointers use int *parr[size];

Buildin functions

 Include a header file #include <cmath> to utilize the inbuilt functions of maths.
 Some of these are
Arduino
structure

 The pin which have ~ sign can give output ranging from 0 to high. And those which donot have
this sign can only give digital output.
 Analog inputs (input from the sensors) can only be given at the analog pins.
 Read other components from the tutorials point pdf.

Programming basics

 Arduino syntax consist of two functions called as setup() and loop () [remember here loop is a
function ].
 The data types of arduino variables are same as that of c++ variables and the arduino variables
also have the scope of the {} within which the variable is declared.
 The if else and switch follows the same syntax as in c++.