Unit5 C Program
Unit5 C Program
Unit5 C Program
Pointers:
Pointer is a derived type in C. Pointer is a variable it has the ability to store the address of another
variable of that type. Size of the pointer variable is two bytes. Pointer variable declaration in very similar
to normal variable declaration, but asteric (*) is precede in variable name.
Syntax:
datatype *var1,*var2,….,*varn;
Ex:
int *ip;
float *fp;
Accessing address of a variable:
We can get the address of a variable using the operator &. The operator & immediately preceding a
variable return the address of the variable associated with it.
Ex:
int quantity,*p;
p=&quantity;
in the above example address of the variable quantity will be stored in the pointer variable P.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,*p;
clrscr();
x=25;
p=&x;
printf("\n Value of x is : %d",x);
printf("\n Address of x is : %u",p);
MS Page 1
UNIT – V Programming in C
Output:
Value of x is : 25
Address of x is : 65524
Value of x is : 25
Advantages of pointers:
Pointers are more efficient in handling arrays.
With the help of pointer a function can return multiple values.
Chain of pointers:
It is possible to make a pointer to point to another pointer, thus creating a chain of pointers. Here,
the pointer variable p2 contains the address of the pointer variable p1.
A variable that is a pointer to a pointer must be declared using additional indirection operator in
front of the name.
Ex:
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int x=15,*p1,**p2;
MS Page 2
UNIT – V Programming in C
clrscr();
p1=&x; /* address of x */
p2=&p1; /* address of pointer p1 */
Output:
Value of x is : 15
Address of x is : 65524
Value of x is : 15
Address of p1 is : 65522
Address of x is : 65524
Value of x is : 15
Pointer Expression:
Like other variables, pointer variables can be used in expression. This kind of expression is called
pointer expression.
int x[5]={2,7,9,3,8};
MS Page 3
UNIT – V Programming in C
Suppose the base address of x is 100 and assuming that each integer requires two bytes. The five elements
will be stored as follows.
The name X can be defined as a constant pointer pointing to the first element x[0], and therefore the
value of x is 100. It can be written as follows,
X=&X[0]=100
If we declare P as an integer pointer, then we can make the pointer P to point the array X by the
following assignment.
P= X;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]={2,7,9,3,8};
int *p,i;
clrscr();
for(i=0;i<5;i++)
printf("\n x[%d] %d %u",i, *(p+i),(p+i));
MS Page 4
UNIT – V Programming in C
getch();
}
Output:
Your values are :
Arrays of pointers:
We can also have array of pointers. Pointers are very helpful in handling character array with rows
of variable length.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char *location[20]={"Chennai", "Delhi","Pune","Mumbai"}; /* Array of pointers */
int i;
clrscr();
MS Page 5
UNIT – V Programming in C
Output:
Your cities are :
Chennai
Delhi
Pune
Mumbai
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter your a and b values :\n");
scanf("%d%d",&a,&b);
printf("\nBefore swapping:\n");
printf("a = %d b = %d",a,b);
MS Page 6
UNIT – V Programming in C
int k;
k=*p;
*p=*q;
*q=k;
}
Output:
Enter your a and b values :
20
40
Before swapping:
a = 20 b = 40
After Swapping:
a = 40 b = 20
Sending the value of the argument Sending the address of the argument
It is the duplicate of the actual argument It is the synonym of the actual argument
Changes of formal arguments will not affect the Changes of formal arguments will affect the actual
actual arguments. arguments.
Write a C program to get item code, price and name and increase its price by Rs. 10
MS Page 7
UNIT – V Programming in C
#include<stdio.h>
#include<conio.h>
struct item
{
int icode;
float price;
char name[25];
};
void inc(struct item *);
void main()
{
struct item x;
clrscr();
printf("Enter your icode, price and name :\n");
scanf("%d%f%s",&x.icode,&x.price,x.name);
MS Page 8
UNIT – V Programming in C
p->price=p->price+10;
}
Output:
Enter your icode, price and name :
107
27.50
LuxSoap
File:
A file represents a sequence of bytes on the disk where a group of related data is stored. File is
created for permanent storage of data. It is a readymade structure.
Operations on File:
Naming a file
Opening a file
Reading from a file
Writing to a file
MS Page 9
UNIT – V Programming in C
Closing a file
For store and retrieve information in a file we need to create a FILE pointer. In C language we can
create FILE pointer as follows
Syntax:
FILE *fptr;
Ex:
FILE *fp;
Opening a file:
The fopen() function is used to create a new file or to open an existing file.
Syntax:
Here filename is the name of the file to be opened and mode specifies the purpose of opening the file.
Ex:
fp=fopen(“myfile.txt”,”w”);
File opening mode specifies the purpose of file to be opened. C language supports the following file
opening modes.
mode Description
MS Page 10
UNIT – V Programming in C
Closing a file:
Syntax :
fclose(fptr );
Ex:
fclose(fp);
Write a C program to open a text file abc and close it.
#include<stdio.h>
MS Page 11
UNIT – V Programming in C
#include<conio.h>
void main()
{
FILE *fp; /* creating file pointer */
clrscr();
fp=fopen("abc.txt","w"); /* opening a file */
if(fp==NULL)
{
}
Input and output operations on Files:
putc():
Syntax:
putc(charvar,fptr);
Ex:
putc(c,fp);
This function will write a character in the character variable c to the file, opened by the file pointer fp.
Example:
MS Page 12
UNIT – V Programming in C
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("xyz.txt","w");
if(fp==NULL)
{
printf("\nThis file can not be open");
getch();
exit(3);
}
Output:
s
a
t
h
i
s
MS Page 13
UNIT – V Programming in C
h
@
getc():
Syntax:
charvar=getc(fptr);
Ex:
c=getc(fp);
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("xyz.txt","r");
if(fp==NULL)
{
MS Page 14
UNIT – V Programming in C
while((c=getc(fp))!=EOF)
printf("%c",c);
fclose(fp);
getch();
}
Output:
s
a
t
h
i
s
h
putw(integerval,fptr);
integervar=getw(fptr);
fprintf()
Syntax:
fprintf(fptr,”controlstring”,variablelist);
Ex:
MS Page 15
UNIT – V Programming in C
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int no;
float sal;
char name[30],ch='y';
clrscr();
fp=fopen("edata.dat","w");
if(fp==NULL)
{
printf("The file cannot be open");
getch();
exit(9);
}
while(ch=='y')
{
printf("Enter your no,name and salary :\n");
scanf("%d %s %f",&no,name,&sal);
fprintf(fp," %d %s %f \n",no,name,sal);
printf("Do you want to continue[y/n] :");
scanf(" %c",&ch);
MS Page 16
UNIT – V Programming in C
}
fclose(fp);
}
fscanf():
MS Page 17