0% found this document useful (0 votes)
22 views

Indira Gandhi Delhi Technical University For Women

Uploaded by

Himanshi Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Indira Gandhi Delhi Technical University For Women

Uploaded by

Himanshi Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

INDIRA GANDHI DELHI TECHNICAL

UNIVERSITY FOR WOMEN

Compiler Design
Practical File
BCS - 306

Submitted by: Submitted to:


Name: Himanshi Garg Ravinder M.
Enrolment No: 03801012021 Assistant Professor
Batch: CSE-1, A1 CSE Department
B.Tech., 6th Semester IGDTUW

038_Himanshigarg
S.no. Date Experiment
1. 17.01.24 Write a C program to identify whether a given line is a comment or
not
Write a C program to recognize strings under ‘a*’, ‘a*b+’, ‘abb’
2. 17.01.24
Write a C program to test whether a given identifier is valid or not
3. 23.01.24
Write a C program to simulate lexical analyzer for validating
4. 23.01.24
operators
Write a C program for implementing the functionalities of predictive
5. 30.01.24
parser for the mini language specified in Note 1
Write a C program for constructing LL(1) parsing
6. 13.02.24
Write a C program for construction of recursive descent parsing for
7. 19.02.24
the following grammar
Write a program to design LALR Bottum-up Parser
8. 19.02.24
Write a C program to implement operator precedence parsing
9. 02.04.24
Write a C program to generate machine code from abstract syntax
10. 09.04.24
tree generated by parser. The instruction set specified in Note 2 may
be considered as the target 2

EXPERIMENT NO - 1

038_Himanshigarg
AIM - Write a C program to identify whether a given line is a comment or not
CODE –
#include<stdio.h>
void main() {
char com[30];
printf("HimanshiGarg_038");
int i = 2, a = 0;
printf("\nEnter comment:");
gets(com);
if (com[0] == '/') {
if (com[1] == '/')
printf("\nIt is a comment");
else if (com[1] == '*') {
for (i = 2; i <= 30; i++) {
if (com[i] == '*' && com[i + 1] == '/') {
printf("\nIt is a comment");
a = 1;
break;}
else
continue;}
if (a == 0)
printf("\nIt is not a comment");
}
else
printf("\nIt is not a comment");}
else
printf("\nIt is not a comment");}

OUTPUT –

EXPERIMENT NO - 2

038_Himanshigarg
AIM - Write a C program to recognize strings under ‘a*’, ‘a*b+’, ‘abb’
CODE –
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main() {
char s[20], c;
int state = 0, i = 0;
printf("HimanshiGarg_038");
printf("\nEnter a string:");
gets(s);
while (s[i] != '\0') {
switch (state) {
case 0:
c = s[i++];
if (c == 'a')
state = 1;
else if (c == 'b')
state = 2;
else
state = 6;
break;
case 1:
c = s[i++];
if (c == 'a')
state = 3;
else if (c == 'b')
state = 4;
else
state = 6;
break;
case 2:
c = s[i++];
if (c == 'a')
state = 6;
else if (c == 'b')
state = 2;
else
state = 6;
break;
case 3:
c = s[i++];
if (c == 'a')
state = 3;
else if (c == 'b')
state = 2;
else

038_Himanshigarg
state = 6;
break;
case 4:
c = s[i++];
if (c == 'a')
state = 6;
else if (c == 'b')
state = 5;
else
state = 6;
break;
case 5:
c = s[i++];
if (c == 'a')
state = 6;
else if (c == 'b')
state = 2;
else
state = 6;
break;
case 6:
printf("\n %s is not recognized.", s);
exit(0);
}}
if (state == 1)
printf("\n %s is accepted under rule 'a'", s);
else if ((state == 2) || (state == 4))
printf("\n %s is accepted under rule 'a*b+'", s);
else if (state == 5)
printf("\n %s is accepted under rule 'abb'", s);
getch();
}

OUTPUT –

EXPERIMENT NO - 3

