C pgm in Unix[1]

download C pgm in Unix[1]

of 15

Transcript of C pgm in Unix[1]

  • 8/12/2019 C pgm in Unix[1]

    1/15

    R.Aadhithya

    311612114001

    1. Dynamic Memory Allocation

    #include#include

    main(){char *str;str=(char *)malloc(11);strcpy(str,"uniersity");print!("n the string is s and the address is d n",str,str);str=(char *)realloc(str,$%);strcpy(str,"uniersity e&aminations");print!("n no' the string is s and the address is d n",str,str);!ree(str);

    Output

    1eee+-/0/ 23 .4a.out

    the string is uniersity and the address is 1-567-8

    no' the string is uniersity e&aminations and the address is 1-567-8

  • 8/12/2019 C pgm in Unix[1]

    2/15

    R.Aadhithya

    311612114001

    2. Swapping wo !aria"le#

    #includes'ap(int a, int +)

    {int t;t=a;a=+;+=t;print!("a!ter s'ap d ,d",a,+);main(){int 9,';print!("enter nos");

    scan!("dd",:9,:');print!("+e!ore s'ap d ,d",9,');s'ap(9,');

    Output1eee+-/0/ 23 .4a.outenter nos8+e!ore s'ap ,8a!ter s'ap 8 ,

  • 8/12/2019 C pgm in Unix[1]

    3/15

    R.Aadhithya

    311612114001

    3. Student Detail#

    #include#include

    #includemain(){int stdreg, stdage;char stdnme%2; *ptr;ptr=!open("std.dat" , "'");print!("nter the students name, age, register num");scan!("s d d" , :stdnme, :stdage, :stdreg);!print!(ptr,"s t d t d",stdnme,stdage,stdreg);!close(ptr);

    ptr=!open("std.dat" , "r");!scan!(ptr, "s d d" , :stdnme, :stdage, :stdreg);print!("n students name s",stdnme);print!("n students age, register num d d" , stdage, stdreg);

    O$%$&

    1eee+$$/0/ 23 .4a.outnter the students name, age, register numpagal 5 1$85

    students namepagalstudents age, register num5 1$85

  • 8/12/2019 C pgm in Unix[1]

    4/15

    R.Aadhithya

    311612114001

    4. 'ile (opy

    #include#includemain()

    {char ch,!ile1%2,!ile%2; *!s, *!t;print!("enter the name o! the !ile to copy");gets(!ile1);!s=!open(!ile1,"r");print!("enter the name o! the target !ile ");gets(!ile);!t=!open(!ile,"'");'hile ((ch=!getc(!s))?=@)!putc(ch,!t);

    print!("!ile copied succuss!ully n");!close(!s);!close(!t);

    ). 'ile Merge

    #include#includemain(){

    *!s1,*!s,*!st;char ch,!ile1%2,!ile%2,!ilet%2;print!("nnter the name o! the !irst !ile ");scan!("s",:!ile1);print!("nnter the name o! the second !ile ");scan!("s",:!ile);print!("nnter the name o! the !ile 'hich stores the contents o! the a+oe t'o!ilesn");scan!("s",:!ilet);!s1=!open(!ile1,"r");!s=!open(!ile,"r");

    !st=!open(!ilet,"'");'hile((ch=!getc(!s1))?=@)!putc(ch,!st);'hile((ch=!getc(!s))?=@)!putc(ch,!st);print!("nA'o !iles 'ere merged into s !ilen",!st);!close(!s1);!close(!s);!close(!st);

  • 8/12/2019 C pgm in Unix[1]

    5/15

    R.Aadhithya

    311612114001

  • 8/12/2019 C pgm in Unix[1]

    6/15

    R.Aadhithya

    311612114001

    6. Sum o* n +um"er#

    #includesum(int n){int i,B,s=%;

    print!("enter the num+ers");!or(i=%;i

  • 8/12/2019 C pgm in Unix[1]

    7/15

    R.Aadhithya

    311612114001

    ,. -mployee %ayroll

    4* Dayroll Eeneration *4

    #include

    struct employee

    {int empid;char ename152;int +asic;!loat hra;!loat da;!loat it;!loat gross;

    !loat netpay;

    ;

    main(){

    struct employee emp5%2, *ptr;int i, F, n;

    print!("nter Go. o! mployees ");scan!("d", :n);!or(i=%; i+asic;ptrK>da = %.%1 * ptrK>+asic;

    ptrK>it = %.%5 * ptrK>+asic;

    ptrK>gross = ptrK>+asicCptrK>hraCptrK>da;ptrK>netpay = ptrK>gross K ptrK>it;ptrCC;

    ptr=emp;

    print!("nnnttttLMN : /o. Dayrollnn");!or(i=%;i

  • 8/12/2019 C pgm in Unix[1]

    8/15

    R.Aadhithya

    311612114001

    print!("nmpdtGamettIasict OPQt HQt AtErossttGet Daynn"); !or(i=%;iename,ptrK

    >+asic,ptrK>hra,ptrK>da,ptrK>it,ptrK>gross, ptrK>netpay);ptrCC;print!("n"); !or(i=%;inter >mployee Hetails

    >nter >mployee nter >mployee Game ) Eopal>nter Iasic Jalary 1%%%%>nter >mployee Hetails>nter >mployee nter >mployee Game ) PaFesh

    >nter Iasic Jalary %%%

    LMN : /o. Dayroll

    ********************************************************************************

    mpd Game Iasic OPQ HQ A Eross Get Day

    ********************************************************************************

    8$- Eopal 1%%%% %%.%% 1%%.%% 5%%.%% 1%$%%.%% R7%%.%%

    8-$ PaFesh %%% 88%.%% %.%% 11%%.%% ,,--%.%%,15-%.%%

    *********************************************************

  • 8/12/2019 C pgm in Unix[1]

    9/15

    R.Aadhithya

    311612114001

    /. MAR M$%(AO+

    +eit%1localhost 23 i matri&mul.c

    #includeint main(){int r1,r,c1,c,a1%21%2,+1%21%2,c1%21%2,i,F,B;print!("n enter the order o! matri& Q");scan!("dd",:r1,:c1);print!("n enter the order o! matri& I");scan!("dd",:r,:c);i!(r1?=c)print!("n matri& multiplication is not possi+le");print!("n enter Q matri&n");

    !or(i=%;i

  • 8/12/2019 C pgm in Unix[1]

    10/15

    R.Aadhithya

    311612114001

    !or(F=%;F

  • 8/12/2019 C pgm in Unix[1]

    11/15

    R.Aadhithya

    311612114001

    . Arithmetic Operation# $#ing 'unction#

    #includeint add(int a,int +){int c;

    c=aC+;return c;int su+(int a,int +){int c;c=aK+;return c;int mul(int a,int +){int c; c=a*+;return c;main(){int a,+,c;

    print!("enter the num+ers");scan!("dd",:a,:+);'hile(1){print!("enter your option");scan!("d",:c);s'itch(c){case 1c=add(a,+);print!("the addition isd",c);+reaB;case

    c=su+(a,+);print!("the su+traction isd",c);+reaB;case $c=mul(a,+);print!("the multiplication isd",c);+reaB;case 8e&it(%);

    O$%$&"s'itch.c" 86, 5-/ 'ritten1eee+R/0/ 23 cc s'itch.c1eee+R/0/ 23 .4a.outenter the num+ers5 -enter your optionthe su+traction isK1enter your option1the addition is11

  • 8/12/2019 C pgm in Unix[1]

    12/15

    R.Aadhithya

    311612114001

    enter your option$the multiplication is$%enter your option8

  • 8/12/2019 C pgm in Unix[1]

    13/15

    R.Aadhithya

    311612114001

    10. Data Manipulation

    #include#includemain(){

    char ch,ct;int choice,tr,letters=%,'ords=%,spaces=%,lines=%; *!p1,*!p;do{!p1=!open("data1.t&t","r");print!("n nter your choice n");print!("n 1.read the !ile n . /opy the !ile n $. /hange n 8./ount n 5.&it");scan! ("d",:choice);s'itch(choice){

    case 1'hile(?!eo!(!p1)){ch=getc(!p1);print!("c",ch);+reaB;case !p=!open("data.t&t","'");!seeB(!p1, 1 ,JSTJA);'hile(?!eo!(!p1))

    {ct=!getc(!p1);tr=!putc(ct,!p);!close(!p1);!close(!p);!p=!open("data.t&t","r");print!(" the content o! copeid !ile is ");'hile (?!eo!(!p)){ch=!getc(!p);

    print!("c",ch);!close(!p);+reaB;case $!p=!open("data.t&t","r");'hile(?!eo! (!p)){ch=getc(!p);

  • 8/12/2019 C pgm in Unix[1]

    14/15

    R.Aadhithya

    311612114001

    i!((ch=-5)){ch=chC$;print!("c",ch);

    else i!((ch>=R6) :: (ch

  • 8/12/2019 C pgm in Unix[1]

    15/15

    R.Aadhithya

    311612114001

    Output &

    1ecea5$/0/ 23 cc !ilemanipulation.c1ecea5$/0/ 23 .4a.outnter your choice

    1. Pead the !ile. /opy the !ile$. /hange8. /ount5. &it1O 'elcomeO 'elcomenter your choice1. Pead the !ile. /opy the !ile

    $. /hange8. /ount5. &itAhe content o! copied !ile is O 'elcomenter your choice1. Pead the !ile. /opy the !ile$. /hange8. /ount5. &it

    $hi V/@0nter your choice1. Pead the !ile. /opy the !ile$. /hange8. /ount5. &it8total lines1total 'ords

    total space1total letters1%nter your choice1. read the !ile. /opy the !ile$. /hange8./ount5.&it5