grammar to recognize declarations of variables, assignment statement & “for” statement as per syntax of C and generate equivalent three address code

, by Prashant Gunjal

####### pr.h #######
struct holder
{
char name[8];
char type[6];
}st[30];


###### pr_6.l ######
%{
#include<stdio.h>
#include "y.tab.h"
#include "pr.h"
char var_type[5];
int var_cnt=0,i;
extern yylval;
%}
type int|float|char
r [-+]?[0-9]+/.?[0-9]*
%%
main {return(mn);}
{type} {
strcpy(var_type,yytext);
return(Type);
}
for {return(FOR);}
[a-z]+[a-z0-9]* {
for(i=0;i<var_cnt;i++)
if(strcmp(st[i].name,yytext)==0)
break;
if(i==var_cnt)
{
strcpy(st[var_cnt].name,yytext);
strcpy(st[var_cnt++].type,var_type);
}
yylval=i;
return(var);
}
{r} {yylval=atoi(yytext);return(num);}
[ \t] {;}
. {return(yytext[0]);}
[\n] {;}
%%


######### pr_6.y ########
/*
5) Write an attributed translation grammar to recognize declarations of variables, assignment statement & “for” statement as per syntax of C and generate equivalent  three address code for the given input made up of constructs mentioned above using LEX and YACC. Write a code to store the identifiers from the input in a symbol table and also to record other relevant information about the identifiers. Display all records stored in the symbol table.
*/
%{
#include<stdio.h>
#include<stdlib.h>
#include "pr.h"
FILE * yyin;
int total_var=0,temp_cnt=0;
char e2_type[6],e1_type[6],e_type[6];
%}
%token mn var Type num FOR

%%
edash : prog {printf("\n\t!!!End of programm!!!!\n");}
;
prog : mn '('')''{'mstmt'}' {;}
;
mstmt : stmt mstmt
|
stmt
;
stmt : dstmt';' {/*printf("\n\tValid Declration Statement");*/}
|
exstmt';' {//printf("\n\tValid Expression Statement");
}
|
dstmt {printf("\n\t**** Error--- Line Not Terminated by ';' ");}
|
exstmt {printf("\n\t**** Error--- Line Not Terminated by ';'");}
|
forloop
;


dstmt : Type varlist
;
varlist : v','varlist
|
v
;
v : var {total_var++;}
| 
var'='num { total_var++;
//Target code
printf("\n\t%s:=%d",st[$1].name,$3);
}
;


exstmt : var'='e {
if(strcmp(e1_type,"int")==0) 
printf("\n\t%s := %d",st[$1].name,$3);
else
printf("\n\t%s:=T%d",st[$1].name,temp_cnt-1);
}
;
e : e '+' e1 {
//target Code
printf("\n\tT%d:=%s+%s",temp_cnt,st[$1].name,st[$3].name);
temp_cnt++;
}
|
e '-' e1 {
//target Code
printf("\n\tT%d:=%s-%s",temp_cnt,st[$1].name,st[$3].name);
temp_cnt++;
}
|
e1 { strcpy(e_type,e1_type);}
;
e1 : e1 '*' e2 {
//target Code
printf("\n\tT%d:=%s*%s",temp_cnt++,st[$1].name,st[$3].name);
}
|
e1 '/' e2 {
//target Code
printf("\n\tT%d:=%s/%s",temp_cnt++,st[$1].name,st[$3].name);
}
|
e2 {strcpy(e1_type,e2_type);}
;
e2 : var {strcpy(e2_type,st[$1].type);}
|
num { strcpy(e2_type,"int");}
;

