Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions 01_Essential_c_and_cpp/01_basic_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
int main()
{
int a[5]={1,2,3,2,1};
for(int i = 0; i < 5; i++)
{
int x = a[i];
x=x*2;
printf("%d\n", x);//Length of array as each int is of 4bit therefore 4*5=20bit lenth or size
}
return 0;
}
30 changes: 30 additions & 0 deletions 01_Essential_c_and_cpp/02_structure.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<stdio.h>
struct reportcard
{
char name[20];
int roll;
int tot_marks;

};

int main()
{
int i;
struct reportcard r[3];//declartion
for(i=0;i<3;i++)//initialization
{
printf("Name ");
scanf("%s", r[i].name);
printf("roll no ");
scanf("%d", &r[i].roll);
printf("Total Marks ");
scanf("%d", &r[i].tot_marks);
printf("\n");
}
for(i=0;i<3;i++)
{
printf("%s %d %d\n", r[i].name, r[i].roll, r[i].tot_marks);
}

return 0;
}
17 changes: 17 additions & 0 deletions 01_Essential_c_and_cpp/03_pointer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[5]={1,3,5,7,9};
int *p=&a[0],*p2,*p3;//(declaration)
printf("Stack Memory array %d\n",p[2]);//using pointer p as array a
p2=(int *)malloc(5*sizeof(int));//accessing heap memory using pointer in c (Initialization)
p3=(int *)malloc(5 * sizeof(int));//accessing heap memory using pointer in c++
p2[0]=4;
p3[0]=5;
printf("Heap Memory array %d %d \n",p2[0],p3[0]);//(dereferencing)
free(p2);//dealocating heap memory in c(deallocation)
free(p3);//dealocating heap memory in c++
printf("Heap Memory array %d %d ",p2[0],p3[0]);
return 0;
}
10 changes: 10 additions & 0 deletions 01_Essential_c_and_cpp/04_reference.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//reference only for c++
#include<stdio.h>
int main()
{
int a=10;
int *ref=&a;//declaring and initializing reference variable
printf("%d\n",a);
printf("%d", *ref);
return 0;
}
31 changes: 31 additions & 0 deletions 01_Essential_c_and_cpp/05_pointer_to_Structure.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdlib.h>
#include <stdio.h>

struct RECTANGLE
{
int length;
int breath;
/* data */
};

int main()
{
struct RECTANGLE r={5,5};
r.length=10;//editing rectangle value
printf("Structure Rectangle in stack and accessed using variable %d %d\n", r.length, r.breath);

//Now Using Pointer
struct RECTANGLE *p1=&r;
p1->length=2;//could even be (*p).length=2
printf("Structure Rectangle in stack and accessed using pointer %d %d\n", p1->length, p1->breath);

//Now creating object structure rectangle in heap memory using pointer
struct RECTANGLE *p2;//struct can be skipped in c++
p2=(struct RECTANGLE *)malloc(sizeof(struct RECTANGLE));//this is for c++ for c it would be p2=(return datatype *)malloc(size of object need to be created)
//here it would be p2=(struct RECTANGLE *)malloc(sizeof(struct RECTANGLE))
p2->length=14;
p2->breath=15;
printf("Structure Rectangle in heap and accessed using pointer %d %d\n", p2->length, p2->breath);
free(p2);
return 0;
}
20 changes: 20 additions & 0 deletions 01_Essential_c_and_cpp/06_functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>

void func(int x, int *y, int *z)
{
x++;
(*y)++;
(*z)++;
}

int main()
{
int a=5,b=10,c=15;

printf("\nBefore calling function \na = %d\nb = %d\nc = %d\n", a, b, c);

func(a, &b, &c);//here a is call by value ,b is call by address//c is call by refence

printf("\nAfter calling function \na = %d\nb = %d\nc = %d\n", a, b, c);//here a is call by value ,b is call by address//c is call by refence
return 0;
}
46 changes: 46 additions & 0 deletions 01_Essential_c_and_cpp/07_array_as_parameter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>

void funct1(int *a)
{
int i;
for(i=0;i<5;i++)
{
printf("\nfunt1 %d", a[i]);
}
}
void funct2(int a[])
{
int i;
for(i=0;i<5;i++)
{
printf("\nfunt2 %d", a[i]);
}
}
int * funct3(int size)//returning address of an array , created and stored in heap by function
{
int *a,i;
a=(int *)malloc(size * sizeof(int));//or a=(int *)malloc(size of array*size of data type) // creating an array in heap memory
for(i=0;i<size;i++)
{
a[i]=i+1;
}
return a;//returning a pointer which hold an array stored in heapmemory
}
int main()
{

int a[]={0,1,2,3,4};
funct1(&a[0]);//pasing array as parameter to funct function
funct2(a);//another way to pass funct as parameter

//returning an array from function
int sz=6,*ptr,i;
ptr=funct3(sz);
for(i=0;i<sz;i++)
{
printf("\nfunct3 %d", ptr[i]);
}
free(ptr);
return 0;
}
49 changes: 49 additions & 0 deletions 01_Essential_c_and_cpp/08_structure_as_parameter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>

