Professional Documents
Culture Documents
Programming languages like C, C++, Java, .Net, Python… are used to develop
applications.
Note: We can store and process the data affectively only when we store the data in a
proper structure.
List of algorithms:
What all the concepts we use in C to implement algorithms?
1. Functions
2. Arrays
3. Structures
4. Pointers
5. Dynamic Memory Allocation.
Static memory:
• All primitive data types having fixed size of memory.
• Using primitive variables we can allocate fixed memory blocks to store the data.
Static allocation:
• Array can store more than 1 element but of same data type.
• String stores only characters as elements.
• Structure can store different types of data like Emp records, Student records,
Account records and so on.
Dynamic memory:
• Once we allocate the memory, we can increase or decrease the size of memory
block depends on requirement.
• We can allocate memory using pointers.
• Pointer can store a memory block address of any size.
What is Compiler?
• It is a translator(program)
• It will check whether the program is syntactically correct or not.
• If the program follows syntax rules, it converts the source code into machine
code.
• Note that, compiler is not responsible for allocating the memory.
What is Function?
• A block of instructions that performs a task.
• Function takes input(arguments), process the input and generate output.
main():
• C application execution starts with main() function.
• OS invokes main() function automatically.
• Main() function is pre-defined as
int main( )
{
….
Logic
….
return 0;
}
#include<stdio.h>
void fun(void);
int main()
{
printf("Main \n");
return 0;
}
void fun(void)
{
printf("fun \n");
}
#include<stdio.h>
void fun(void);
int main()
{
printf("Main starts\n");
fun();
printf("Main ends \n");
return 0;
}
void fun(void)
{
printf("fun \n");
}
We can define more than 1 function in a program.
We can define functions in any order.
#include<stdio.h>
void m1(void);
void m2(void);
void m2()
{
printf("Say Hi \n");
}
void m1()
{
printf("Say Hello \n");
}
int main()
{
printf("1\n");
m1();
printf("2\n");
m2();
printf("3\n");
return 0;
}
#include<stdio.h>
void m1(void);
void m2(void);
int main()
{
printf("3\n");
m1();
printf("4\n");
m2();
printf("1\n");
return 0;
}
void m1()
{
printf("7 \n");
}
void m2()
{
printf("8 \n");
m1();
printf("5 \n");
}
int main()
{
int choice, a, b, res;
while(1)
{
printf("Arithmetic operations :\n");
printf("1. add \n");
printf("2. subtract \n");
printf("3. multiply \n");
printf("4. quit \n");
case 4 : exit(1);
• Array can store more than one element but of same type.
• Array variable stores address(base) where as primitive variable stores value.
• Array elements store in consecutive locations.
• We can access array elements using their index starts with 0
#include<stdio.h>
int arr[5];
int main()
{
int i;
printf("Array elements are : \n");
for(i=0 ; i<5 ; i++)
{
printf("%d \n", arr[i]);
}
return 0;
}
#include<stdio.h>
int* arr[5];
int main()
{
int i;
printf("Array elements are : \n");
for(i=0 ; i<5 ; i++)
{
printf("%s \n", arr[i]);
}
return 0;
}
#include<stdio.h>
int main()
{
int arr[5] = {10,20}, i;
printf("Array elements are : \n");
for(i=0 ; i<5 ; i++)
{
printf("%d \n", arr[i]);
}
return 0;
}
#include<stdio.h>
void display(int[]);
int main()
{
int a[5] = {10,20,30,40,50};
display(a);
return 0;
}
void display(int x[])
{
int i;
printf("Array elements are : \n");
for(i=0 ; i<5 ; i++)
{
printf("%d \n", x[i]);
}
}
#include<stdio.h>
void modify(int[]);
int main()
{
int a[5] = {10,20,30,40,50}, i;
modify(a);
printf("Array elements are : \n");
for(i=0 ; i<5 ; i++)
{
printf("%d \n", a[i]);
}
return 0;
}
void modify(int x[])
{
int i;
for(i=0 ; i<5 ; i++)
{
x[i]=x[i]-5;
}
}
Strings
#include<stdio.h>
int main()
{
char name[20] = "Annie";
printf("%s \n", name);
return 0;
}
Reading elements into array:
• We must repeat loop to read elements into array.
• We need to specify every location address to read the element of that location.
• As we know the length of array to read elements, we can use loops.
We can read the string directly into array without using loops.
No need to specify the address of each location.
#include<stdio.h>
int main()
{
char name[20];
printf("Enter your name : ");
scanf("%s", name);
printf("Hello %s, welcome to DS class \n", name);
return 0;
}
#include<stdio.h>
int main()
{
char name[20];
printf("Enter your name : ");
scanf("%s", name);
printf("Hello %s, welcome to DS class \n", name);
return 0;
}
Gets():
• Pre-defined function can read multi word strings.
• Stop reading characters into array only when we press enter key.
#include<stdio.h>
int main()
{
char name[20];
printf("Enter your name : ");
gets(name);
printf("Hello %s, welcome to DS class \n", name);
return 0;
}
Output:
Enter your name : sri vatsa
Hello sri vatsa, welcome to DS class
Structures
Syntax :
struct identity
{
datatype ele1;
datatype ele2;
..
..
datatype elen;
};
Examples:
Size of integer:
#include <stdio.h>
int main()
{
int x;
printf("int size : %d \n", sizeof(x));
printf("int size : %d \n", sizeof(int));
return 0;
}
#include <stdio.h>
struct Emp
{
int id;
char name[20];
float salary;
};
int main()
{
struct Emp e = {101, "Hareen", 50000};
printf("Emp id : %d \n", e.id);
printf("Emp name : %s \n", e.name);
printf("Emp salary : %f \n", e.salary);
return 0;
}
#include <stdio.h>
struct Emp
{
int id;
char name[20];
float salary;
};
int main()
{
struct Emp e ;
#include <stdio.h>
struct Emp
{
int id;
char name[20];
float salary;
};
void display(struct Emp);
int main()
{
struct Emp e ;
printf("Enter Emp details : \n");
scanf("%d%s%f", &e.id, e.name, &e.salary);
display(e);
return 0;
}
void display(struct Emp x)
{
printf("Emp details are : \n");
printf("Emp id : %d \n", x.id);
printf("Emp name : %s \n", x.name);
printf("Emp salary : %f \n", x.salary);
return 0;
}
A function can return the structure:
• After processing the data inside the function, it can returns the value.
• A function can return any type of data.
• We must specify the structure type as return type when we return from that
structure.
#include <stdio.h>
struct Emp
{
int id;
char name[20];
float salary;
};
struct Emp read();
int main()
{
struct Emp x;
x = read();
printf("Emp details are : \n");
printf("Emp id : %d \n", x.id);
printf("Emp name : %s \n", x.name);
printf("Emp salary : %f \n", x.salary);
return 0;
}
struct Emp read()
{
struct Emp e;
printf("Enter Emp details : \n");
scanf("%d%s%f", &e.id, e.name, &e.salary);
return e;
}
Pointers
Types of pointers:
• Typed pointers: These pointers can point to specific type of data.
o Int* -> can points to int data only
o Float* -> can points to float data only
o Struct Emp* -> can points to only Emp data.
• Uptyped pointer: This pointer can points to any type of data. It is also called
Generic pointer.
o Void* -> Can points to any type of data.
Call By Value:
• Calling the function by passing values as parameters.
• All parameter values will be copied into arguments(local variables) of called
function.
• The data which is processed in the called function cannot be accessed from the
calling function.
#include <stdio.h>
void modify(int, int);
int main()
{
int a=10, b=20;
modify(a, b);
printf("a value is : %d \n", a);
printf("b value is : %d \n", b);
return 0;
}
void modify(int a , int b)
{
a=a+5;
b=b+5;
}
Call By Reference:
• Calling the function by passing address(reference) as parameter.
• Pointer variables can collect address.
• We need to collect these references into pointer type arguments in called
function.
• When we modify the data in called function that affects calling function
arguments data.
#include <stdio.h>
void modify(int*, int*);
int main()
{
int a=10, b=20;
modify(&a, &b);
printf("a value is : %d \n", a);
printf("b value is : %d \n", b);
return 0;
}
void modify(int* a , int* b)
{
*a = *a+5;
*b = *b+5;
}
Note: Passing array as parameter (or) Passing structure as parameter comes under “Call
by Reference”.
#include <stdio.h>
struct Emp
{
int id;
char name[20];
float salary;
};
void display(struct Emp);
int main()
{
struct Emp e ;
printf("Enter Emp details : \n");
scanf("%d%s%f", &e.id, e.name, &e.salary);
display(e);
return 0;
}
void display(struct Emp x)
{
printf("Emp details are : \n");
printf("Emp id : %d \n", x.id);
printf("Emp name : %s \n", x.name);
printf("Emp salary : %f \n", x.salary);
return 0;
}
Size of Pointer:
• The size of pointer varies based on compiler type.
#include <stdio.h>
struct Emp
{
int id;
char name[20];
float salary;
};
int main()
{
char* cp;
short* sp;
double* dp;
struct Emp* ep;
printf("size of char* : %d \n", sizeof(cp));
printf("size of short* : %d \n", sizeof(sp));
printf("size of double* : %d \n", sizeof(dp));
printf("size of Emp* : %d \n", sizeof(ep));
return 0;
}
Pointer modify:
• Increment and Decrement operators are called Modify operators.
• Modify operators increase or decrease the value of integer by one.
#include <stdio.h>
int main()
{
int x=10, y=20;
printf("x value : %d \n", x);
printf("++x value : %d \n", ++x);
#include <stdio.h>
int main()
{
char ch='g';
short s=10;
float f=2.3;
char* cp=&ch;
short* sp=&s;
float* fp=&f;
return 0;
}
#include <stdio.h>
int main()
{
int arr[5] = {10,20,30,40,50}, i;
Pointer to structure:
• We can create pointers to user defined data types also.
• We access structure elements using dot(.) operator.
#include <stdio.h>
struct Emp
{
int id;
char name[20];
float salary;
};
int main()
{
struct Emp e;
printf("Enter Emp details : \n");
scanf("%d%s%f", &e.id, e.name, &e.salary);
#include <stdio.h>
struct Emp
{
int id;
char name[20];
float salary;
};
int main()
{
struct Emp e;
struct Emp *p = &e;
printf("Enter Emp details : \n");
scanf("%d%s%f", &p->id, p->name, &p->salary);
Malloc():
• Allocate memory to structures dynamically.
• Allocated block address will be assigned to pointer type variable.
• The prototype is..
• Malloc() function allocates size bytes memory and returns base address of
memory block.
• It returns NULL pointer if the memory is not available.
Code:
#include<stdio.h>
#include<stdlib.h>
struct Emp
{
int id;
char name[20];
float salary;
};
int main()
{
struct Emp *p;
p = (struct Emp*)malloc(sizeof(struct Emp));
if(p==NULL)
{
printf("Out of memory \n");
}
else
{
printf("Enter Emp details : \n");
scanf("%d%s%f", &p->id, p->name, &p->salary);
printf("Name : %s \n", p->name);
}
return 0;
}
Calloc():
• It is used to allocate memory to arrays dynamically.
• It is belongs to stdlib.h header file.
• The prototype is:
o void* calloc(size_t n, size_t size);
• After memory allocation, all the locations initializes with default values.
Note: When a pointer is pointing to array, we access elements using addresses not
through indexing. Generally we use arr[i] as *(arr+i)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *arr, n, i;
printf("Enter array size : ");
scanf("%d", &n);
arr = (int*)calloc(n, sizeof(int));
if(arr==NULL)
{
printf("Out of memory \n");
}
else
{
printf("Array default values are : \n");
for(i=0 ; i<n ; i++)
{
printf("%d \n", *(arr+i));
}
}
return 0;
}
Reading elements into array and find the sum of all elements in array:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *arr, n, i, sum=0;
printf("Enter array size : ");
scanf("%d", &n);
arr = (int*)calloc(n, sizeof(int));
if(arr==NULL)
{
printf("Out of memory \n");
}
else
{
printf("Enter %d elements : \n", n);
for(i=0 ; i<n ; i++)
{
scanf("%d", arr+i);
}
for(i=0 ; i<n ; i++)
{
sum = sum + *(arr+i);
}
printf("Sum of array elements : %d \n", sum);
}
return 0;
}