038_Himanshigarg
AIM - Write a C program to test whether a given identifier is valid or not
CODE –
#include<stdio.h>
#include <conio.h>
#include <ctype.h>
void main()
{
printf("HimanshiGarg_03801012021");
char a[10];
int flag, i = 1;
printf("\n Enter an identifier:");
gets(a);
if (isalpha(a[0]))
flag = 1;
else
printf("\n Not a valid identifier");
while (a[i] != '\0')
{
if (!isdigit(a[i]) && !isalpha(a[i]))
{
flag = 0;
break;
}
i++;
}
if (flag == 1)
printf("\n Valid identifier");
getch();
}

OUTPUT –

038_Himanshigarg
EXPERIMENT NO - 4
AIM - Write a C program to simulate lexical analyzer for validating operators
CODE -
#include<stdio.h>

void main() {
printf("HimanshGarg_03801012021");
char s[5];
printf("\nEnter any operator:");
gets(s);

switch(s[0]) {
case '>':
if(s[1] == '=')
printf("\nGreater than or equal");
else
printf("\nGreater than");
break;
case '<':
if(s[1] == '=')
printf("\nLess than or equal");
else
printf("\nLess than");
break;
case '=':
if(s[1] == '=')
printf("\nEqual to");
else
printf("\nAssignment");
break;
case '!':
if(s[1] == '=')
printf("\nNot Equal");
else
printf("\nBit Not");
break;
case '&':
if(s[1] == '&')
printf("\nLogical AND");
else
printf("\nBitwise AND");
break;
case '|':
if(s[1] == '|')
printf("\nLogical OR");
else
printf("\nBitwise OR");

038_Himanshigarg
break;
case '+':
printf("\nAddition");
break;
case '-':
printf("\nSubtraction");
break;
case '*':
printf("\nMultiplication");
break;
case '/':
printf("\nDivision");
break;
case '%':
printf("\nModulus");
break;
default:
printf("\nNot an operator");
}
}

OUTPUT-

