Mehran University of Engineering & Technology
Jamshoro,Pk
#include Includes standard header file
<iostream>
Input output stream permission
console
#include<conio.h> (Console input-
output header)
Perform console input and console
output operations.
#include<math.h> (Math header )
Perform mathematical operations
like sqrt() and pow(). To obtain
the square root and the power of a
number respectively.
#include<iomanip.h> (Input-
Output
Manipulation) – Used to access
set() and setprecision().
Using namespace std; Standard directories/ library
#include "Header.h" Includes Header.h file
#define PI 3.14159265359 Defines Easy Macro
#define
This C++ Command can be used
to substitute a particular value
throughout the file in which it is
located.
int main()
This is the main function from
where execution of any C program
begins.
Cout
print command prints the output
onto the screen.
Basic Commands in C++
getch();
This command waits for any
character input from keyboard.
return 0;
This command terminates C
program (main function) and
returns 0.
}
This indicates the end of the main
function.
Statements and Arguments
if
We use if so as to make decisions
in C programming
#else Else part of the above if- statement
#endif Ends the if-statement
break
The break statement would make
the program jump out of the most
inner and enclosing loop in an
explicit manner.
continue
The continue is used for
statements skipping certain
statements that are inside the loop.
goto
goto keyword is needed for the
transfer of control from one
position to another.
switch, case and default
We use switch as well as case statements
whenever the block of statements needs to
be executed among various blocks.
Do-while loop
do is used along with a while to
make a different form of repetition
of the statement. This form of the
loop makes use of the following
syntax:
while
while it is used for repeating the
execution when the condition is
true.
{
This indicates the beginning of the
main function.
/*_some comments_*/
whatever is given inside the
command “/* */” in any C
program, won’t be considered for
compilation and execution.
Data Types
Double
This is used to assign a double
type to the variable name
following it.
Char
This is the command which is used
to define any variable contained
with the char datatype.
Float
This is used to define a floating
kind of value to the variable.
Int Integer
Intermediate Commands
for
his is the looping C command
which is used when you know the
definite loops.
\t:
This is the command for using a
tab or a blank tab of horizontal
space.
\n
This is another format specifier
escape sequence which is used to
break into a new line.
Arithmetic Operators
+ Adds two values
– Subtract second value from first
* Multiply two values
/ Divide numerator by the
denominator
% Remainder of division
++ Increment operator – increases
integer value by one.
— Decrement operator – decreases
integer value by one.
Relational Operators
== This will check if two operands
are equal
!= This will check if two operands
are not equal.
> This will check if operand on the
left is greater than operand on the
right
< This will check if operand on the
left is smaller than the right
operand
>= This will check if the left operand
is greater than or equal to the right
operand
<= This will check if operand on left
is smaller than or equal to the right
operand
Logical Operators
&& It is used to check if both the
operands are true.
|| OR These operators are used to
check if at least one of the operand
is true.
! NOT Used to check if the operand
is false
Bitwise Operators
& Bitwise AND: Converts the value
of both the operands into binary
form and performs AND operation
bit by bit.
| Bitwise OR: Converts the value of
both the operands into binary form
and performs OR operation bit by
bit.
^ Bitwise exclusive OR: Converts
the value of both the operands into
binary form and performs
EXCLUSIVE OR operation bit by
bit.
~ Bitwise complement: Converts the
operand into its complementary
form.
<< Shift left
>> Shift right
Assignment Operators
= This will assign values from right
side operands to left side operand
+= This will add the right operand to
the left operand and assign the
result to left
-= This will subtract right operand
from the left operand and assign
the result to left operand
*= This will multiply left operand
with the right operand and assign
the result to left operand
/= This will divide left operand with
the right operand and assign the
result to left operand
%= This will calculate modulus using
two operands and assign the result
to left operand
Special Operators
& It refers to the address (memory
location) in which the operand is
stored.
* It is a pointer operator to a variable.
size of () This operator gives the size of the
variable.
? (Condition) – It is an alternative for if-else
condition
Various Math Functions in C
Sqrt () This function returns the square
root of a specified number.
round () This function rounds the nearest
value of a given input.
pow () This function returns to power for
the given number(ab)
exp()
This function does computation on
exponential for a given value(ex).
There are also other subtypes like
frexp(), Idexp() returning mantissa
and multiplied to the power of x.
log() This function returns the logarithm
value of a given number. (to the
base e. loge)