Introduction to Computer
Introduction to Computer
Charles Babbage
Component of a Computer
Input Devices/Units: used to provide data and
control signals to an information processing system
(CPU)
Memory units: The Data storage is the
recording of information/data in a storage device
The purpose of the CPU is to process data get it
from input device and send result to output device
A flowchart helps to clarify how things are currently working and showed in pictorial manner for better understanding
Benefits of the flowchart: Makes Logic Clear, Communication, Effective Analysis, Useful in Coding, Proper Testing and Debugging
Limitations of the flowchart : Complex, Difficult to Modify, No Update.
Pseudo code: is a mixture of a natural
language and programming language
• Initialize: SET,INIT
• Input: READ,GET,OBTAIN
• Process: COMPUTE, CALCULATE, DETERMINE
• Decision: IF, IF…ELSE,IF…ELSEIF…ELSE,SWITCH
• Looping: FOR, WHILE,UNTIL ,REPEAT
• Output: PRINT, DISPLAY, SHOW,WRITE
Program
s
A program consists of a series of instructions that a computer process
to perform the required operation.
Flexibility: it allows to add new features without changing the existing module
Documentation: It helps the user to fully utilize the functionality of the application
Problem Definition: Sum of two numbers
Flowchart
Algorithm
Step 1 : START
Step 2 : Declare the variables a & b
Step 3: Read the value of variables a & b
Step 4 : Calculate the sum of a & b and Store it into C
Program
Step 5: Print the value of C #include<stdio.h>
Step 6 : STOP int main ()
{
int a, b, c;
Pseudo Code: printf ("\n Enter the First Number a : ");
scanf ("%d",&a);
READ a,b printf ("\n Enter the Second Number b : ");
DETERMINE c=a+b scanf ("%d",&b);
c=a+b;
PRINT c printf (" Sum is %d“,c);
return 0;
}
Exchange of two variables
2. Algorithm Description
1. Algorithm Development
1. Read a and b
2. Assign t=a, a=b and b=t
3. print a and b
3. Design Consideration
• The use of an intermediate temporary
variable allows the exchange of two
variables to proceed correctly.
4. Applications
• Currency Exchange
• Sorting Problem
Insertion Sort
Merge Sort
Count the students – getting pass
mark
Counting problem (count the pass marks if mark > =50 )
1. Read N
2. Assign i=1, counter=0
3. while i <= N do
3.1 Read the mark
3.2 if mark > = 50 then counter=counter+1
3.3 increment i by 1 i.e i=i+1
Done
4. print counter
Application:
Counting Problems
Example: Literals
int number=90;
int x=071;
int z=Ox1AB;
float=89.7;
char ch=‘y’;
char c[]=“raj”
Variable: The value of the variable is changed during
Identifiers Rules the program execution.
• Identifier has to begin with a Syntax: Data type variable_name = value;
letter or underscore _ Example: int age=20;
age=20; //modified
followed by alphanumeric
Constant: The value of the variable can not be changed
characters i.e fixed
• Cannot use a keyword as Syntax: const datatype constant_name=value;
identifiers. Example: const float PI=3.14;
• It should not contain white Symbolic Constant: using pre-processor directives
space and special characters. Syntax: #define CONSTANT_NAME=value
• case sensitive Example: #define PI=3.14
Enumeration:
• Identifier names are unique. An enumeration type (also called enum) is a data type that
• Identifiers should be written consists of integral constants. (Attaching name to number)
Syntax: enum enumname{const1, const2=value, const3, ... };
in such a way that it is default const1 has the value of 0
meaningful, short, and easy const2 has assigned value
const3 has the value is const2+1 (next value of const2)
to read. Example: enum week{sun,mon,tue,wed,thu,fri,sat}
Input and Output Statement in C : Input-Output simply means to give some
data or information to the computer so that it can perform various types of
actions and produces the desired result, and this result is termed as output
Operators are used to perform operations on variables and values.
#include<stdio.h> #include<stdio.h>
void main() void main() {
{ const volatile int local = 10;
int a,b; printf("Local = %i", local);
printf(“enter the a & b”); int *ptr=(int *)&local;
scanf(“%d%d”,&a,&b); *ptr=100;
printf(“%d”,a+b); printf(" Modified = %i", local);
} }
Note: X=6, and Y=12 binary value respectively X=0110 ,
Y=1100
#include<stdio.h>
#include<stdio.h>
#include<stdio.h> void main()
void main()
void main() {
{
{ int N; int i=1,N;
int i=1,N;
printf(“enter the N ”); printf(“enter the N ”);
printf(“enter the N ”);
scanf(“%d”,&N); scanf(“%d”,&N);
scanf(“%d”,&N);
for(int i=1;i<=N;i++) while(i<=N){
do{
{ printf(“%d”,i);
printf(“%d”,i);
printf(“%d”,i); i++;
i++;
} }
}while(i<=N);
} }
}
Break Statement: Leave the current block that is breaks/terminates the current
Loop. Also used to leave case statement in switch
Continue Statement: It terminates the current iteration and transfer the control to
the next iteration in loop.
Goto Statement: goto statement is used for altering the normal
sequence of program execution by transferring control to some other
part of the program.(Forward/Backward Jump)
#include <stdio.h>
#include <stdio.h>
void main() {
void main()
int sum=0,n;
{
printf("Sum of First N/2 Numbers?");
int num,i=1;
scanf("%d",&n);
printf("Which table to be Print?");
for(int i = 1; i <= n/2 ; i++){
scanf("%d",&num);
sum = sum+i;
table:
if(i == 5) //stop calculation
printf("%d x %d = %d\n",num,i,num*i);
goto addition;
i++;
}
if(i<=10)
addition:
goto table;
printf("%d", sum);
}
}
Storage Class in C: A storage class defines the scope (visibility) and life-time of
variables and/or functions within a C Program.
Global Scope (extern): Can be accessed anywhere in a program
Block Scope / Local Scope: A Block is a set of statements enclosed within left and right braces
({ and } respectively)
• The auto storage class is the default storage class for all local variables
• The static storage class instructs the compiler to keep a local variable in existence during
the life-time of the program instead of creating and destroying it each time it comes into and
goes out of scope.
• The register storage class is used to define local variables that should be stored in a register
instead of RAM
example:
auto int mark=75;
•Predefined or Library functions are already defined in the system libraries. Programmer can
reuse it and helpful to write error free code
#include<stdio.h>
#include<math.h>
void main (){
int x,y;
printf ("enter a number");
scanf ("%d", &x)
y = pow(x,3);
printf("cube = %d", y);
}
•User Defined Functions: These functions must be defined by the
programmer or user.
Function and its Types:
1. Functions with no arguments and no return values.
2. Function with no arguments and return values
3. Functions with arguments and no return values.
4. Functions with arguments and return values
#include<stdio.h>
#include<stdio.h> #include<stdio.h> #include<stdio.h>
int add(int a,int b);
void add(); int add(); void add(int,int);
void main()
void main() void main() void main()
{
{ { {
int a,b;
add(); int r=add(); int a,b;
printf(“enter the a & b”);
} printf(“%d”,r); printf(“enter the a & b”);
scanf(“%d%d”,&a,&b);
void add() } scanf(“%d%d”,&a,&b);
int r=add(a,b);
{ int add() add(a,b);
printf(“%d”,r);
int a,b; { }
}
printf(“enter the a & b”); int a,b;
scanf(“%d%d”,&a,&b); printf(“enter the a & b”); void add(int a, int b)
int add(int a, int b)
printf(“%d”,a+b); scanf(“%d%d”,&a,&b); {
{
} return a+b; printf(“%d”,a+b);
return a+b;
} }
}
In an inline function, a function call is
replaced by the actual program code. Most #include<stdio.h>
of the Inline functions are used for small inline int add(int a,int b){
computations. return a+b;
}
void main()
inline returntype functionname(int a, int {
b) //inline function int a,b;
{ printf(“enter the a & b”);
… scanf(“%d%d”,&a,&b);
return statments; int r=add(a,b);
printf(“%d”,r);
} }
Finding Factorial a Number Using Recursion
Recursive Function:
A Function call itself again
and again to solve the
problem.
ARRAY
An array is a collection of similar(Homogeneous) data elements. The elements of the array are
stored in consecutive memory locations and are referenced by an index
Syntax:
type name[size][][] …={values};
One Dimensional Array
Declaration: type array_name[size];
initialization: array_name={values};
for(i=0;i<n;++i)
if(a[i]==search)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
}
#include<stdio.h>
2D Array: A two dimensional array is specified void main()
{
using two subscripts where one subscript denotes int a[3][3],b[3][3],c[3][3],i,j;
row and the other denotes column printf("Enter values of Matrix A : \n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("Enter values of Matrix B : \n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<3;i++) {
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("Added Matrix\n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
#include<stdio.h>
void main() printf("Matrix Multiplication\n");
{ for(i=0;i<3;i++)
int a[3][3],b[3][3],c[3][3],i,j; {
printf("Enter values of Matrix A : \n"); for(j=0;j<3;j++)
for(i=0;i<3;i++) printf("%d\t",c[i][j]);
for(j=0;j<3;j++) printf("\n");
scanf("%d",&a[i][j]); }
}
printf("Enter values of Matrix B : \n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
3D Array: A three dimensional array is specified #include<stdio.h>
using three subscripts where first subscript denotes void main()
number of 2D arrays, second subscripts denotes row {
int i, j, k;
and the other denotes column int arr[3][3][3];
printf("Enter the Matrix 3D Array Values : \n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
scanf("%d",&a[i][j][k]);
printf(":::3D Array Elements:::\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
printf("%d\t",arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
}
STRING #include <stdio.h>
void main()
String is a collection of characters {
int i = 0, count = 0;
Strings are defined as a null-terminated char s[100];
character array.
printf("Input a string\n");
string is terminated with a special NULL gets(s);
character \0.
while (s[i] != NULL) {
if (s[i] == 'a' || s[i] == 'A' || s[i] == 'e' || s[i] == 'E'
|| s[i] == 'i' || s[i] == 'I' || s[i] =='o' || s[i]=='O'
|| s[i] == 'u' || s[i] == 'U')
count++;
i++;
}
#include<stdio.h>
void main()
{
int c=getchar();
while(c!=EOF)
{
putchar(c);
c=getchar();
}
}
#include <stdio.h> #include <stdio.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <string.h>
void main() int main()
int main()
{ {
{
char s1[30] = "Good Luck"; char s1[10] = "Hello";
char s1[20]="Kamaraj";
char s2[30] = "All of You !"; char s2[10] = "World";
char s2[40]="kamaraj";
strcpy(s1,s2); strcat(s1,s2);
if (strcmp(s1, s2) ==0) //-,+,0 value
printf("String s1 is: %s", s1); printf("string concatenation: %s", s1);
printf("s1 and s2 are equal");
int len=strlen(s1); }
else
printf("Length of s1 is: %d", len);
printf("s1 and s2 are different");
}
#include <stdio.h> }
#include <string.h>
void main()
#include<stdio.h> {
#include<string.h> char s1[10],s2[10];
void main() printf("Enter the string: ");
{ gets(s1);
char s[100]; strcpy(s2,s1);
printf("Enter a string: "); strrev(s2);
scanf("%[^\n]",s); if(!strcmp(s1,s2))
printf("In Lower Case:\n"); printf("string is palindrome");
puts(strlwr(s)); else
} printf("string is not palindrome");
}
#include <stdio.h>
int main() {
char a[10],b[10],rev[10];
int i,j;
printf("Enter the String:"); i=0;
gets(a); while(a[i]!='\0')
printf("Copying a string:"); {
for(i=0;a[i]!=NULL;i++) if(a[i]>='A' && a[i]<='Z')
b[i]=a[i];
a[i]=a[i]+32;
b[i]='\0';
puts(b); i++;
printf("length is %d",i); }
j=0; printf("Lower case is");
for(i=i-1;i>=0;i--) puts(a);
rev[j++]=a[i];
return 0;
rev[j]='\0';
printf("\nReversed String "); }
puts(rev);
Arrays of Strings #include <stdio.h>
int main ()
{
char s[5][10];
• Strings of Strings or an Arrays of Strings would store another individual string.
printf ("Enter the string");
for (int i = 0; i < 5; i++)
Syntax: char str_name[row_size][column_size];
scanf ("%s", s[i]);
row_size specifes the total number of strings column_size
printf ("Display String");
specifies length of the individual strings for (int i = 0; i < 5; i++)
printf ("\n%s", s[i]);
return 0;
}
Pointer
A pointer is a variable that stores the memory Pointer to Pointer: stores address of another pointer variable
address of another variable
Syntax: type *ptr=&variablename;
Example
int a=10;
int *p=&a; // stores the address of a
Benefits:
• Enables us to access a variable that is defined
outside the function. #include<stdio.h>
• Can be used to pass information back and void main()
{
forth between a function and its reference int num= 10;
point. int *ptr=#
#include<stdio.h>
• More efficient in handling data tables. printf("%d", *ptr);
} void main()
• Reduces the length and complexity of a {
char *ch = "Hello World";
program. printf("%s", ch);
• Sometimes also increases the execution }
speed.
ARRAY AND POINTER
When an array is declared,
#include <stdio.h>
• The compiler allocates a base address and sufficient amount of
int main ()
storage to contain all the elements of the array in contiguous {
memory locations. char *s[4];
• The base address is the location of the first element (index 0) of printf ("Enter the string");
the array. for (int i = 0; i < 4; i++)
• The compiler also defines the array name as a constant pointer to scanf ("%s", s[i]);
the first element.
printf ("Display String");
x &x[0] 2500 ; for (int i = 0; i < 4; i++)
printf ("\n%s", s[i]);
p = x; and p = &x[0]; are equivalent. return 0;
We can access successive values of x by using p++ }
or p- - to move from one element to another.
Relationship between p and x: *(p+i) x[i] Array of Strings using pointer
p = &x[0] = 2500 char *str[4] = {
p+1 = &x[1] = 2504 "String",
p+2 = &x[2] = 2508 "Topics",
"Pointers",
p+3 = &x[3] = 2512 "World"
p+4 = &x[4] = 2516 };
Pointer Expressions / Arithmetic
Like other variables, pointer variables can be used in expressions. Data Type Scale Factor
If p1 and p2 are two pointers, the following statements are valid: char 1
int 4
sum = *p1 + *p2 ;
float 4
prod = *p1 * *p2 ;
double 8
prod = (*p1) * (*p2) ; If p1 is an integer pointer, then
*p1 = *p1 + 2; p1++
x = *p1 / *p2 + 5 ; will increment the value of p1 by 4.
We have seen that an integer value can be added to or subtracted
from a pointer variable.
int *p1, *p2 ; #include<stdio.h>
void main()
int i, j; {
p1 = p1 + 1 ; int arr[5] = { 1, 2, 3, 4, 5 };
p2 = p1 + j ; int *ptr = arr;
for(int i=0;i<5;i++)
p2++ ; printf("%d",*(ptr+i));
p2 = p2 – (i + j) ; }
In reality, it is not the integer value which is added/subtracted, but
rather the scale factor times the value.
#include<stdio.h> #include<stdio.h>
void swap(int,int); void swap(int *a,int *b);
void main() void main()
{ {
int a,b; int a,b;
printf(“enter the a & b”); printf(“enter the a & b”);
scanf(“%d%d”,&a,&b); scanf(“%d%d”,&a,&b);
printf(“A is %d B is %d”,a,b); printf(“A is %d B is %d”,a,b);
swap(a,b); swap(&a,&b);
printf(“A is %d B is %d”,a,b);
} }
void swap(int a, int b)
{ void swap(int *a, int *b)
int t=a; {
a=b; int t=*a;
b=t; *a=*b;
printf(“A is %d B is %d”,a,b); *b=t;
} }
Function Pointers
1) Unlike normal pointers, a function pointer points to code, #include <stdio.h>
not data. Typically a function pointer stores the start of int add(int,int);
executable code. int main()
{
2) Unlike normal pointers, we do not allocate de-allocate int a,b;
memory using function pointers. int (*ip)(int,int);
3) A function’s name can also be used to get functions’ int result;
printf("Enter the values of a and b : ");
address. For example, in the below program, we have scanf("%d %d",&a,&b);
removed address operator ‘&’ in assignment. We have also ip=add;
changed function call by removing *, the program still works. result=(*ip)(a,b);
printf("Value after addition is : %d",result);
1.Declaration of a function pointer. return 0;
type *fp(type,..); }
float (*fp) (int , int); int add(int a,int b)
{
2. Declaration of function. int c=a+b;
type func(type,..); return c;
float func( int , int ); }
int main()
{
void (*fun_ptr)(int) = fun; // & removed
fun_ptr(10); // * removed
return 0;
}
int main()
#include <stdio.h> {
void add(int a, int b) // fun_ptr_arr is an array of function pointers
{ void (*fun_ptr_arr[])(int, int) = {add, sub, mul};
printf("Addition is %d\n", a+b); unsigned int ch, a = 15, b = 10;
}
void sub(int a, int b) printf("Enter Choice: 0 for add, 1 for sub & 2 for
{ multiply\n");
printf("Subtraction is %d\n", a-b); scanf("%d", &ch);
}
void mul(int a, int b) if (ch > 2) return 0;
{
printf("Multiplication is %d\n", a*b); (*fun_ptr_arr[ch])(a, b);
}
}
A null pointer which is a special pointer value that is known not to point
anywhere. This means that a NULL pointer does not point to any valid memory
address.
To declare a null pointer you may use the predefined constant NULL,
int *ptr = NULL;
GENERIC POINTER
• A generic pointer is pointer variable that has void as its
data type. #include<stdio.h>
void main()
• The generic pointer, can be pointed at variables of any {
data type. int x=10;
• It is declared by writing char ch = ‘A’;
void *gp;
void *ptr; gp = &x;
• You need to cast a void pointer to another kind of printf("\n integer value = %d", *(int*)gp);
pointer before using it. gp = &ch;
printf("\n the character %c", *(char*)gp);
• Generic pointers are used when a pointer has to point to }
data of different types at different times.
Dynamic Memory Allocation is a process in which allocate or
deallocate a block of memory during the run-time of a program.
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
int main() int main()
{ {
int i, n; int i, n;
int *arr; int *arr;
printf("\n Enter the number of elements: "); printf("\n Enter the number of elements: ");
scanf("%d", &n); scanf("%d", &n);
arr = (int*)malloc(n*sizeof(int)); arr = (int*)calloc(n,sizeof(int));
for (i=0; i<n;i++) for (i=0; i<n;i++){
{ printf("\n Enter the value of %d of the array:",i);
printf("\n Enter the value of %d of the array:",i); scanf("%d",&arr[i]);
scanf("%d",&arr[i]); }
} printf("\n The array contains \n");
printf("\n The array contains \n"); for (i=0; i<n;i++)
for (i=0; i<n;i++) printf("%d",arr[i]);
printf("%d",arr[i]); free(arr);
return 0; return 0;
} }
#include <stdio.h>
#include <stdlib.h>
int main() { // rellocating the memory
int *ptr, i , n1, n2; ptr = realloc(ptr, n2 * sizeof(int));
printf("Enter size: "); for (i=0; i<n2;i++)
scanf("%d", &n1); {
ptr = (int*) malloc(n1 * sizeof(int)); printf("\n Enter the value of %d of the
for (i=0; i<n1;i++) array:",i);
{ scanf("%d",&ptr[i]);
printf("\n Enter the value of %d of the }
array:",i); printf("\n The array contains \n");
scanf("%d",&ptr[i]); for (i=0; i<n2;i++)
} printf("%d",ptr[i]);
printf("\n The array contains \n"); free(ptr);
for (i=0; i<n1;i++) return 0;
printf("%d",ptr[i]); }
printf("\nEnter the new size: ");
scanf("%d", &n2);
Structure: A structure is a user defined data type or derived data type
that can be used to group of variables of different types into a single type
• A structure is declared using the keyword
struct followed by a structure name. All the
variables of the structures are declared within
the structure. A structure type is defined by
using the given syntax.
Syntax:
struct struct-name Example struct student
{ {
data_type var-name; int r_no;
data_type var-name; char name[20];
... char course[20];
}; float fees;
};
Methods of Declaring a Structure
Variable 3
1 struct
struct student student
{ int r_no;
{ int r_no; char name[20];
char name[20]; char
char course[20];
course[20]; }; float fees;
float fees; struct student
}student1; student1,student2,student3;
4
2 struct
struct student
{ int r_no;
student
{ int r_no; char name[20];
char name[20]; char
char course[20];
course[20]; } float fees;
float fees;
}student1,student2,student3; struct student
student1={23456,”Priya”,”BCA”,25000.75};
#include<stdio.h> printf(“\n ***STUDENT’S DETAILS ***”);
int main() printf(“\n ROLL No. = %d”, stud1.roll_no);
{ printf(“\n NAME. = %s”, stud1.name);
struct student printf(“\n Fees. = %f”, stud1.fees); printf(“\
{ n DOB. = %s”, stud1.DOB);
int roll_no; }
char
name[80];
float fees;
char DOB[80];
};
Output:
struct student
Enter the roll number: 1234
stud1; Enter the name : Ajay
printf(“\n Enter the roll number : Enter the fees : 34500.78
“); scanf(“%d”, &stud1.roll_no); Enter the DOB :
printf(“\n Enter the name : “); 23.05.1998
scanf(“%s”, stud1.name); ***STUDENT’S DETAILS ***
printf(“\n Enter the fees : “); ROLL No. = 1234
scanf(“%f”, &stud1.fees); NAME. = Ajay
printf(“\n Enter the DOB : ROLL No. = 34500.781250
ROLL No. = 23.05.1998
“); scanf(“%s”, stud1.DOB);
#include<stdio.h
> struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
main()
{ struct student stud1, *ptr_stud1;
Accessing ptr_stud1 = &stud1;
ptr_stud1->r_no = 01;
Structure strcpy(ptr_stud1->name, "Rahul");
strcpy(ptr_stud1->course,
using "BCA"); ptr_stud1->fees = 45000;
Pointer printf("\n DETAILS OF
STUDENT");
printf("\n -");
printf("\n ROLL NUMBER = %d", ptr_stud1->r_no);
printf("\n NAME =%s ", puts(ptr_stud1->name));
printf("\n COURSE = %s", puts(ptr_stud1->course));
} printf("\n FEES = %f", ptr_stud1->fees);
Array of
Structure
emp[0 emp[1
] ]
e
int roll_no; float }
scanf("%f", &arr_student[i].marks);
Program
marks; printf("\n");
}; printf("Name\tRoll no\tMarks\n");
int main() for(i = 0; i < 2; i++ )
{ {
struct student arr_student[2]; printf("%s\t%d\t%.2f\n", arr_student[i].name,
arr_student[i].roll_no,
int i;
arr_student[i].marks);
}
return 0;
#include<stdio.h> Output:
struct address ******
{ Enter employee information Name/ City/ PIN Code/
char Phone Number?
city[20]; int Kani
pin; Madurai
char 600009
999411
phone[14];
7284
Write a program }; Printing the employee information....
to read and struct employee name: Kani
display { City: Madurai
information of an
char name[20]; Pincode: 600009
employee using
structure within a struct address Phone:
structure add; 9994117284
};
printf("Enter
void main () employee information Name/ City/ PIN Code/ Phone Number?\
{ n"); scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin,
emp.add.phone);
struct employee printf("Printing the employee information....\n");
printf("name:
emp; %s\nCity: %s\nPincode: %d\nPhone:
%s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
}
#include<stdio.h>
typedef struct
{
int x;
int
y;
}POINT;
void
Passing display(POI
Structure NT);
void main()
as {
arguments POI
NT
p1 =
{2,
3};
displ
Self Referential Structures: contain
reference to data of
struct Node
its same type
{
int data;
struct Node *next;
};
Dynamic data structures : Structures with pointer members that
refer to the same structure.
• Arrays and other simple variables are allocated at block
entry.
• But dynamic data structures require storage management
routine to explicitly obtain and release memory.
int main()
// A simple C program for traversal of a linked list {
struct Node* head = NULL;
struct Node* second = NULL;
#include <stdio.h> struct Node* third = NULL;
#include <stdlib.h>
// allocate 3 nodes in the heap
struct Node { head = (struct Node*)malloc(sizeof(struct Node));
int data; second = (struct Node*)malloc(sizeof(struct Node));
struct Node* next; third = (struct Node*)malloc(sizeof(struct Node));
};
head->data = 1; // assign data in first node
head->next = second; // Link first node with second
void printList(struct Node* n)
{ second->data = 2; // assign data to second node
while (n != NULL) { second->next = third;
printf(" %d ", n->data);
n = n->next; third->data = 3; // assign data to third node
} third->next = NULL;
}
// Function call
printList(head);
return 0;
}
//Accessing union through pointer:
Syntax :
typedef <existing_name> <alias_name> ;
MACRO is defined with the pre-processor directive.
• Macros are pre-processed which means that all the macros would be
processed before your program compiles
Types of Macro
Object Like Macro Function Like Macro
To add a user-defined library to your program, you need to perform the following steps:
1. Write the code for the library: This involves writing the functions that will be included
in the library.
2. Compile the code: Compile the code for the library into an object file (usually with
filename.o or .obj extension) using the appropriate compiler.
3. Create the library: create a library file (usually with filename. a extension ) from the
object file. The library file contains the compiled code for the functions in the library.
4. Link the library to your program: To use the library in your program, you need to link
the library file to the program at compile time. This can be done using linker scripts.
5. Include the header file: In order to call the functions in the library, you need to
include the header file for the library in your program
Example of how to add a user-defined library in C: (Using Command Prompt in Windows or linux)
ar stands for "archiver", which is a tool used to manage libraries in C
•r: replace existing files in the library
•c: create the library if it does not exist
•s: write an index to the archive, allowing faster access to its contents
•libmylib.a is the name of the library file that will be created,
•mylib.o is the name of the object file that will be included in the library.