forloop : FOR '(' exstmt ';' cnd ';' nxt ')' '{' body '}' {printf("\nEnd : ");}
;
cnd : var '<' var {printf("\nStart:\n\tIf %s < %s goto next else goto End",st[$1].name,st[$3].name);}
|
var '<' num {printf("\nStart:\n\tIf %s < %d goto next else goto End",st[$1].name,$3);}
|
var '>' var {printf("\nStart:\n\tIf %s > %s goto next else goto End",st[$1].name,st[$3].name);}
|
var '>' num {printf("\nStart:\n\tIf %s > %d goto next else goto End",st[$1].name,$3);}
|
var '<''=' var {printf("\nStart:\n\tIf %s <= %s goto next else goto End",st[$1].name,st[$4].name);}
|
var '<''=' num {printf("\nStart:\n\tIf %s <= %d goto next else goto End",st[$1].name,$4);}
|
var '>''=' var {printf("\nStart:\n\tIf %s >= %s goto next else goto End",st[$1].name,st[$4].name);}
|
var '>''=' num {printf("\nStart:\n\tIf %s >= %d goto next else goto End",st[$1].name,$4);}
;
body : mstmt   {printf("\n\tgoto Action");}
;
nxt : var'+''+' {printf("\nAction:\n\t%s=%s+1\n\tgoto Start\nnext : ",st[$1].name,st[$1].name);}
|
var'-''-' {printf("\nAction:\n\t%s=%s-1\n\tgoto Start\nnext :",st[$1].name,st[$1].name);}
;
%%

int main()
{
int i;
yyin=fopen("ip.c","r");
printf("\n\t***** SEE 3 ADDR code **** ");
yyparse();
printf("\n\t++++ symbal table ...++++");
for(i=0;i<total_var;i++)
{
printf("\n\t%s\t%s",st[i].name,st[i].type);
}
return(0);
}

void yyerror(char *c)
{
fprintf(stderr,"%s\n",c);
}

######## ip.c #########
main ( )
{
float a=0,b=4,c=5;
int no1 = 0,no2=0;
a=b+c;
no1=no2-a;
for(a=0;a<10;a++)
{
c=b+c;
}
}


read more

attributed translation grammar to recognize declarations of variables,assignment statement with type checking as per syntax of C and generate equivalent three address code

, by Prashant Gunjal

######pr.h#######
struct holder
{
char name[8];
char type[6];
}st[30];



###### pr_5.l #####
%{
#include<stdio.h>
#include "y.tab.h"
#include "pr.h"
char var_type[5];
int var_cnt=0,i;
extern int yylval;
%}
type int|float|char
%%
"main" {return(mn);}
{type} {
strcpy(var_type,yytext);
return(Type);
}
[a-z]+[a-z0-9]* {
for(i=0;i<var_cnt;i++)
if(strcmp(st[i].name,yytext)==0)
break;
if(i==var_cnt)
{
strcpy(st[var_cnt].name,yytext);
strcpy(st[var_cnt++].type,var_type);
}
yylval=i;
return(var);
}
[0-9]+ {yylval=atoi(yytext);return(num);}
[ \t] {;}
. {return(yytext[0]);}
[\n] {;}
%%


##### pr_5.y #####

/*
4) Write an attributed translation grammar to recognize declarations of variables,      
   assignment statement with type checking as per syntax of C and generate equivalent
   three address code for the given input made up of constructs mentioned above using
   LEX and YACC. Write a code to store the identifiers from the input in a symbol table
   and also to record other relevant information about the identifiers. Display all records
   stored in the symbol table.
   Input:  C code snippet
   Output:  a] equivalent three address code
   b] Symbol table with type information
   c] Error handling for type checking
*/
%{
#include<stdio.h>
#include<stdlib.h>
#include "pr.h"
FILE * yyin;
int total_var=0,temp_cnt=0;
char e2_type[6],e1_type[6],e_type[6];
%}
%token mn var Type num

%%
edash : prog {printf("\n\t!!!End of programm!!!!\n");}
;
prog : mn '('')''{'mstmt'}' {;}
;
mstmt : stmt mstmt
|
stmt
|

