Notes for the Doucmentation
Notes for the Doucmentation
An array is a collection of a fixed number of values of a single type. For example: if you want to store
100 integers in sequence, you can creaate an array for it.
int a[100];
#include<stdio.h>
int main(){
int i;
printf("%d\t",a[i]);
-------------------------------------------
#include<stdio.h>
int main()
int arr[4];
int i, j;
printf("%d\n", arr[j]);
}
----------------------------
#include<stdio.h>
main(){
int i,j,s;
scanf("%d", &s);
int arr[s];
printf("%d\n", arr[j]);
-------------------
#include<stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &marks[i]);
sum += marks[i];
average = sum/n;
return 0;
#include<stdio.h>
main()
int arr[3][4];
int i, j;
scanf("%d", &arr[i][j]);
}
printf("\n");
Example 5:-
#include<stdio.h>
int main()
int i, j;
scanf("%f", &a[i][j]);
}
scanf("%f", &b[i][j]);
printf("\nSum Of Matrix:");
printf("%.1f\t", c[i][j]);
if(j==1)
printf("\n");
return 0;
}
================================
#include<stdio.h>
int main()
int n,r,sum=0,temp;
scanf("%d",&n);
temp=n;
while(n>0)
r=n%10;
sum=(sum*10)+r;
n=n/10;
if(temp==sum)
else
printf("not palindrome");
return 0;
Fibonacci Series in C
#include<stdio.h>
main(){
int n1=0,n2=1,n3,i,number;
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for(i=2;i<number;++i) {
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
#include<stdio.h>
int main(){
int n,i,m=0,k=0;
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++) {
if(n%i==0) {
k=1;
}
}
if(k==0) {
printf("Number is prime");
==========================
#include<stdio.h>
void smallnum(int[],int);
void main(){
int i, arr[10];
printf("Enter 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d", &arr[i]);
smallnum(arr, 10);
int i;
int min=arr1[0];
for(i=0;i<n;i++) {
min=arr1[i];
The body of do...while loop is executed at least once. Only then, the test expression is evaluated.
#include<stdio.h>
void main()
int num;
char rep;
do
scanf("%d", &num);
continue;
rep = getche();
printf("\n");
#include<stdio.h>
int main()
scanf("%i", &num[i]);
temp = num[j];
num[j] = num[i];
num[i] = temp;
}
else{
continue;
#include<stdio.h>
main(){
int ctr;
ctr = 0;
do{
character = getchar();
sentence[ctr] = character;
ctr++;
} while(character != '\n');
sentence[ctr] = '\0';
==========
#include<stdio.h>
main()
int num,i,j,k;
scanf("%d",&num);
printf(" ");
for(j=1;j<=2*i;j++)
printf("*");
printf("\n");
for(i=num-1; i>0;i--)
for(k=1;k<=num-i;k++){
printf(" ");
for(j=1;j<=2*i;j++)
{
printf("*");
printf("\n");
Functions in C:- (C function is a self-contained block of statements that can be executed repeatedly
whenever we need it.)
In large programs, debugging and editing tasks is easy with the use of functions.
a)Built-in(Library) Functions:
The system provided these functions and stored in the library. Therefore it is also called Library
Functions.
To use these functions, you just need to include the appropriate C header files.
These functions are defined by the user at the time of writing the program.
Part of Function:
3)Function Call
Function Prototype:
syntax -
Example-
int addition();
Function Definition:-
Syntax -
Example:-
int addition()
#include<stdio.h>
int addition();
int main()
int answer;
answer = addition();
return 0;
int addition()
return num1+num2;
-----------------------------------------------------------------------
Description:-
New memory area created for the passed parameters, can be used only within the function.
Description:-
1) Call by value
#include<stdio.h>
int main(){
int answer;
int num2 = 5;
answer = addition(num1,num2);
printf("%d\n""%d\n",num1,num2);
printf("%d\n""%d\n",num1,num2);
return 0;
num1=55;
num2=33;
}
-----------------------------------------------
Call by Reference:-
#include<stdio.h>
int main()
int answer;
int num2 = 5;
printf("%d\n""%d\n",num1,num2);
answer = addition(&num1,&num2);
printf("%d\n""%d\n",num1,num2);
return 0;
*a=33;
*b=22;
return *a + *b;
}
------------------------------
#include <stdio.h>
void checkPrimeNumber();
int main()
checkPrimeNumber();
return 0;
void checkPrimeNumber()
int n, i, flag=0;
scanf("%d",&n);
if(n%i == 0)
flag = 1;
if (flag == 1)
else
-------------------------
#include <stdio.h>
int getInteger();
int main()
int n, i, flag = 0;
n = getInteger();
if(n%i==0){
flag = 1;
break;
}
}
if (flag == 1)
else
return 0;
int getInteger()
int n;
scanf("%d",&n);
return n;
----------------------
#include <stdio.h>
int main()
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
checkPrimeAndDisplay(n);
return 0;
void checkPrimeAndDisplay(int n)
int i, flag = 0;
if(n%i == 0){
flag = 1;
break;
if(flag == 1)
else
------------------------------
#include <stdio.h>
int checkPrimeNumber(int n);
int main()
int n, flag;
scanf("%d",&n);
flag = checkPrimeNumber(n);
if(flag == 1)
else
return 0;
int checkPrimeNumber(int n)
int i;
if(n%i == 0)
return 1;
return 0;
}
C Pointers
The pointer in C language is a variable which stores the address of another variable.
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc.
and used with arrays, structures, and functions.
3) It makes you able to access any memory location in the computer's memory.
int n = 10;
int *p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.
#include<stdio.h>
main(){
int number=50;
int *p;
p=&number;
=====================================
#include<stdio.h>
int main(){
int number=50;
return 0;
===================================
Pointer Program to swap two numbers without using the 3rd variable.
#include<stdio.h>
int main(){
int a=10,b=20,*p1=&a,*p2=&b;
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
return 0;
===================================
#include<stdio.h>
main ()
{
int a = 10;
int *p;
int **pp;
printf("value stored at p: %d\n",*p); // value stoted at the address contained by p i.e. 10 will be
printed
printf("value stored at pp: %d\n",**pp); // value stored at the address contained by the pointer
stoyred at pp
===================================
=============================Files in C:-
C files I/O functions handle data on a secondary storage device, such as a hard disk.
syntax:-
2)w - Opens a text file for writing if the file doesn't exist then a new file is created.
3)a - Opens a text file for appending(writing at the end of existing file) and create the file if it
does not exist.
5)w+ - Open for reading and writing and create the file if it does not exist. If the file exists then
make it blank.
6)a+ - Open for reading and appending and create the file if it does not exist. The reading will start
from the beginning, but writing can only be appended.
Example.1
#include<stdio.h>
int main()
FILE *fp;
fp = fopen("fileName.txt","w");
return 0;
Ex.2
#include<stdio.h>
main() {
FILE *fp;
fp = fopen("test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");//fprintf() write data into the file
fclose(fp);
Ex.3
#include<stdio.h>
int main( )
FILE *fp ;
char ch ;
fp = fopen("test.txt","r") ;
while ( 1 )
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
printf("%c",ch) ;
fclose (fp ) ;
Ex.5
#include<stdio.h>
int main(){
FILE *fp;
char text[300];
fp=fopen("myfile2.txt","r");
printf("%s",fgets(text,200,fp));
fclose(fp);
C++ PROGRAMMING
C++ Polymorphism
In C++, we can use two functions having the same name if they have different parameters (either
types or number of arguments).
#include <iostream>
int main() {
return 0;
-------------------
We cannot use operator overloading for basic types such as int, double, etc.
Operator overloading is basically function overloading, where different operator functions have the
same symbol but different operands.
#include <iostream>
class Count {
private:
int value;
public:
Count() : value(5) {}
value = value + 1;
void display() {
};
int main() {
Count count1;
++count1;
count1.display();
return 0;
------------------------------
In C++ inheritance, we can have the same function in the base class as well as its derived classes.
#include <iostream>
class Base {
public:
};
class Derived : public Base {
public:
void print() {
};
int main() {
Derived derived1;
derived1.print();
return 0;
---------------------------------------------
In C++, we may not be able to override functions if we use a pointer of the base class to point to an
object of the derived class.
Using virtual functions in the base class ensures that the function can be overridden in these cases.
#include <iostream>
public:
};
public:
void print() {
};
int main() {
Derived derived1;
base1->print();
return 0;
Scope Resolution:-
#include<iostream>
int x; // Global x
int main() {
return 0;
---------------------------------------------------------------------
Ex.2
#include<iostream>
class A {
public:
// Only declaration
void fun();
};
void A::fun() {
int main() {
A a;
a.fun();
return 0;
It is used to tell the compiler to perform dynamic linkage or late binding on the function.
There is a necessity to use the single pointer to refer to all the objects of the different classes.
When the function is made virtual, C++ determines which function is to be invoked at the runtime
based on the type of the object pointed by the base class pointer.
5) A virtual function must be defined in the base class, even though it is not used.
6) The prototypes of a virtual function of the base class and all the derived classes must be identical.
If the two functions with the same name but different prototypes, C++ will consider them as the
overloaded functions.
#include<iostream>
class base {
public:
void show()
};
public:
void print()
void show()
};
int main()
base *bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
return 0;
C++ Files
* To use the fstream library, include both the standard <iostream> AND the <fstream> header file:
Example
#include <iostream>
#include <fstream>
To create a file, use either the ofstream or fstream object, and specify the name of the file.
Example 1
#include <iostream>
#include <fstream>
int main() {
ofstream MyFile("e://ajai333.txt");
MyFile.close();
===================
#include <iostream>
#include <fstream>
ofstream MyFile;
MyFile.open("e://ajai333.txt", ios::app);
-----------------------------------------------------------
Example 2
#include <iostream>
#include <fstream>
int main() {
string myText;
ifstream MyReadFile("D://filename.txt");
MyReadFile.close();
------------------------------------------------------
There are some mode flags used for file opening. These are:
1). ios::app: append mode
2). ios::ate: open a file in this mode for output and read/write controlling to the end of the file
5). ios::trunk: when any file already exists, its contents will be truncated before file opening
/* File Handling with C++ using ifstream & ofstream class object*/
#include <iostream>
fstream classes */
#include <fstream>
#include <iostream>
int main() {
ofstream fout;
string line;
while (fout) {
getline(cin, line);
if (line == "-1")
break;
fout.close();
ifstream fin;
fin.open("d://sample.txt");
while (fin) {
getline(fin, line);
fin.close();
return 0;
=========================================================
========================================================
Exception Handling:-
Logical errors
Syntactic errors
An exception is a problem that arises during the execution of a program. A C++ exception is a
response to an exceptional circumstance that arises while a program is running, such as an attempt to
divide by zero.
throw - A program throws an exception when a problem shows up. This is done using a throw
keyword.
catch - A program catches an exception with an exception handler at the place in a program where
you want to handle the problem. The catch keyword indicates the catching of an exception.
try - A try block identifies a block of code for which particular exceptions will be activated. It's
followed by one or more catch blocks.
Ex.1
#include<iostream>
int main()
try {
throw 6;
catch (int a) {
Ex.2
#include<iostream>
if (var2 == 0) {
cout<<var1/var2;
int main()
int a = 30;
int b = 0;
double d = 0;
try {
division(a, b);
return 0;
===============
#include<iostream>
using namespace std;
if (var2 == 0) {
throw 22;
cout<<var1 / var2;
int main()
int a = 30;
int b = 0;
try {
division(a, b);
catch (...) {
return 0;