struct RECTANGLE
{
int length;
int breath;
};

void fun1(struct RECTANGLE r1)
{
(r1.length)++;
(r1.breath)++;
}
void fun2(struct RECTANGLE *r1)
{
(r1->length)++;
(r1->breath)++;
}
void fun3(struct RECTANGLE *r1)
{
r1->length++;
r1->breath++;
}
struct RECTANGLE * fun4()
{
struct RECTANGLE *r2;
r2=(struct RECTANGLE *)malloc(sizeof(struct RECTANGLE));// or r2=(struct RECTANGLE *)malloc(sizeof(struct RECTANGLE));
r2->length=1;
r2->breath=2;
return r2;
}
int main()
{
struct RECTANGLE r={10,5};
printf("Before any call %d %d\n", r.length, r.breath);
fun1(r);
printf("After call by value %d %d\n", r.length, r.breath);
fun2(&r);
printf("After call by referece %d %d\n", r.length, r.breath);
fun3(&r);
printf("After call by address %d %d\n", r.length, r.breath);

// creating structure in heap memory and accesing them using function
struct RECTANGLE *ptr=fun4();
printf("After creating structure in heap memory %d %d\n", ptr->length, ptr->breath);// -> is to access struct elements using pointer
free(ptr);
return 0;
}
12 changes: 12 additions & 0 deletions 01_Essential_c_and_cpp/09_01_monolythic_program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//monolytic style of programing
#include <stdio.h>
int main()
{
int l,b;
printf("Enter lenth and breath of rectangle ");
scanf("%d %d", &l, &b);
int area=l*b;
int peri=2*(l+b);
printf("%d is area\n%d is perimeter",area,peri);
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//modular style of programing along with structure
#include <stdio.h>

struct Rectangle
{
int length;
int breath;
};
void initialization(struct Rectangle *r,int l,int b)
{
r->length=l;
r->breath=b;
}
int area(struct Rectangle r1)
{
return r1.length*r1.breath;
}
int peri(struct Rectangle r1)
{
return 2*(r1.length+r1.breath);
}
int main()
{
struct Rectangle r={0,0};
int l,b;
printf("Enter lenth and breath of rectangle ");
scanf("%d %d", &l, &b);
initialization(&r,l,b);
int a=area(r);
int p=peri(r);
printf("%d is area\n%d is perimeter",a,p);
return 0;
}
35 changes: 35 additions & 0 deletions 01_Essential_c_and_cpp/09_03_oop_style.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//object oriented style of programing
#include <stdio.h>

struct Rectangle
{
int length;
int breath;
};

void init_Rectangle(struct Rectangle* self, int l, int b)
{
self->length=l;
self->breath=b;
}
int Rectangle_area(struct Rectangle* self)
{
return self->length * self->breath;
}
int Rectangle_peri(struct Rectangle* self)
{
return 2*(self->length + self->breath);
}

int main()
{
int l,b;
printf("Enter lenth and breath of rectangle ");
scanf("%d %d", &l, &b);
struct Rectangle r;
init_Rectangle(&r, l, b);
int a = Rectangle_area(&r);
int p = Rectangle_peri(&r);
printf("%d is area\n%d is perimeter",a,p);
return 0;
}
66 changes: 66 additions & 0 deletions 01_Essential_c_and_cpp/10_rectangle_class_with_oop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>

struct Rectangle
{
int length;
int breadth;
};

void init_Rectangle_default(struct Rectangle* self)
{
self->length=0;
self->breadth=0;
}
void init_Rectangle(struct Rectangle* self, int l, int b)
{
self->length=l;
self->breadth=b;
}
int Rectangle_area(struct Rectangle* self)
{
return self->length * self->breadth;
}
int Rectangle_perimeter(struct Rectangle* self)
{
return 2*(self->length + self->breadth);
}
void Rectangle_setLength(struct Rectangle* self, int l)
{
self->length=l;
}
void Rectangle_setbreadth(struct Rectangle* self, int b)
{
self->breadth=b;
}
int Rectangle_getLength(struct Rectangle* self)
{
return self->length;
}
int Rectangle_getbreadth(struct Rectangle* self)
{
return self->breadth;
}
void destroy_Rectangle(struct Rectangle* self)
{
printf("Destructor\n");
}

int main()
{
struct Rectangle r;
init_Rectangle(&r, 10, 4);
int a=Rectangle_area(&r);
int p=Rectangle_perimeter(&r);
printf("area = %d perimerter = %d\n",a,p);
Rectangle_setLength(&r, 5);
Rectangle_setbreadth(&r, 4);
a=Rectangle_area(&r);
p=Rectangle_perimeter(&r);
printf("area = %d perimerter = %d\n",a,p);
int l=Rectangle_getLength(&r);
int b=Rectangle_getbreadth(&r);
printf("Length = %d breadth = %d\n",l,b);

destroy_Rectangle(&r);
return 0;
}
Loading