;
stmt : dstmt';' {/*printf("\n\tValid Declration Statement");*/}
|
exstmt';' {//printf("\n\tValid Expression Statement");
}
|
dstmt {printf("\n\t**** Error--- Line Not Terminated by ';' ");}
|
exstmt {printf("\n\t**** Error--- Line Not Terminated by ';'");}
;
dstmt : Type varlist
;
varlist : v','varlist
|
v
;
v : var {total_var++;}
|
var'='num { total_var++;
//Target code
st[$1].val=$3;
printf("\n\t%S:=%d",st[$1].name,$3);
}
;
exstmt : var'='e {
// This part check types in expression
if(strcmp(st[$$].type,e_type)!=0)
if(strcmp(st[$$].type,"int")==0 && strcmp(e_type,"float")==0)
printf("\n\tPlease check type you may need typecasting in expression");


printf("\n\t%s:=T%d",st[$1].name,temp_cnt-1);
}
;
e : e '+' e1 {
typechecker1();
//target Code
printf("\n\tT%d:=%s+%s",temp_cnt,st[$1].name,st[$3].name);
temp_cnt++;
}
|
e '-' e1 {
typechecker1();
//target Code
printf("\n\tT%d:=%s-%s",temp_cnt,st[$1].name,st[$3].name);
temp_cnt++;
}
|
e1 {strcpy(e_type,e1_type);}
;
e1 : e1 '*' e2 {
typechecker2();
//target Code
printf("\n\tT%d:=%s*%s",temp_cnt++,st[$1].name,st[$3].name);
}
|
e1 '/' e2 {
typechecker2();
//target Code
printf("\n\tT%d:=%s/%s",temp_cnt++,st[$1].name,st[$3].name);
}
|
e2 {strcpy(e1_type,e2_type);}
;
e2 : var {strcpy(e2_type,st[$1].type);}
|
num {strcpy(e2_type,"int");}
;
%%

int main()
{
int i;
yyin=fopen("ip.c","r");
printf("\n\t***** SEE 3 ADDR code **** ");
yyparse();
printf("\n\t++++ symbal table ...++++");
for(i=0;i<total_var;i++)
{
printf("\n\t%s\t%s\t %d",st[i].name,st[i].type,st[i].val);
}
return(0);
}
void typechecker1()
{
// This part check types in expression
if(strcmp(e1_type,e_type)!=0)
{
if((strcmp(e1_type,"int")==0 && strcmp(e_type,"float")==0)||(strcmp(e1_type,"float")==0 && strcmp(e_type,"int")==0))
strcpy(e_type,"float");
else
printf("\n\tPlease check type you may need typecasting in expression");
}
}
void typechecker2()
{
// This part check types in expression
if(strcmp(e1_type,e2_type)!=0)
{
if((strcmp(e1_type,"int")==0 && strcmp(e2_type,"float")==0)||(strcmp(e1_type,"float")==0 && strcmp(e2_type,"int")==0))
strcpy(e1_type,"float");
else
printf("\n\tPlease check type you may need typecasting in expression");
}
}
void yyerror(char *c)
{
fprintf(stderr,"%s\n",c);
}


###### ip.c ######
main ( ) 
{ 
float a=0,b=4,c=5;
int no1 = 0,no2=0;
a=b+c;
a=b*c;
no1=no2-a;
}

read more

Ambiguous CFG to recognize an infix expression and implement a parser that recognizes the infix expression using YACC

, by Prashant Gunjal

###### pr.h #########
struct holder
{
char name[10];
int val;
}st[20];

###### pr_4.l #######

%{
#include<stdio.h>
#include "y.tab.h"
#include "pr.h"
extern int yylval;
int var_cnt=0,i;
%}

