C Tut

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Recursion:

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

Array is an aggregate data type or derived data type.

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];

100 102 104 106 108

The block of memory will be allocated.

An array elements always process the help of index number.

Index number starts with 0 to size-1.

Starting address is the random address.

Array variable always holds the base address of the memory block.

Array variable is called internal pointer variable.

Consider Integer occupies 2 bytes so totally 10byte memory will be allocated.

We can access array elements by using its index number only.

Local Declaration:

Int arr[5]; G.V G.V G.V G.V G.V

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.

If we want to process the information such as names..

Syntax:

Data type indentity[size];

Char name[30];

We can store only 29 characters because the last character is the null character by default.

Ex:

Char name[5] = “hari”; h a r I ‘\0’

100 102 103 104 105

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>

Data type ele 1;

Data type ele 2;

};

Ex:

Struct Student

Int roll_no;

Char name;

Float percentage;

};

Array VS structure

Int arr[5] = {12,2,3,43,43};

Char str[4]= ”one”;


Ex:

Struct Student (data type)

Int roll_no;

Char name[20];

Float percentage;

};

Void main()

Struct Student s;

When we declare structure variable that time memory will be allocated.

Base address is the random address like an array.

Structure variable always holds a base address.

Roll_no name percentage


Totally 26 bytes memory will be allocated

100 - 101 121 122-126

.
Structure variables we can access with the help of accessor ( ) .

Ex:

e.roll_no;

e.name;

e.percentage;

How to find the size of a structure?

Struct Student (data type)


{
Int roll_no;
Char name[20];
Float percentage;
};
Void main()
{
Struct Student s;
Printf(“size of students :%d”,sizeof(s));
Printf(“size of students :%d”, sizeof(struct student));
}

Notes:

It will take either variable or data type as an argument, it will return the size in the form of integer.

Local Structures & Global Structures:

What type of structure we should declare as a local & What type of structure we should declare as a
Global?

The structure definition is common for all is called Global.

The structure definition is specific to a particular function is called local.

The declaration of structure inside a particular function is called local structure.

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?

Struct Student (data type)

{
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];

Roll No Name Percentage


S[0]
S[0].roll_no S[1].name S[2].percentage

2000 - 2001 2002- 2021 2022- 2024


S[1]

S[2] 3000

0 1 2 4000
2000 3000 4000

100 - 101 121 122-126

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

2000 3000 4000

100 - 101 121 122-126

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

You might also like