038_Himanshigarg
EXPERIMENT NO - 5
AIM - Write a C program for implementing the functionalities of predictive
parser for the mini language specified in Note 1
CODE -
#include<stdio.h>
#include<string.h>
char prol[7][10]={"S","A","A","B","B","C","C"};
char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"};
char prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B->@","C->Cc","C->@"};
char first[7][10]={"abcd","ab","cd","a@","@","c@","@"};
char follow[7][10]={"$","$","$","a$","b$","c$","d$"};
char table[5][6][10];
int numr(char c){
switch(c){
case 'S': return 0;
case 'A': return 1;
case 'B': return 2;
case 'C': return 3;
case 'a': return 0;
case 'b': return 1;
case 'c': return 2;
case 'd': return 3;
case '$': return 4;
}
return(2);
}
void main(){
printf("HimanshiGarg_03801012021");
int i,j,k;
for(i=0;i<5;i++)
for(j=0;j<6;j++)
strcpy(table[i][j]," ");
printf("\nThe following is the predictive parsing table for the following
grammar:\n");
for(i=0;i<7;i++)
printf("%s\n",prod[i]);
printf("\nPredictive parsing table is\n");
fflush(stdin);
for(i=0;i<7;i++){
k=strlen(first[i]);
for(j=0;j<10;j++)
if(first[i][j]!='@')
strcpy(table[numr(prol[i][0])+1][numr(first[i][j])+1],prod[i]);
}
for(i=0;i<7;i++){
if(strlen(pror[i])==1){
if(pror[i][0]=='@'){

038_Himanshigarg
k=strlen(follow[i]);
for(j=0;j<k;j++)
strcpy(table[numr(prol[i][0])+1][numr(follow[i][j])
+1],prod[i]);
}
}
}
strcpy(table[0][0]," ");
strcpy(table[0][1],"a");
strcpy(table[0][2],"b");
strcpy(table[0][3],"c");
strcpy(table[0][4],"d");
strcpy(table[0][5],"$");
strcpy(table[1][0],"S");
strcpy(table[2][0],"A");
strcpy(table[3][0],"B");
strcpy(table[4][0],"C");
printf("\n--------------------------------------------------------\n");
for(i=0;i<5;i++)
for(j=0;j<6;j++){
printf("%-10s",table[i][j]);
if(j==5)
printf("\
n--------------------------------------------------------\n");}}

OUTPUT-

038_Himanshigarg
EXPERIMENT NO - 6
AIM - Write a C program for constructing LL(1) parsing
CODE –
#include<stdio.h>
#include<string.h>
char s[20],stack[20];
void main(){
char m[5][6][3]={"tb"," "," ","tb"," "," "," ","+tb"," ","
","n","n","fc"," "," ","fc"," "," "," ","n","*fc"," a ","n","n","i"," ","
","(e)"," "," "};
intsize[5]
[6]={2,0,0,2,0,0,0,3,0,0,1,1,2,0,0,2,0,0,0,1,3,0,1,1,1,0,0,3,0,0}; int
i,j,k,n,str1,str2;
printf("HimanshiGarg_03801012021");
printf("\n Enter the input string: ");
scanf("%s",s);
strcat(s,"$");
n=strlen(s);
stack[0]='$';
stack[1]='e';
i=1; j=0;
printf("\nStack Input\n");
printf("______\n");
while((stack[i]!='$')&&(s[j]!='$')){
if(stack[i]==s[j]){
i--;
j++; }
switch(stack[i]){
case 'e':
str1=0;
break;
case 'b':
str1=1;
break;
case 't':
str1=2;
break;
case 'c':
str1=3;
break;
case 'f':
str1=4;
break; }
switch(s[j]){
case 'i':
str2=0;

038_Himanshigarg
break;
case '+':
str2=1;
break;
case '*':
str2=2;
break;
case '(':
str2=3;
break;
case ')':
str2=4;
break;
case '$':
str2=5;
break; }
if(m[str1][str2][0]=='\0'){
printf("\nERROR"); }
else if(m[str1][str2][0]=='n')
i--;
else if(m[str1][str2][0]=='i')
stack[i]='i';
else{
for(k=size[str1][str2]-1;k>=0;k--){
stack[i]=m[str1][str2][k];
i++; } i--; }
for(k=0;k<=i;k++)
printf(" %c",stack[k]);
printf(" ");
for(k=j;k<=n;k++)
printf("%c",s[k]);
printf(" \n "); }
printf("\n SUCCESS");}
OUTPUT –

038_Himanshigarg
EXPERIMENT NO - 7
AIM - Write a C program for construction of recursive descent parsing for the
following grammar
CODE –
#include<stdio.h>
#include<conio.h>
#include<string.h>

char input[100];
int i,l;

int E();
int EP();
int T();
int TP();
int F();

void main() {
printf("HimanshiGarg_03801012021");
printf("\nRecursive descent parsing for the following grammar\n");
printf("\nE->TE'\nE'->+TE'/@\nT->FT'\nT'->*FT'/@\nF->(E)/ID\n");
printf("\nEnter the string to be checked:");
gets(input);

i = 0; // Resetting the index before parsing starts

if(E()){
if(input[i]=='\0')
printf("\nString is accepted");
else
printf("\nString is not accepted");

038_Himanshigarg
} else
printf("\nString not accepted");

getch();
}

int E() {
if(T()) {
if(EP())
return 1;
else
return 0;
} else
return 0;
}

int EP() {
if(input[i]=='+'){
i++;
if(T()) {
if(EP())
return 1;
else
return 0;
} else
return 0;
} else
return 1;
}

int T() {
if(F()) {
if(TP())
return 1;
else
return 0;
} else
return 0;
}

int TP() {
if(input[i]=='*') {
i++;
if(F()) {
if(TP())
return 1;
else
return 0;

038_Himanshigarg
} else
return 0;
} else
return 1;
}

int F() {
if(input[i]=='(') {
i++;
if(E()) {
if(input[i]==')') {
i++;
return 1;
} else
return 0;
} else
return 0;
} else if((input[i]>='a' && input[i]<='z') || (input[i]>='A' &&
input[i]<='Z')) {
i++;
return 1;
} else
return 0;
}

OUTPUT –

038_Himanshigarg
EXPERIMENT NO - 8
AIM - Write a program to design LALR Bottum-up Parser
CODE –
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void rep(char t[], int r);
struct action {
char row[6][5];
};
const struct action A[12] = {
{"sf", "emp", "emp", "se", "emp", "emp"},
{"emp", "sg", "emp", "emp", "emp", "acc"},
{"emp", "rc", "sh", "emp", "rc", "rc"},
{"emp", "re", "re", "emp", "re", "re"},
{"sf", "emp", "emp", "se", "emp", "emp"},
{"emp", "rg", "rg", "emp", "rg", "rg"},
{"sf", "emp", "emp", "se", "emp", "emp"},
{"sf", "emp", "emp", "se", "emp", "emp"},
{"emp", "sg", "emp", "emp", "sl", "emp"},
{"emp", "rb", "sh", "emp", "rb", "rb"},
{"emp", "rb", "rd", "emp", "rd", "rd"},
{"emp", "rf", "rf", "emp", "rf", "rf"}
};
struct gotol {

038_Himanshigarg
char r[3][4];
};
const struct gotol G[12] = {
{"b", "c", "d"}, {"emp", "emp", "emp"},
{"emp", "emp", "emp"}, {"emp", "emp", "emp"},
{"i", "c", "d"}, {"emp", "emp", "emp"},
{"emp", "j", "d"}, {"emp", "emp", "k"},
{"emp", "emp", "emp"}, {"emp", "emp", "emp"}
};
char ter[6] = {'i', '+', '*', ')', '(', '$'};
char nter[3] = {'E', 'T', 'F'};
char states[12] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'm', 'j', 'k',
'l'};
char stack[100];
int top = -1;
char temp[10];
struct grammar {
char left;
char right[5];
};
const struct grammar rl[6] = {
{'E', "e+T"}, {'E', "T"}, {'T', "T*F"}, {'T', "F"}, {'F', "(E)"},
{'F', "i"}
};
void push(char *s, int *sp, char item) {
if (*sp == 99)
printf(" stack is full ");
else {
*sp = *sp + 1;
s[*sp] = item;}}
char stacktop(char *s) {
char i;
i = s[top];
return i;}
int ister(char x) {
int i;
for (i = 0; i < 6; i++)
if (x == ter[i])
return i + 1;
return 0;}
int isstate(char p) {
int i;
for (i = 0; i < 12; i++)
if (p == states[i])
return i + 1;
return 0;
}
void isproduct(char x, char p) {

038_Himanshigarg
int k, l;
k = ister(x);
l = isstate(p);
strcpy(temp, A[l - 1].row[k - 1]);
}
int isnter(char x) {
int i;
for (i = 0; i < 3; i++)
if (x == nter[i])
return i + 1;
return 0;}
void error() {
printf(" error in the input ");
exit(0);
}
void isreduce(char x, char p) {
int k, l;
k = isstate(x);
l = isnter(p);
strcpy(temp, G[k - 1].r[l - 1]);}
char pop(char *s, int *sp) {
char item;
if (*sp == -1)
printf(" stack is empty ");
else {
item = s[*sp];
*sp = *sp - 1;
}
return item;}
void printt(char *t, int *p, char inp[], int i) {
int r;
printf("\n");
for (r = 0; r <= *p; r++)
rep(t, r);
printf("\t\t\t");
for (r = i; inp[r] != '\0'; r++)
printf("%c", inp[r]);
}
void rep(char t[], int r) {
char c;
c = t[r];
switch (c) {
case 'a':
printf("0");
break;
case 'b':
printf("1");
break;

038_Himanshigarg
case 'c':
printf("2");
break;
case 'd':
printf("3");
break;
case 'e':
printf("4");
break;
case 'f':
printf("5");
break;
case 'g':
printf("6");
break;
case 'h':
printf("7");
break;
case 'm':
printf("8");
break;
case 'j':
printf("9");
break;
case 'k':
printf("10");
break;
case 'l':
printf("11");
break;
default:
printf("%c", t[r]);
break;}}
void main() {
printf("HimanshiGarg_03801012021\n");
char inp[80], x, p, dl[80], y, bl = 'a';
int i = 0, j, k, l, n, m, c, len;
printf(" Enter the input :");
scanf("%s", inp);
len = strlen(inp);
inp[len] = '$';
inp[len + 1] = '\0';
push(stack, &top, bl);
printf("\n stack \t\t\t input");
printt(stack, &top, inp, i);
do {
x = inp[i];
p = stacktop(stack);

038_Himanshigarg
isproduct(x, p);
if (strcmp(temp, "emp") == 0)
error();
if (strcmp(temp, "acc") == 0)
break;
else {
if (temp[0] == 's') {
push(stack, &top, inp[i]);
push(stack, &top, temp[1]);
i++;
} else {
if (temp[0] == 'r') {
j = isstate(temp[1]);
strcpy(temp, rl[j - 2].right);
dl[0] = rl[j - 2].left;
dl[1] = '\0';
n = strlen(temp);
for (k = 0; k < 2 * n; k++)
pop(stack, &top);
for (m = 0; dl[m] != '\0'; m++)
push(stack, &top, dl[m]);
l = top;
y = stack[l - 1];
isreduce(y, dl[0]);
for (m = 0; temp[m] != '\0'; m++)
push(stack, &top, temp[m]);
}
}
}
printt(stack, &top, inp, i);
} while (inp[i] != '\0');
if (strcmp(temp, "acc") == 0)
printf(" \n accept the input ");
else
printf(" \n do not accept the input ");
getch();
}

OUTPUT –

038_Himanshigarg
EXPERIMENT NO - 9
AIM - Write a C program to implement operator precedence parsing

CODE – #include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main ()
{ printf("HimanshiGarg_03801012021\n");
char stack[20], ip[20], opt[10][10][2], ter[10];
int i, j, k, n, top = 0, col, row;
for (i = 0; i < 10; i++)
{
stack[i] = 0;
ip[i] = 0;
for (j = 0; j < 10; j++)
{
opt[i][j][0] = 0; // Fixed index
opt[i][j][1] = 0; // Fixed index

038_Himanshigarg
} }
printf ("Enter the no.of terminals:");
scanf ("%d", &n);
printf ("\nEnter the terminals:");
scanf ("%s", ter);
printf ("\nEnter the table values:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf ("Enter the value for %c %c:", ter[i], ter[j]);
scanf ("%s", opt[i][j]);
}
}
printf ("\nOPERATOR PRECEDENCE TABLE:\n");
for (i = 0; i < n; i++)
{
printf ("\t%c", ter[i]);
}
printf ("\n");
for (i = 0; i < n; i++)
{
printf ("\n%c", ter[i]);
for (j = 0; j < n; j++)
{
printf ("\t%s", opt[i][j]); // Changed index to 0
} }

stack[top] = '$';
printf ("\nEnter the input string:");
scanf ("%s", ip);
i = 0;
printf ("\nSTACK\t\t\tINPUT STRING\t\t\tACTION\n");
printf ("\n%s\t\t\t%s\t\t\t", stack, ip);
while (i <= strlen (ip)) // Fixed loop condition
{
for (k = 0; k < n; k++)
{
if (stack[top] == ter[k])col = k;
if (ip[i] == ter[k])row = k;
}
if ((stack[top] == '$') && (ip[i] == '$'))
{
printf ("String is accepted");
break;
}
else if ((opt[col][row][0] == '<') || (opt[col][row][0] == '='))
{

038_Himanshigarg
stack[++top] = opt[col][row][0];
stack[++top] = ip[i];
printf ("Shift %c", ip[i]);
i++;
}
else
{
if (opt[col][row][0] == '>')
{
while (stack[top] != '<')
{
--top;
}
top = top - 1;
printf ("Reduce");
}
else
{
printf ("\nString is not accepted");
break;
} }
printf ("\n");
for (k = 0; k <= top; k++)printf ("%c", stack[k]);
printf ("\t\t\t");
for (k = i; k < strlen (ip); k++)printf ("%c", ip[k]);
printf ("\t\t\t");
}
getch (); }