%%
[0-9]+ {yylval=atoi(yytext);return(id);}
[a-z]+[a-z0-9]* {
for(i=0;i<var_cnt;i++)
if(strcmp(st[i].name,yytext)==0)
break; //already present variable

yylval=i; // in any case(found/not found)value remain same ie = i

if(i==var_cnt) //indicate new var encounter so insert in struct
{
strcpy(st[var_cnt++].name,yytext);
return(newvar);
}
else
return(oldvar);
}
[\n] {return('\n');}
. {return(yytext[0]);}
%%


####### pr_4.y ########

/*
3) Write an ambiguous CFG to recognize an infix expression and implement a parser that     
     recognizes the infix expression using YACC. Provide the details of all conflicting 
     entries in the parser table generated by LEX and YACC and how they have been 
     resolved.
     Input :  Infix expressions with variables
     Output:  a]Stepwise evaluation of expression, 
        b] Symbol table
        c] explanation of conflicting entries in y.output file
        d] explanation for resolving the ambiguities
*/

%{
#include<stdio.h>
int cnt=0,i;
#include "pr.h"
%}

%token id newvar oldvar
%%
edash : E '\n'
|
edash E '\n'
;
E : E'='E {st[$$].val=$3;$$=$3;}
|
E'+'E {$$=$1+$3;}
|
E'-'E {$$=$1-$3;}
|
E'*'E {$$=$1*$3;}
|
E'/'E {$$=$1/$3;}
|
oldvar {$$=st[$1].val;}
|
newvar {cnt++;}
|
id
;
%%
int main()
{
printf("\nEnter Inputs \n ");
yyparse();
printf("\n\tSymbol table\n\tVariable\tValue");
for(i=0;i<cnt;i++)
printf("\n\t%s\t\t%d\n",st[i].name,st[i].val);

return(0);
}
void yyerror (char *s)
{
fprintf (stderr,"%s", s);
}


read more

Lex and Yacc program for grammar evaluation

, by Prashant Gunjal

 ####### pr_3.l ##########
%{
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}

%%
[0-9]+ {yylval=atoi(yytext);return(id);}
[\n] {return(0);}
. {return(yytext[0]);}
%%

######## pr_3.y #########

%{
#include<stdio.h>
int cnt=0;
%}

%token id
%left '+' '-'
%left '*' '/'

%%
edash : E {printf("\n\t\tAnswer=%d\n",$$);}
;
E : E'+'E {$$=$1+$3;printf("\n\t%d)E->E+E Rule is reduced ---- %d -> %d + %d",cnt++,$$,$1,$3);}
|
E'-'E {$$=$1-$3;printf("\n\t%d)E->E-E Rule is reduced ---- %d -> %d - %d",cnt++,$$,$1,$3);}
|
E'*'E {$$=$1*$3;printf("\n\t%d)E->E*E Rule is reduced ---- %d -> %d * %d",cnt++,$$,$1,$3);}
|
E'/'E {$$=$1/$3;printf("\n\t%d)E->E/E Rule is reduced ---- %d -> %d / %d",cnt++,$$,$1,$3);}
|
id
;
%%
int main()
{
printf("\n** We will see the execution sequence **");
printf("\n\tEnter Input = ");
yyparse();
return(0);
}
void yyerror (char *s)
{
fprintf (stderr,"%s", s);
}

/* OP
** We will see the execution sequence **
Enter Input = 10+5*2-1/1

0)E->E/E Rule is reduced ---- 1 -> 1 / 1
1)E->E-E Rule is reduced ---- 1 -> 2 - 1
2)E->E*E Rule is reduced ---- 5 -> 5 * 1
3)E->E+E Rule is reduced ---- 15 -> 10 + 5
Answer=15
pr@prashant-HP:~/LEX&YACC$ 
*/

read more

Lex program to recognize tokens from C program

, by Prashant Gunjal

%{
#include<stdio.h>
#include<string.h>
#include<malloc.h>
int flag=0,found=0,var_cnt=0,i;
struct holder
{
char name[10];
int val;
}st[20];
%}

