Professional Documents
Culture Documents
C Tut
C Tut
C Tut
Function calling itself is call recursive function. i.e., calling a function from the definition of a same
function is called recursion.
Ex:
Main()
{
printf(“Start”);
Main();
Printf(“End”);
Process:
Generally to execute functions in the application, the memory will be allocated. That memory is
called stack memory. To execute a method or block some amount of memory will be allocated inside
the stack that is called frame.
#include<stdio.h>
#include<conio.h>
Void main()
{
int n, res;
Printf(“Enter the no”);
Scanf(“%d”,&n);
Res=fact(n);
Printf(“factorial od n is:%d”,res);
}
Int fact(int n)
{
If(n==0)
{
Res=1;
}
Else
{
Res=n*fact(n-1);
}
Return res;
}
Arrays
Using an array we can n number of element but same data types, that is only homogeneous data
elements allowed an arrays either only int or double or float or etc.,
Syntax;
Int a[5];
Array variable always holds the base address of the memory block.
Local Declaration:
Int arr[5]={10,20,30,34,4}; 10 20 30 34 4
Int arr[5]={10,20 }; 10 20 0 0 0
Global Declaration:
Int arr[5]; 0 0 0 0 0
String
Ex: char[]
String is one dimensional character array.
Syntax:
Char name[30];
We can store only 29 characters because the last character is the null character by default.
Ex:
Ex:
Char name1[3]={‘o’,’n’,’e’);
Ex:
Void main()
{ Enter the name:
Char name[20];
Printf(“enter the name”); Hari Krishna
Scanf(“%s”,name);
%s %s
// gets(name);
Printf(“the name is: %s”,name);
}
Structure
Structure is a user defined data type & using structure we can define a data type which holds more than
one element of different types.
Syntax:
Struct <identity>
};
Ex:
Struct Student
Int roll_no;
Char name;
Float percentage;
};
Array VS structure
Int roll_no;
Char name[20];
Float percentage;
};
Void main()
Struct Student s;
.
Structure variables we can access with the help of accessor ( ) .
Ex:
e.roll_no;
e.name;
e.percentage;
Notes:
It will take either variable or data type as an argument, it will return the size in the form of integer.
What type of structure we should declare as a local & What type of structure we should declare as a
Global?
The declaration of structure outside to all the functions is called global structure.
Ex:
Local Global
Void main() Struct Global;
{ {
Struct Local; Int a,b;
{ };
Int a,b;
}; main ()
Struct Local L; {
} Struct Global G;
Check () }
{ Check ()
{
Struct Local L; Struct Global G;
}
}
How to initialize the structure & access the elements?
{
Int roll_no;
Char name[20];
Float percentage;
};
Void main()
{
Struct Student s ={ 1001,”Sai”,88.4};
Printf(“students Details \n);
Printf(“roll number :%d\n”,s.roll_no);
Printf(“roll number :%s\n”,s.name);
Printf(“roll number :%f\n”,s.percentage);
}
Note:
In which order we define the element to the structure, in the same order we should assign the values.
Array of structures
Struct Student (data type)
{
Int roll_no;
Char name[20];
Float percentage;
};
Void main()
{
Struct Student s1, s2,s3 ( instead of ) Struct Student s[3];
S[2] 3000
0 1 2 4000
2000 3000 4000
Ex:
Array of structures
Struct Student (data type)
{
Int roll_no;
Char name[20];
Int marks[5];
};
Void main()
{
Struct Student s[3];
}
0 1 2 3 4
Roll No Name Marks
S[0] S[0]. S[0]. S[0]. S[0]. S[0].
S[0].roll_ S[0].name
5000 sm[0] sm[1] sm[2] sm[3] sm[4]
no
5000
2000
S[1]
6000
6000
S[2] 3000
7000
7000
0 1 2 4000
Unions
Union is a user defined data type & using structure we can define a data type which holds more than
one element of different types.
Union Student (data type)
{
Int roll_no;
short s;
Roll_no s percentage
float percentage;
};
Void main() Totally allocated 4 bytes
{
Union Student s;
}
Ex:
Union un
{ a=10 10
Short a;
Short b; Totally allocated 2 bytes
};
Void main()
{
Union un u;
u.a=10; after assigned try to print b? and see the output.
Printf(“%d”,b); 10
u.b=20; after assigned try to print a? and see the output.
Printf(“%d”,b); 20
Printf(“%d”,a); 20
}
Pointers