038_Himanshigarg
OUTPUT -

038_Himanshigarg
EXPERIMENT NO - 10
AIM - Write a C program to generate machine code from abstract syntax tree
generated by parser. The instruction set specified in Note 2 may be considered
as the target 2

CODE –
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int label[20];
int no=0;

int check_label(int k)
{
int i;
for(i=0;i<no;i++)
{
if(k==label[i])
return 1;
}
return 0;
}

int main()
{
printf("HimanshiGarg_03801012021");
FILE *fp1,*fp2;
char op[10], ch;
char operand1[8], operand2[8], result[8];
int i=0, j=0;

// Open input and output files


fp1=fopen("int.txt","r");
fp2=fopen("target.txt","w");

if(fp1==NULL || fp2==NULL)
{
printf("\n Error opening the file");
exit(0);
}

// Read input file line by line


while(!feof(fp1))

038_Himanshigarg
{
fprintf(fp2,"\n");
fscanf(fp1,"%s",op);
i++;

// Check for label


if(check_label(i))
fprintf(fp2,"\nlabel#%d",i);

// Parse each operation


if(strcmp(op,"print")==0)
{
fscanf(fp1,"%s",result);
fprintf(fp2,"\n\tOUT %s",result);
}
else if(strcmp(op,"goto")==0)
{
fscanf(fp1,"%s %s",operand1,operand2);
fprintf(fp2,"\n\tJMP %s,label#%s",operand1,operand2);
label[no++]=atoi(operand2);
}
else if(strcmp(op,"[]=")==0)
{
fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n\tSTORE %s[%s],%s",operand1,operand2,result);
}
else if(strcmp(op,"uminus")==0)
{
fscanf(fp1,"%s %s",operand1,result);
fprintf(fp2,"\n\tLOAD -%s,R1",operand1);
fprintf(fp2,"\n\tSTORE R1,%s",result);
}
else // Arithmetic operations
{
switch(op[0])
{
case '*':
fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n\tLOAD %s,R0",operand1);
fprintf(fp2,"\n\tLOAD %s,R1",operand2);
fprintf(fp2,"\n\tMUL R1,R0");
fprintf(fp2,"\n\tSTORE R0,%s",result);
break;
case '+':
fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n\tLOAD %s,R0",operand1);
fprintf(fp2,"\n\tLOAD %s,R1",operand2);
fprintf(fp2,"\n\tADD R1,R0");

038_Himanshigarg
fprintf(fp2,"\n\tSTORE R0,%s",result);
break;
case '-':
fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n\tLOAD %s,R0",operand1);
fprintf(fp2,"\n\tLOAD %s,R1",operand2);
fprintf(fp2,"\n\tSUB R1,R0");
fprintf(fp2,"\n\tSTORE R0,%s",result);
break;
case '/':
fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n\tLOAD %s,R0",operand1);
fprintf(fp2,"\n\tLOAD %s,R1",operand2);
fprintf(fp2,"\n\tDIV R1,R0");
fprintf(fp2,"\n\tSTORE R0,%s",result);
break;
case '=':
fscanf(fp1,"%s %s",operand1,result);
fprintf(fp2,"\n\tSTORE %s %s",operand1,result);
break;
case '>':
j++;
fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n\tLOAD %s,R0",operand1);
fprintf(fp2,"\n\tJGT %s,label#%s",operand2,result);
label[no++]=atoi(result);
break;
case '<':
fscanf(fp1,"%s %s %s",operand1,operand2,result);
fprintf(fp2,"\n\tLOAD %s,R0",operand1);
fprintf(fp2,"\n\tJLT %s,label#%s",operand2,result);
label[no++]=atoi(result);
break;
}
}
}

fclose(fp2);
fclose(fp1);

// Print the contents of target.txt


fp2=fopen("target.txt","r");
if(fp2==NULL)
{
printf("Error opening the file\n");
exit(0);
}

038_Himanshigarg
printf("\nContents of target.txt:\n");
do
{
ch=fgetc(fp2);
printf("%c",ch);
}while(ch!=EOF);

fclose(fp2);

return 0;
}
OUTPUT –

038_Himanshigarg
038_Himanshigarg

You might also like