nl [\n]+
ws [ \t]+
no [0-9]
kwd main|int|char|float|double|if|while|do|for|void
var [a-zA-Z][a-zA-Z0-9]*
cnst [\-]?[0-9]+[\.]?[0-9]*
ariop [+*\-\/]
ariop1 [+*\-\/]=
relop >|<|<=|>=|!=
ass =
fun main|printf|scanf|strlen|strcpy
prest #
ignore [{}()"",&;]

%%
{nl} {flag=0;}
{ws} {;}
{ignore} {;}
^# {flag=1; fprintf(yyout,"\n\t\tPreprocessor Statement\t\t");}
{kwd} {if(!flag) fprintf(yyout,"\n%s\t\tKEYWORD\t\t\t\t***",yytext);}
{ass} {if(!flag) fprintf(yyout,"\n%s\t\tASSIGNMENT OPERATOR\t\t***",yytext);}
{cnst} {if(!flag) fprintf(yyout,"\n%s\t\tCONSTANT\t\t\t***",yytext);}
{relop} {if(!flag) fprintf(yyout,"\n%s\t\tRELATIONAL OPERATOR\t\t***",yytext);}
{ariop} {if(!flag) fprintf(yyout,"\n%s\t\tARITHMATIC OPERATOR\t\t***",yytext);}
{ariop1} {if(!flag) fprintf(yyout,"\n%s\t\tARITHMATIC OPERATOR\t\t***",yytext);}
{var} { if(!flag)
{ for(i=0;i<var_cnt;i++)
if(strcmp(st[i].name,yytext)==0)
break; //already present variable

if(i==var_cnt) //indicate new var encounter so insert in struct
strcpy(st[var_cnt++].name,yytext);
fprintf(yyout,"\n%s\t\t Variable\t\t(%d)",yytext,i);

}
}
%%

int main()
{
yyin=fopen("a.c","r");
yyout=fopen("out.txt","w");
fprintf(yyout,"\nLexeme\t\tToken\t\tAttribute Value");
yylex();
fclose(yyout);
}
int yywrap()
{
return(1);
}


######input file a.c ##########

#include<stdio.h>
int main()
{
int a=0,b,c;
a=b+c;
a+=1;
a-=2.2;
}

##########################

#### OUTPUT ###########


Lexeme Token Attribute Value
Preprocessor Statement .
int KEYWORD ***
main Variable (0)
int KEYWORD ***
a Variable (1)
= ASSIGNMENT OPERATOR ***
0 CONSTANT ***
b Variable (2)
c Variable (3)
a Variable (1)
= ASSIGNMENT OPERATOR ***
b Variable (2)
+ ARITHMATIC OPERATOR ***
c Variable (3)
a Variable (1)
+= ARITHMATIC OPERATOR ***
1 CONSTANT ***
a Variable (1)
-= ARITHMATIC OPERATOR ***
2.2 CONSTANT ***
read more

Lex programm to recgnize static words from english

, by Prashant Gunjal

%{
#include<stdio.h>
#include<string.h>

struct holder
{
char word[10];
char type[15];
}st[20];

int count=0,i,flag=0;
char type[15];
%}
newln [\n]+
noune [a-zA-Z]+

%%
{newln} {flag=0;}
^verb { flag=1;
strcpy(type,"verb");
}
^adjective {
flag=1;
strcpy(type,"adjective");
}
^preposition {
flag=1;
strcpy(type,"preposition");
}
^adverb {
flag=1;
strcpy(type,"adverb");
}
{noune} {
if(flag)
{
for(i=0;i<count;i++)
if(strcmp(st[i].word,yytext)==0)
break;
if(i==count)
{
strcpy(st[count].type,type);
strcpy(st[count].word,yytext);
count++;
}
}
else
{
for(i=0;i<count;i++)
if(strcmp(st[i].word,yytext)==0)
break;
if(i==count)
{
fprintf(yyout,"\n%s\t\tNOUNE",yytext);
}
else
fprintf(yyout,"\n%s\t\t%s",st[i].word,st[i].type);
}
}
. {;}
%%

