2.C Language Basics
2.C Language Basics
SUSHANT BHATTARAI
Prepared By: Sushant Bhattarai
About this chapter 2
Construction
#include <iostream>
using namespace std;
int main()
{
cout<< “Hello World\n”;
return 0;
}
Basic Program 4
Construction
Basic Program Construction- Functions 5
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout<<“Enter two numbers”<<endl;
cin>>a>>b;
cout<<“The sum is”<<a+b;
return 0;
}
Some Programs-2 12
#include <iostream>
using namespace std;
Int main()
{
char msg[20];
cout<<“Enter a some text:”;
cin>>msg;
cout<<“ Your message is :”<<msg;
return 0;
}
Some Programs-3 13
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<“Enter two numbers”<<endl;
cin>>a>>b;
c=a+b;
cout<<“The sum of ”<<a<<“ and ”<<b<<“ is: ”<<c;
return 0;
}
2.1 Character set, tokens (keywords,
identifiers, operators)
SUSHANT BHATTARAI
Tokens 15
1. +
2. -
3. *
4. /
5. %
20
Arithmetic Assignment Operators
1. a += b is same as a = a + b
2. a *= b is same as a = a*b
3. a -= b is same as a = a – b
4. a /= b is same as a = a/b
5. a %= b is same as a = a % b
21
Increment Operators
Pre increment and post increment.
The use of ++i and i++ is same as i=i+1
22
Increment Operators
23
Increment Operators
Relational Operators 24
1. AND &&
2. OR ||
3. NOT !
Logical Operators 28
Logical Operators 29
Conditional Operator 30
Conditional Operator 31
2.2 Commenting
SUSHANT BHATTARAI
Comments 33
/*this
Is a multi line
comment
*/
Comments 34
SUSHANT BHATTARAI
Variables Declaration 36
variable
The variable name should start with only letters or
underscore. int a; int _a;
The variable name should not be keyword.
White spaces are not allowed before, between
characters of variables.
The variable name is case sensitive.
Example 38
#include <iostream>
using namespace std;
int main()
{
string a;
string b;
cout<<"Enter your first name"<<endl;
cin>>a;
cout<<"Enter your surname"<<endl;
cin>>b;
cout<<"How You Doin’? "<<a+b<<endl;
return 0;
}
Example 39
Constants 40
#include <iostream>
#define pi 3.14
using namespace std;
int main()
{
float r,a;
cout<<“ Enter Radius”<<endl;
cin>>r;
a=pi*r*r;
cout<<“ The area is ”<<a;
return 0;
}
Declared Constant 43
#include <iostream>
using namespace std;
int main()
{
float r;
const float pi=3.14;
cout<< "ENTER A RADIUS "<<endl;
cin>>r;
cout<<"The area is "<<pi*r*r<<endl;
return 0;
}
3.4 Data Type
SUSHANT BHATTARAI
Data Type 46
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a,b;
cout<<" ENTER NUMBER "<<endl;
cin>>a>>b;
cout<<"The sum is "<<a+b<<endl;
cout<<"The difference is "<<a-b<<endl;
cout<<"The product is "<<a*b<<endl;
cout<<"The quotient is "<<a/b<<endl;
return 0;
}
Some Programs-5 49
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float r;
cout<<" ENTER A RADIUS "<<endl;
cin>>r;
cout<<"The area is "<<(22/7)*r*r<<endl;
return 0;
}
3.5 Type Conversion and
promotion rules
SUSHANT BHATTARAI
Type Conversion 51
#include <iostream>
numi=num;//type conversion
#include <cmath>
a=ai;
#include <windows.h> cout<<"Value as short type: "<<num<<endl;
using namespace std; cout<<"Value as int type: "<<numi<<endl;
int main() cout<<a<<endl;
{ cout<<ai<<endl;
return 0;
int a;
}
float ai=3.14;
short num=200;
int numi;
cout<<"Hello"<<endl;
system("cls");
Implicit Type Conversion 54
Explicit Type Conversion 55
SUSHANT BHATTARAI
Output using cout 59
SUSHANT BHATTARAI
Preprocessor directives 62
SUSHANT BHATTARAI
Loops 64
SUSHANT BHATTARAI
Array 89
A simple example
Array 91
Defining Array 92
Defining Array 93
Array Example-2 94
for(i=0;i<4;i++)
#include <iostream> {
cout<<"ENTER NAME: ";
#include <iomanip>
cin>>name[i];
using namespace std; cout<<"ENTER ADDRESS:";
cin>>address[i];
}
int main()
cout<<"NAME"<<setw(12)<<"ADDRESS"<<endl;
{
for(i=0;i<4;i++)
int i; {
const int size=4; cout<<name[i]<<setw(8)<<address[i]<<endl;
}
string return 0;
name[size],address[size]; }
Array Example-2 95
Initializing Array 96
Initializing Array 97
Multidimensional Array 98
datatype array_name[size1][size2]
Multidimensional Array 99
10
Multidimensional Array 0
10
String 1
Compares 2 string
Accepts two strings as a parameter and returns
1. Less than 0 if the first string is less than the second
2. 0 if the first string if both are same
3. More than 0 if the first string is more than the
second
11
strcmp() 6
11
strcmp() 7
11
strcmp() 8
11
strcmp() 9
12
strrev() 0
Variable
A pointer can be re-assigned any number of times
while a reference can’t be reassigned after
initialization.
References cannot be null whereas pointers can.
3.10 Dynamic memory
allocation
SUSHANT BHATTARAI
14
Dynamic memory 2
allocation
The process of allocating and freeing memory at
runtime is called DMA.
This is advantageous as memory is used efficiently.
Memory can be shrinked as well as expanded.
14
new operator 3
#include <iostream>
#include <iomanip> for(i=0;i<n;i++)
using namespace std; {
cin>>*(p+i);
sum=sum+*(p+i);
int main() }
{ avg=sum/n;
int n,i; cout<<endl;
float *p,sum=0,avg; cout<<"THE AVERAGE MARKS
OF ";
cout<<"ENTER THE NUMBER OF
STUDENTS: "; for(i=0;i<n;i++)
cin>>n; cout<<setw(3)<<*(p+i);
cout<<" is: "<<avg<<endl;
p = new float[n];
delete []p;
cout<<"ENTER MARKS OF "<<n<<" return 0;
STUDENTS"<<endl;
}
14
Example 6
3.11 Functions
SUSHANT BHATTARAI
14
What is Function? 8
#include <iostream>
z=add(x,y);
using namespace std;
cout<<"THE SUM IS
int add(int a,int b); "<<z<<endl;
return 0;
int main() }
{ int add(int a,int b)
{
int x,y,z;
return a+b;
cout<<"ENTER TWO }
NUMBERS"<<endl;
cin>>x>>y;
14
What is function? 9
3 types.
1. Passing arguments as constants.
2. Passing arguments by value.
3. Passing arguments by address(Function Call by
reference)
15
Passing constants 6
15
Passing Values 7
15
8
Pass by address(call by
reference)
In this kind of function call, the address of variable
is passed to the function as argument instead of
actual value of variable.
The reference variable is used for call by
reference.
15
9
Pass by address(call by
reference)
16
0
Pass by address(call by
reference)
16
Without return 1
16
Returning by value 2
16
3
Function overloading
Overloading is like the joke about the famous
scientist who insisted that the thermos bottle was
the greatest invention of all time.
Why? “It’s a miracle device”, he said.
“It keeps hot things hot and cold things cold. How
does it know?”
16
4
Function overloading
An overloaded function appears to perform
different activities depending on the kind of data
sent to it.
We can have two function with the same name
but different in either number of arguments or
type of arguments. The correct version of function
is called based on nature of argument. This is
called Function overloading.
16
Types of function 5
overloading.
1. Different number of Arguments.
2. Different types of Arguments.
16
Different number of 6
Arguments
We can define more than one function with same
name but different number of arguments.
16
Different number of 7
Arguments
16
Different number of 8
Arguments
16
Different type of 9
arguments
We can define more than one function with same
name but different type of argument.
17
Different type of 0
arguments
17
Different type of 1
arguments
17
2
Inline function
Call and return take time.
So, to save execution time we put the code in the
function body directly inline with the code in the
calling program.
It is called inline function.
We use inline keyword in the function definition.
17
3
Inline function
17
4
Inline function
17
5
Default argument
Sometimes, a function can be called without
specifying all it’s argument.
The function definition must provide default values
for those arguments that are not specified.
17
6
Default argument
17
7
Default argument
17
Passing array to a function 8
SUSHANT BHATTARAI
18
Structures 2
structure.
entities using
A structure is a collection of
simple variables.
The variables in a structure
can be of different types.
The data items are called the members of the
structure.
18
Structures Example 3
18
Structures Example 4
18
Declaring Structures 5
struct part
{
int modelnumber;
float cost;
};
18
Defining a Structure 6
Variable
18
Accessing Structure 7
Members
part1.modelnumber;
part1.cost;
18
Initializing Structure 8
part part1={101,200000};
18
Another Example 9
19
Another Example 0
19
Another Example 1
19
Array of Structures 2
Sequences
19
Enumerators 7