int main()
{
yyout=fopen("out2.txt","w");
yylex();
fclose(yyout);
}


read more

Lex programm to recgnize static words from english

, by Prashant Gunjal

/*
Aim : Write a programm to recgnize static words from english
*/
%{
#include<stdio.h>
%}

verb is|are|was|were
adjective good|bad|big|small
preposition this|under|below
adverb happely|gracefully
space [ \n]+
noune [a-zA-Z]+

%%
{verb} {fprintf(yyout,"\n%s\t\tVERB",yytext);}
{adverb} {fprintf(yyout,"\n%s\t\tADVERB",yytext);}
{adjective} {fprintf(yyout,"\n%s\t\tADJECTIVE",yytext);}
{noune} {fprintf(yyout,"\n%s\t\tNOUNE",yytext);}
. {;}
%%

int main()
{
yyout=fopen("out.txt","w");
yylex();
fclose(yyout);
}


read more

Computer Lab I

, by Prashant Gunjal

1)
2)
3)
4)
5)
6)
read more

Find open ports on given Ip

, by Prashant Gunjal

C - program to find open ports on give IP address.

Program :-


#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

int main()
{
char ip[20];
struct sockaddr_in addr;
int st_port,end_port,i,skt,use;

printf("\n\tEnter Ip address : ");
gets(ip);

printf("\n\tEnter starting port : ");
scanf("%d",&st_port);

printf("\n\tEnter End port : ");
scanf("%d",&end_port);

for(i=st_port;i<=end_port;i++)
{
skt=socket(PF_INET,SOCK_STREAM,0);
if(skt<0)
{
printf("\n\tERROR : socket() failed ");
exit(0);
}
addr.sin_family=PF_INET;
addr.sin_port=htons(i);
addr.sin_addr.s_addr=inet_addr(ip);
use=connect(skt,(struct sockaddr *)&addr,sizeof(addr));
close(skt);
if(use!=-1)
{
printf("\n\t%d  is running",i);
}
}
}

read more

IPC (tcp client and server for chatting )

, by Prashant Gunjal

IPC using c port programming
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Server.c


#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>

int main()
{
int skt,bnd,new_skt,i,len;

char msg[100];
struct sockaddr_in addr;
skt=socket(AF_INET,SOCK_STREAM,0);

if(skt>0)
{
addr.sin_family=AF_INET;
addr.sin_port=htons(10000);
addr.sin_addr.s_addr=INADDR_ANY;
printf("\n\t*** Listining ***");
bnd=bind(skt,(struct sockaddr *) &addr,sizeof(addr));
if(bnd==0)
{
listen(skt,3);
len=sizeof(struct sockaddr_in);
new_skt=accept(skt,(struct sockaddr *) &addr,&len);
printf("\n\t*** Accepted client ***");
if(new_skt>0)
{
while(1)
{
for(i=0;i<100;i++)
msg[i]=NULL;
recv(new_skt,msg,100,0);
printf("\nClient : %s",msg);

if(strcmp(msg,"exit")==0)
break;

for(i=0;i<100;i++)
msg[i]=NULL;
printf("\nServer : ");
gets(msg);
send(new_skt,msg,strlen(msg),0);

if(strcmp(msg,"exit")==0)
break;
}
}
else
printf("\n\t**** REFUSED ****");
}
else
printf("**** Binding error ****");
}
else
printf("\n\t****Socket Creation ERROR ****");

close(skt);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
client.c
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>

int main()
{
int skt,con,i;
char ip[10],msg[100];
struct sockaddr_in address;
skt = socket(AF_INET,SOCK_STREAM,0);
if(skt>0)
{
address.sin_family=AF_INET;
address.sin_port=htons(10000);
printf("\n\tEnter Server IP : ");
gets(ip);
inet_pton(AF_INET,ip,&address.sin_addr);
con=connect(skt,(struct sockaddr *) &address,sizeof(address));
if(con==0)
{
while(1)
{
for(i=0;i<100;i++)
msg[i]=NULL;
printf("\nClient : ");
gets(msg);
send(skt,msg,strlen(msg),0);
if(strcmp(msg,"exit")==0)
break;
for(i=0;i<100;i++)
msg[i]=NULL;

recv(skt,msg,100,0);
printf("\nServer : %s",msg);
if(strcmp(msg,"exit")==0)
break;
}
}
else
printf("\n\t*** CON ERROR ****");
}
else
printf("\n****Sock creation err ****");

close(skt);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
read more

Multi-threaded echo Server

, by Prashant Gunjal


Multi-threader echo program using c

Server.c :-
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

int no=0;
void *thread_p(void * args)
{
int sid=*(int*)args,i;
char msg[100];
while(1)
{
printf("\n\t Client is %d and number : %d ",sid,no);
for(i=0;i<100;i++)
msg[i]=NULL;
recv(sid,msg,100,0);
send(sid,msg,strlen(msg),0);
printf("\n\tMsg : %s",msg);
if(strcmp(msg,"exit")==0)
{
close(sid);
exit(1);
}
}
}

int main()
{
int skt,bnd,len,new_skt,i;
pthread_t clnt;
struct sockaddr_in address;
skt=socket(AF_INET,SOCK_STREAM,0);
if(skt>0)
{
address.sin_family=AF_INET;
address.sin_port=htons(10000);
address.sin_addr.s_addr=INADDR_ANY;

bnd=bind(skt,(struct sockaddr * ) &address,sizeof(address));
if(bnd==0)
{
while(1)
{
listen(skt,3);
len=sizeof(struct sockaddr_in);
new_skt=accept(skt,(struct sockaddr*)&address,&len);

if(new_skt>0)
{
no++;
pthread_create(&clnt,NULL,thread_p,&new_skt);
}
else
printf("\n\t*** Rejected ***");
}
}
else
printf("\n\t***Binding error ***");
}
else
printf("\n\t*** Soc creation Error ***");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Client.c
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>

int main()
{
int skt,con,i;
char ip[10],msg[100];
struct sockaddr_in address;
skt = socket(AF_INET,SOCK_STREAM,0);
if(skt>0)
{
address.sin_family=AF_INET;
address.sin_port=htons(10000);
printf("\n\tEnter Server IP : ");
gets(ip);
inet_pton(AF_INET,ip,&address.sin_addr);
con=connect(skt,(struct sockaddr *) &address,sizeof(address));
if(con==0)
{
while(1)
{
for(i=0;i<100;i++)
msg[i]=NULL;
printf("\nClient : ");
gets(msg);
send(skt,msg,strlen(msg),0);
if(strcmp(msg,"exit")==0)
break;
for(i=0;i<100;i++)
msg[i]=NULL;

recv(skt,msg,100,0);
printf("\nServer : %s",msg);
if(strcmp(msg,"exit")==0)
break;
}
}
else
printf("\n\t*** CON ERROR ****");
}
else
printf("\n****Sock creation err ****");

close(skt);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
read more

C programm to calculate factorial

, by Prashant Gunjal



Theory :

As we know that
1) 5! = 1*2*3*4*5
2) 3! = 1*2*3
3) 6! = 1*2*3*4*5*6

by observing above 3 examples we can built logic.We will accept number and then run for loop up to number times and simultaneously
we will calculate factorial

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,fact;
clrscr();
printf("\n\t Enter the number : ");

scanf("%d",&no);
fact = 1;
for(i=1;i<=no;i++)
{
fact = i * fact;
}
printf("\n\t Factorial of %d is = %d ",no,fact);
getch();
}

Output :

read more